forked from Elijas/auto-youtube-subscription-playlist-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheetScript.gs
213 lines (184 loc) · 6.96 KB
/
sheetScript.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
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
// TODO: Better exception handling for Youtube API calls
// TODO: Deal with playlist limits (~ 200-218 videos)
// TODO: Special keyword "ALLOTHER" for all other (unmentioned yet in the app) channel ids
function updatePlaylists() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var data = sheet.getDataRange().getValues();
var reservedTableRows = 3; // Start of the range of the PlaylistID+ChannelID data
var reservedTableColumns = 2; // Start of the range of the ChannelID data
var reservedTimestampCell = "F1";
if (!sheet.getRange(reservedTimestampCell).getValue()) sheet.getRange(reservedTimestampCell).setValue(ISODateString(new Date()));
var debugFlag_dontUpdateTimestamp = true;
var debugFlag_dontUpdatePlaylists = false;
/// For each playlist...
for (var iRow = reservedTableRows; iRow < sheet.getLastRow(); iRow++) {
var playlistId = data[iRow][0];
if (!playlistId) continue;
/// ...get channels...
var channelIds = [];
for (var iColumn = reservedTableColumns; iColumn < sheet.getLastColumn(); iColumn++) {
var channel = data[iRow][iColumn];
if (!channel) continue;
else if (channel == "ALL")
channelIds.push.apply(channelIds, getAllChannelIds());
else if (!(channel.substring(0,2) == "UC" && channel.length > 10)) // Check if it is not a channel ID (therefore a username). MaybeTODO: do a better validation, since might interpret a channel with a name "UC..." as a channel ID
{
try {
channelIds.push(YouTube.Channels.list('id', {forUsername: channel, maxResults: 1}).items[0].id);
} catch (e) {
Logger.log("ERROR: " + e.message);
continue;
}
}
else
channelIds.push(channel);
}
/// ...get videos from the channels...
var videoIds = [];
var lastTimestamp = sheet.getRange(reservedTimestampCell).getValue();
for (var i = 0; i < channelIds.length; i++) {
videoIds.push.apply(videoIds, getVideoIds(channelIds[i], lastTimestamp)); // Append new videoIds array to the original one
}
if (!debugFlag_dontUpdateTimestamp) sheet.getRange(reservedTimestampCell).setValue(ISODateString(new Date())); // Update timestamp
/// ...add videos to the playlist
if (!debugFlag_dontUpdatePlaylists) {
for (var i = 0; i < videoIds.length; i++) {
try {
YouTube.PlaylistItems.insert
( { snippet:
{ playlistId: playlistId,
resourceId:
{ videoId: videoIds[i],
kind: 'youtube#video'
}
}
}, 'snippet,contentDetails'
);
} catch (e) {
Logger.log("ERROR: " + e.message);
continue;
}
Utilities.sleep(1000);
}
}
}
}
function getVideoIds(channelId, lastTimestamp) {
var videoIds = [];
// First call
try {
var results = YouTube.Search.list('id', {
channelId: channelId,
maxResults: 50,
order: "date",
publishedAfter: lastTimestamp
});
} catch (e) {
Logger.log("ERROR: " + e.message);
return;
}
for (var j = 0; j < results.items.length; j++) {
var item = results.items[j];
videoIds.push(item.id.videoId);
}
// Other calls
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) {
Logger.log("ERROR: " + e.message);
continue;
}
for (var j = 0; j < results.items.length; j++) {
var item = results.items[j];
videoIds.push(item.id.videoId);
}
nextPageToken = results.nextPageToken;
}
return videoIds;
}
function getAllChannelIds() { // get YT Subscriptions-List, src: https://www.reddit.com/r/youtube/comments/3br98c/a_way_to_automatically_add_subscriptions_to/
var AboResponse, AboList = [[],[]], nextPageToken = [], nptPage = 0, i, ix;
// Workaround: nextPageToken API-Bug (this Tokens are limited to 1000 Subscriptions... but you can add more Tokens.)
nextPageToken = ['','CDIQAA','CGQQAA','CJYBEAA','CMgBEAA','CPoBEAA','CKwCEAA','CN4CEAA','CJADEAA','CMIDEAA','CPQDEAA','CKYEEAA','CNgEEAA','CIoFEAA','CLwFEAA','CO4FEAA','CKAGEAA','CNIGEAA','CIQHEAA','CLYHEAA'];
try {
do {
AboResponse = YouTube.Subscriptions.list('snippet', {
mine: true,
maxResults: 50,
order: 'alphabetical',
pageToken: nextPageToken[nptPage],
fields: 'items(snippet(title,resourceId(channelId)))'
});
for (i = 0, ix = AboResponse.items.length; i < ix; i++) {
AboList[0][AboList[0].length] = AboResponse.items[i].snippet.title;
AboList[1][AboList[1].length] = AboResponse.items[i].snippet.resourceId.channelId;
}
nptPage += 1;
} while (AboResponse.items.length > 0 && nptPage < 20);
if (AboList[0].length !== AboList[1].length) {
return 'Length Title != ChannelId'; // returns a string === error
}
} catch (e) {
return e;
}
Logger.log('Acquired subscriptions %s', AboList[1].length);
return AboList[1];
}
function getAllChannelIds_OLD() { // Note: this function is not used.
var channelIds = [];
// First call
try {
var results = YouTube.Subscriptions.list('snippet', {
mine: true,
maxResults: 50
});
} catch (e) {
Logger.log("ERROR: " + e.message);
return;
}
for (var i = 0; i < results.items.length; i++) {
var item = results.items[i];
channelIds.push(item.snippet.resourceId.channelId);
}
// Other calls
var nextPageToken = results.nextPageToken;
for (var pageNo = 0; pageNo < (-1+Math.ceil(results.pageInfo.totalResults / 50.0)); pageNo++) {
try {
results = YouTube.Subscriptions.list('snippet', {
mine: true,
maxResults: 50,
pageToken: nextPageToken
});
} catch (e) {
Logger.log("ERROR: " + e.message);
continue;
}
for (var i = 0; i < results.items.length; i++) {
var item = results.items[i];
channelIds.push(item.snippet.resourceId.channelId);
}
nextPageToken = results.nextPageToken;
}
Logger.log('Acquired subscriptions %s, Actual subscriptions %s', channelIds.length, results.pageInfo.totalResults);
return channelIds;
}
function ISODateString(d) { // modified from src: http://stackoverflow.com/questions/7244246/generate-an-rfc-3339-timestamp-similar-to-google-tasks-api
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'.000Z'
}
function onOpen() {
SpreadsheetApp.getActiveSpreadsheet().addMenu("Functions", [{name: "Update Playlists", functionName: "updatePlaylists"}]);
}