-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (68 loc) · 1.87 KB
/
app.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
const {google} = require('googleapis');
const key = require('./key.json');
const youtube = google.youtube('v3');
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/youtube'],
null
);
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
}
const videoId = 'Q4VGQPk2Dl8';
const channelId = 'UCBJycsmduvYEL83R_U4JriQ';
getVideosByChannel(jwtClient, channelId);
// getVideoMetadata(jwtClient, videoId);
});
let videoIds = [];
// Note that querying with channelId has limitation for 500 items
// Find more on https://developers.google.com/youtube/v3/docs/search/list#channelId
function getVideosByChannel(auth, channelId, pageToken){
youtube.search.list({
channelId: channelId,
auth: auth,
part: 'id',
type: 'video',
maxResults: 50,
pageToken: pageToken
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
const {data} = response;
const {nextPageToken} = data;
const {items} = data;
const ids = items.map(i => i.id.videoId);
videoIds = videoIds.concat(ids);
if(nextPageToken && nextPageToken.length > 0 && items.length > 0){
getVideosByChannel(auth, channelId, nextPageToken);
} else {
console.log('Done', videoIds.length)
}
});
}
function getVideoMetadata(auth) {
youtube.videos.list(
{
id: videoId,
auth: auth,
part: 'id,snippet,statistics'
},
function(err, response) {
if (err) {
console.log(err);
}
for (item of response.data.items) {
console.log('Title:', item.snippet.title);
console.log('Publish Date:', item.snippet.publishedAt);
console.log('Channel:', item.snippet.channelTitle);
console.log('Statistics:', item.statistics);
}
}
);
}