@google-cloud/nodejs-common 0.9.5-beta → 0.9.5-beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/apis/youtube.js +89 -1
package/package.json
CHANGED
package/src/apis/youtube.js
CHANGED
|
@@ -24,6 +24,7 @@ const {
|
|
|
24
24
|
Schema$Video,
|
|
25
25
|
Schema$CommentThread,
|
|
26
26
|
Schema$Playlist,
|
|
27
|
+
Schema$Search,
|
|
27
28
|
} = google.youtube;
|
|
28
29
|
const AuthClient = require('./auth_client.js');
|
|
29
30
|
const {getLogger} = require('../components/utils.js');
|
|
@@ -105,6 +106,45 @@ let ListCommentThreadsConfig;
|
|
|
105
106
|
*/
|
|
106
107
|
let ListPlaylistConfig;
|
|
107
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Configuration for listing youtube search results.
|
|
111
|
+
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
112
|
+
* @typedef {{
|
|
113
|
+
* part: Array<string>,
|
|
114
|
+
* forContentOwner: (boolean|undefined),
|
|
115
|
+
* forDeveloper: (boolean|undefined),
|
|
116
|
+
* forMine: (boolean|undefined),
|
|
117
|
+
* relatedToVideoId: (string|undefined),
|
|
118
|
+
* channelId: (string|undefined),
|
|
119
|
+
* channelType: (string|undefined),
|
|
120
|
+
* eventType: (string|undefined),
|
|
121
|
+
* location: (string|undefined),
|
|
122
|
+
* locationRadius: (string|undefined),
|
|
123
|
+
* maxResults: (unsigned integer|undefined),
|
|
124
|
+
* onBehalfOfContentOwner: (string|undefined),
|
|
125
|
+
* order: (string|undefined),
|
|
126
|
+
* pageToken: (string|undefined),
|
|
127
|
+
* publishedAfter: (datetime|undefined),
|
|
128
|
+
* publishedBefore: (datetime|undefined),
|
|
129
|
+
* q: (string|undefined),
|
|
130
|
+
* regionCode: (string|undefined),
|
|
131
|
+
* relevanceLanguage: (string|undefined),
|
|
132
|
+
* safeSearch: (string|undefined),
|
|
133
|
+
* topicId: (string|undefined),
|
|
134
|
+
* type: (string|undefined),
|
|
135
|
+
* videoCaption: (string|undefined),
|
|
136
|
+
* videoCategoryId: (string|undefined),
|
|
137
|
+
* videoDefinition: (string|undefined),
|
|
138
|
+
* videoDimension: (string|undefined),
|
|
139
|
+
* videoDuration: (string|undefined),
|
|
140
|
+
* videoEmbeddable: (string|undefined),
|
|
141
|
+
* videoLicense: (string|undefined),
|
|
142
|
+
* videoSyndicated: (string|undefined),
|
|
143
|
+
* videoType: (string|undefined)
|
|
144
|
+
* }}
|
|
145
|
+
*/
|
|
146
|
+
let ListSearchConfig;
|
|
147
|
+
|
|
108
148
|
/**
|
|
109
149
|
* Youtube API v3 stub.
|
|
110
150
|
* See: https://developers.google.com/youtube/v3/docs
|
|
@@ -116,6 +156,8 @@ let ListPlaylistConfig;
|
|
|
116
156
|
* https://developers.google.com/youtube/v3/docs/commentThreads/list
|
|
117
157
|
* Playlist list type definition, see:
|
|
118
158
|
* https://developers.google.com/youtube/v3/docs/playlists/list
|
|
159
|
+
* Search list type definition, see:
|
|
160
|
+
* https://developers.google.com/youtube/v3/docs/search/list
|
|
119
161
|
*/
|
|
120
162
|
class YouTube {
|
|
121
163
|
/**
|
|
@@ -220,7 +262,7 @@ class YouTube {
|
|
|
220
262
|
* @return {!Promise<Array<Schema$Playlist>>}
|
|
221
263
|
*/
|
|
222
264
|
async listPlaylists(config, resultLimit = 1000, pageToken = null) {
|
|
223
|
-
if (resultLimit <= 0)
|
|
265
|
+
if (resultLimit <= 0) return [];
|
|
224
266
|
|
|
225
267
|
const playlistsRequest = Object.assign({
|
|
226
268
|
auth: this.auth,
|
|
@@ -255,6 +297,51 @@ class YouTube {
|
|
|
255
297
|
throw new Error(errorMsg);
|
|
256
298
|
}
|
|
257
299
|
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Returns a collection of search results that match the query parameters
|
|
303
|
+
* specified in the API request.
|
|
304
|
+
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
305
|
+
* @param {!ListSearchConfig} config List search result configuration.
|
|
306
|
+
* @param {number} resultLimit The limit of the number of results.
|
|
307
|
+
* @param {string} pageToken Token to identify a specific page in the result.
|
|
308
|
+
* @return {!Promise<Array<Schema$Search>>}
|
|
309
|
+
*/
|
|
310
|
+
async listSearchResults(config, resultLimit = 1000, pageToken = null) {
|
|
311
|
+
if (resultLimit <= 0) return [];
|
|
312
|
+
|
|
313
|
+
const searchRequest = Object.assign({
|
|
314
|
+
auth: this.auth,
|
|
315
|
+
}, config, {
|
|
316
|
+
pageToken
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
if (Array.isArray(searchRequest.part)) {
|
|
320
|
+
searchRequest.part = searchRequest.part.join(',');
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
const response = await this.instance.search.list(searchRequest);
|
|
325
|
+
this.logger.debug('Response: ', response.data);
|
|
326
|
+
if (response.data.nextPageToken) {
|
|
327
|
+
this.logger.debug(
|
|
328
|
+
'Call youtube search:list API agian with Token: ',
|
|
329
|
+
response.data.nextPageToken);
|
|
330
|
+
const nextResponse = await this.listSearchResults(
|
|
331
|
+
config,
|
|
332
|
+
resultLimit - response.data.items.length,
|
|
333
|
+
response.data.nextPageToken);
|
|
334
|
+
return response.data.items.concat(nextResponse);
|
|
335
|
+
}
|
|
336
|
+
return response.data.items;
|
|
337
|
+
} catch (error) {
|
|
338
|
+
const errorMsg =
|
|
339
|
+
`Fail to list search results: ${JSON.stringify(searchRequest)}`;
|
|
340
|
+
this.logger.error('YouTube list search results failed.', error.message);
|
|
341
|
+
this.logger.debug('Errors in response:', error);
|
|
342
|
+
throw new Error(errorMsg);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
258
345
|
}
|
|
259
346
|
|
|
260
347
|
module.exports = {
|
|
@@ -263,6 +350,7 @@ module.exports = {
|
|
|
263
350
|
ListVideosConfig,
|
|
264
351
|
ListCommentThreadsConfig,
|
|
265
352
|
ListPlaylistConfig,
|
|
353
|
+
ListSearchConfig,
|
|
266
354
|
API_VERSION,
|
|
267
355
|
API_SCOPES,
|
|
268
356
|
};
|