-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
debug.gs
90 lines (78 loc) · 2.31 KB
/
debug.gs
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
// The core functionality of the program, used for debugging
// Instructions:
// 1. Create an empty Docs or Sheet File
// 2. Open Script Editor and copy-paste the contents
// 3. Change values in main() function
// 4. Select 'main' and run it, authorize (`Resources`/`Advanced Google Services`)
// 5. See the log (Ctrl+Enter in the Script Editor) for details
function err(id,msg) {
out("ERROR in "+id+": "+msg);
}
function out(s) {
Logger.log(s);
}
////////////////////////
function main() {
var lastTimestamp = "2016-04-30T13:22:53.000Z"
var playlistId = "PLN-2p7teOQQwXuihgy5E3lAywmd_pHjo"; // *** CHANGE THIS ***
var channelId = "UCbiGcwDWZjz05njNPrJU7jA"; //a.k.a. "ExplainingComputers"
addNewVideosToPlaylist(channelId, lastTimestamp, playlistId)
}
////////////////////////
function addNewVideosToPlaylist(channelId, lastTimestamp, playlistId) {
// First request
try {
var results = YouTube.Search.list('id', {
channelId: channelId,
maxResults: 50,
order: "date",
publishedAfter: lastTimestamp
});
} catch (e) {
err(1,e.message);
throw new Error(e);
}
addVideosToPlaylist(results, playlistId);
// Subsequent requests
var nextPageToken = results.nextPageToken;
for (var pageNo = 0; pageNo < (-1+Math.ceil(results.pageInfo.totalResults / 50.0)); pageNo++) {
try {
results = YouTube.Search.list('id', {
channelId: channelId,
maxResults: 50,
order: "date",
publishedAfter: lastTimestamp,
pageToken: nextPageToken
});
} catch (e) {
err(2,e.message);
throw new Error(e);
}
addVideosToPlaylist(results, playlistId);
nextPageToken = results.nextPageToken;
}
}
function addVideosToPlaylist(results, playlistId) {
Logger.log(results);
for (var j = 0; j < results.items.length; j++) {
var videoId = results.items[j].id.videoId;
if (videoId == undefined) continue;
Logger.log(videoId);
try {
YouTube.PlaylistItems.insert( {
snippet: {
playlistId: playlistId,
resourceId: {
videoId: videoId,
kind: 'youtube#video'
}
}
},
'snippet'//,contentDetails'
);
} catch (e) {
err('adding video '+videoId+' to '+playlistId,e.message);
throw new Error(e);
}
}
}