-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
375 lines (322 loc) · 11.1 KB
/
api.js
File metadata and controls
375 lines (322 loc) · 11.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*jshint node:true,expr:true*/
'use strict';
var spashttp = require("spas-http"),
_ = require("underscore")._,
async = require("async");
var getVideoDetails = function (credentials) {
// If this bundle is using oauth2, add in the access token
var tokenString = _.isObject(credentials) && _.has(credentials, 'access_token') ?
"&access_token=" + credentials.access_token :
'';
return function (obj, cb) {
spashttp.request({url: "http://gdata.youtube.com/feeds/api/videos/" + obj.media$group.yt$videoid.$t + '?v=2&alt=json' + tokenString }, credentials, function( err, video ) {
if(video && _.has(video, 'entry')) {
obj.media$group.media$keywords.$t = video.entry.media$group.media$keywords.$t;
obj.category = video.entry.category;
}
cb(err);
});
};
};
exports.custom = {
videosWithKeywords: function(params, credentials, cb) {
params.url = "http://gdata.youtube.com/feeds/api/videos?v=2";
// Ensure we have a number to perform calculation.
var maxResults = parseInt(params['max-results']) || 50;
var pages = Math.floor((maxResults-1)/50) + 1;
var startIndices = [];
// Prep an array for starting indices to use with `async.concat`
for (var i = 0; i < pages; i++) { startIndices[i] = i*50 + 1; }
// In order to concat the video entries only
var data;
async.concat(startIndices, function (startIndex, callback) {
var shadowed = _.clone(params);
shadowed['max-results'] = maxResults >= 50 ? '50' : maxResults.toString(10);
shadowed['start-index'] = startIndex.toString(10);
spashttp.request(shadowed, credentials, function ( err, videos ) {
if (_.has(videos, 'feed') && videos.feed.entry) {
// Save meta data outside.
if (!data) data = videos;
async.each(videos.feed.entry, getVideoDetails(credentials), function (err) {
callback(err, videos.feed.entry);
});
} else {
callback( err, [] );
}
});
}, function(err, results) {
// After concatenation is done, save the entries back and return.
data.feed.entry = results;
cb(err, data);
});
}
};
var BASE_V3_API = "https://www.googleapis.com/youtube/v3";
/**
* Recursively retrieves items from an API endpoint
*
* @param {Number} [maxResults] - how many to pull if set. Otherwise,
* pull all items.
*
* Requests items from an API endpoint recursively until there is no
* `.nextPageToken` in the response. The `params.maxResults` will be
* recalculated to see if next request is needed.
*
* When `params.maxResults` is set to less than 50, first request
* returns the exact amount specified, and function terminates.
*
* Now, if it is set to greater than 50, the first request is capped
* at 50 (due to API's limit). The response will contain the next
* page token if the total results are more than 50. Substracting
* the different between acquired items and `params.maxResults`
* will give the next `params.maxResults` to query.
*
* Another extreme case is `params.maxResults` set to really high,
* way over the total results. In this case, at the last recursive
* call for `totalResults % 50`, there won't be `.nextPageToken`,
* so there won't be next call, fulfill the set.
*/
function requestUntil(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
var limit = params.maxResults || 0;
if (!limit) {
// No limit, caps at 50.
params.maxResults = 50;
}
if (limit && limit > 50) {
// User specified a limit that is greater than 50, also cap at 50.
params.maxResults = 50;
}
spashttp.request(params, credentials, function (err, result) {
// YouTube API returns the error in the response.
if (err || result.error) {
return cb(err || result.error);
}
var items = result.items;
if (limit !== items.length && result.nextPageToken) {
// API returns a token for next page, we use it to go to the next one.
// Also, only when we haven't gotten the exact amount of items needed.
params.pageToken = result.nextPageToken;
var remainings = limit - items.length;
// Remainings will be gt than 0, except when limit is 0, because
// this clause only happens when there is next page, meaning either
// limit was set to 0 or gt 50.
params.maxResults = remainings > 0? remainings : 0;
// Recursively call the next results page, appending to the items.
requestUntil(params, credentials, function(err, nextPageItems) {
if (err) {
return cb(err);
}
Array.prototype.push.apply(items, nextPageItems);
cb(null, items);
});
} else {
// Base case, no more results, bail out.
cb(null, items);
}
});
}
function channels(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
params.url = BASE_V3_API + "/channels";
if (!params.forUsername) {
params.mine = true;
}
if (!params.part) {
params.part = "contentDetails";
}
if (credentials && credentials.access_token) {
params.access_token = credentials.access_token;
}
return spashttp.request(params, credentials, function (err, channelsResult) {
if (err || channelsResult.error) {
// YouTube API returns the error in the response.
return cb(err || channelsResult.error);
}
cb(null, channelsResult.items[0][params.part]);
});
}
/**
* Retrieves a list of playlists in a channel
*
* @param {string} channelId - ID of the user's channel
* @param {string} [part=snippet] - comma-separated list of data to retrieve
* @param {Number} [maxResults] - how many to pull if set.
*
* For now, `channelId` is required. By using `v3.channels`, this ID can be
* acquired in the `id` property.
*/
function playlists(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
params.url = BASE_V3_API + "/playlists";
if (!params.part) {
params.part = "snippet";
}
if (credentials && credentials.access_token) {
params.access_token = credentials.access_token;
}
requestUntil(params, credentials, function (err, items) {
if (err) {
return cb(err);
}
cb(null, items);
});
}
/**
* Recursively retrieves videos from a playlist
*
* @param {string} playlistId - The ID of the playlist (default to uploads).
* @param {string} [part=snippet] - comma-separated list of data to retrieve.
* @param {Number} [maxResults] - how many to pull if set. Otherwise,
* pull all videos.
*
* Requests items in playlist recursively until `params.maxResults` or all.
*/
function playlistItems(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
if (!params.playlistId) {
// Retrived from channels.list for uploads playlist.
var channelsParams = {
part: "contentDetails",
forUsername: params.author,
key: params.key
};
return channels(channelsParams, credentials, function (err, contentDetails) {
if (err) {
return cb(err);
}
params.playlistId = contentDetails.relatedPlaylists.uploads;
return playlistItems(params, credentials, cb);
});
}
params.url = BASE_V3_API + "/playlistItems";
if (!params.part) {
// Retrived from playlist.list for uploads playlist.
params.part = "snippet";
}
params.part = params.part.replace(/,statistics/, '');
if (credentials && credentials.access_token) {
params.access_token = credentials.access_token;
}
requestUntil(params, credentials, function (err, items) {
if (err) {
return cb(err);
}
items = items.map(function (item) {
// We need the video's ID, which is nested inside.
item.snippet.id = item.snippet.resourceId.videoId;
return item;
});
cb(null, items);
});
}
/**
* Recursively retrieves videos from a list of IDs
*
* @param {array} id - The list of video IDs to retrieve.
* @param {string} [part=snippet] - comma-separated list of data to retrieve.
*
* The function requests videos with details from `/videos` endpoint
* It splits the `params.id` array into chunk of 50 and recursively
* makes requests.
*
* The total number of requests is `params.id.length` div 50.
*/
function videos(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
params.url = BASE_V3_API + "/videos";
if (!params.part) {
params.part = "snippet";
}
if (credentials && credentials.access_token) {
params.access_token = credentials.access_token;
}
// Remove the first 50 IDs (due to YouTube's restriction)
// After spliced, params.id has the first 50.
var ids = params.id;
var restIds = ids.splice(50, ids.length);
params.id = ids.join(",");
spashttp.request(params, credentials, function (err, result) {
if (err || result.error) {
return cb(err || result.error);
}
var items = result.items;
if (restIds.length > 0) {
params.id = restIds;
// Still needs more, recursively call this again and merge results
videos(params, credentials, function (err, nextItems) {
if (err) {
return cb(err);
}
Array.prototype.push.apply(items, nextItems);
cb(err, items);
});
} else {
cb(err, result.items);
}
});
}
/**
* Get detail videos in a playlist
*
* @param {string} playlistId - The ID of the playlist (default to `uploads`).
* @param {string} [part=snippet] - comma-separated list of data to retrieve.
* @param {Number} [maxResults] - how many to pull if set. Otherwise,
* pull all videos.
*
* The name of the function is misleading. It does not only pull tags, but
* also all details of the videos (depending on `params.part` of course).
* The name is for legacy reason.
*
* The maximum number of requests for a N item playlist:
* (N div 50) + (N div 50).
*/
function playlistItemsWithTags(params, credentials, cb) {
playlistItems(params, credentials, function (err, items) {
if (err) {
return cb(err);
}
var ids = items.map(function (item) {
return item.snippet.id;
});
var videoParams = {
id: ids,
key: params.key,
part: params.part || "snippet"
};
videos(videoParams, credentials, cb);
});
}
/**
* Performs a search query
*
* Supports all parameters from
* https://developers.google.com/youtube/v3/docs/search/list,
* with the exception of `maxResults`. It can be set higher
* than YouTube limit of 50.
*/
function search(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
params.url = BASE_V3_API + "/search";
if (!params.part) {
params.part = "snippet";
}
if (credentials && credentials.access_token) {
params.access_token = credentials.access_token;
}
requestUntil(params, credentials, cb);
}
exports.v3 = {
search: search,
videos: videos,
channels: channels,
playlists: playlists,
playlistItems: playlistItems,
playlistItemsWithTags: playlistItemsWithTags
};