forked from dtex/spas-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
153 lines (134 loc) · 5.68 KB
/
api.js
File metadata and controls
153 lines (134 loc) · 5.68 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
/*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);
});
}
};
/**
* Recursively retrieves uploaded video with tags
*
* @param {string} playlistId - The ID of the `uploads` playlist.
* @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 function requests `uploads` playlist 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 uploadedVideo(params, credentials, cb) {
/* Clone the params to avoid messing with the API data */
params = _.clone(params);
var BASE_API = "https://www.googleapis.com/youtube/v3",
limit = params.maxResults || 0;
// No limit, caps at 50.
limit || (params.maxResults = 50);
// User specified a limit that is greater than 50, also cap at 50.
limit && limit > 50 && (params.maxResults = 50);
params.url = BASE_API + "/playlistItems";
// Retrived from playlist.list for uploads playlist.
params.part || (params.part = "snippet");
spashttp.request(params, credentials, function (err, playlistResult) {
if (err || !playlistResult.items) {
return cb(err, playlistResult);
}
var items = playlistResult.items;
async.map(items, function(item, callback) {
// Extra request per item to get the tags
var videoParams = {
url: BASE_API + "/videos",
id: item.snippet.resourceId.videoId,
part: params.part
};
credentials.access_token && (videoParams.access_token = credentials.access_token);
spashttp.request(videoParams, credentials, function(err, result) {
callback(err, result && result.items && result.items[0]);
});
}, function(err, fullItems) {
// Store it back to the first request's result.
items = playlistResult.items = fullItems;
// All items now have tags.
if (limit !== items.length && playlistResult.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 = playlistResult.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.
uploadedVideo(params, credentials, function(err, nextPageResult) {
Array.prototype.push.apply(items, nextPageResult.items);
cb(err, playlistResult);
});
} else {
// Base case, no more results, bail out.
cb(err, playlistResult);
}
});
});
}
exports.v3 = {
videosWithKeywords: uploadedVideo
};