forked from jatzek/ng-youtube-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netgenes.ng-youtube-search.js
158 lines (106 loc) · 4.36 KB
/
netgenes.ng-youtube-search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Created by jacek on 22.02.16.
*/
(function ( angular ) {
angular
.module('netgenes.ng-youtube-search',['ng'])
.provider('youtubeSearch', function youtubeSearchProvider() {
var key;
var defaultParams;
var apiEndpoint;
defaultParams = {
part: 'snippet',
type: 'video',
maxResults: 10,
order : 'rating'
};
apiEndpoint = 'https://www.googleapis.com/youtube/v3/search';
this.setKey = function setKey( providedKey ) {
key = providedKey;
};
this.setDefaultsParams = function setDefaultParams( params ) {
if (angular.isObject( params) ) {
angular.extend( defaultParams, params );
}
};
this.$get = function youtubeSearchFactory( $http ) {
if ('undefined' === typeof key) {
throw new Error('youtubeSearch service isn\'t setup properly. Please setup api key using youtubeSearchProvider.setKey( key )');
}
function YoutubeVideo( item ) {
var apiEndpoint = 'https://www.googleapis.com/youtube/v3/videos';
var self = this;
this.v = item;
this.f = null;
this.getFileDetails = function getFileDetails( ) {
var params = { key : key, part: 'contentDetails', id : this.v.id.videoId };
$http.get( apiEndpoint, { params: params } ).then(function ( responseObject ) {
self.f = responseObject.data.items[0];
});
}
}
function search( query, extraParams ) {
return new Promise( function ( resolve, reject ) {
var params;
params = angular.extend( { key : key, q: query }, defaultParams, extraParams || {} );
$http.get(apiEndpoint,{ params:params }).then(function( responseObject ) {
var items = responseObject.data.items;
var videos = [];
if ( items && items.length ) {
angular.forEach( items , function( item) {
videos.push(new YoutubeVideo( item ));
});
}
resolve(videos);
}, reject);
});
}
return search;
};
})
.constant('extractYtId', function( url ) {
var pattern = /v\=([^&]+)/;
if (!pattern.test(url)) {
return null;
}
return pattern.exec(url)[1];
})
.constant('checkYtId', function( id ) {
var pattern = /^([\w\_\-]+)$/;
if (!pattern.test(id)) {
return null;
}
return pattern.exec(id)[0];
})
.factory('ytBrowserToEmbed', function( checkYtId, extractYtId ) {
return function( url ) {
var id;
id = checkYtId( url ) || extractYtId(url);
return '//www.youtube.com/embed/' + id;
}
})
.filter('ytTimeFormat', function() {
return function(value) {
if (!value) {
return '-';
}
var hours, minutes, fMinutes, seconds, fSeconds, hRx, mRx, sRx, parts;
parts = [];
hRx = /(\d+)H/;
mRx = /(\d+)M/;
sRx = /(\d+)S/;
if (hRx.test(value))
{
hours = hRx.exec(value)[1];
parts.push(hours);
}
minutes = mRx.exec(value) ? mRx.exec(value)[1] : '0';
fMinutes = hours ? ('0'+minutes).slice( -2 ) : minutes;
parts.push(fMinutes);
seconds = sRx.exec(value) ? sRx.exec(value)[1] : '00';
fSeconds = ('00'+seconds).slice(-2);
parts.push(fSeconds);
return parts.join(':');
}
});
})( window.angular );