-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.js
361 lines (308 loc) · 10.7 KB
/
code.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const baseUrl = "https://api.trakt.tv/";
// Data URLs
const apiUrl = baseUrl + "users/me/";
const pathList = [
"?extended=full", // profile info
"collection/movies?extended=metadata",
"collection/shows?extended=metadata",
"followers",
"following",
"friends",
"history/episodes?limit=250",
"history/movies?limit=250",
"stats",
"watched/movies",
"watched/shows",
];
const paginatedPathList = [
"comments/all?include_replies=true&limit=250",
"likes/comments?limit=250",
"favorites/movies?limit=250",
"favorites/shows?limit=250",
"ratings/episodes?limit=250",
"ratings/movies?limit=250",
"ratings/seasons?limit=250",
"ratings/shows?limit=250",
"watchlist/episodes?limit=250",
"watchlist/movies?limit=250",
"watchlist/seasons?limit=250",
"watchlist/shows?limit=250",
];
// Authentication URLs
const authUrl = baseUrl + "/oauth/device/code";
const pollUrl = baseUrl + "/oauth/device/token";
const refreshUrl = baseUrl + "/oauth/token";
function getData(authInfo, url, getAllPages = false)
{
var options = {
"headers":
{
"Authorization": "Bearer " + authInfo.accessToken,
"Content-Type": "application/json",
"trakt-api-version": "2",
"trakt-api-key": authInfo.clientId
},
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options);
var headers = response.getHeaders();
var data = response.getContentText();
// Bail out if we only wanted the first page, or
// this data isn't paginated, or there's only one page to get
if (!getAllPages || !headers.hasOwnProperty("x-pagination-page-count") ||
headers["x-pagination-page-count"] == 1)
{
return data;
}
// Retrieve page count
var totalPages = headers["x-pagination-page-count"];
data = JSON.parse(data);
for (let page = 2; page <= totalPages; page++)
{
var pageUrl = url + `&page=${page}`;
response = UrlFetchApp.fetch(pageUrl, options);
var newData = JSON.parse(response.getContentText());
data = data.concat(newData);
}
return JSON.stringify(data);
}
function getFreshAuth()
{
// Retrieve refreshable auth info from scratch
// Using interactive "device" auth
// Initial Request - get code for user to enter
var options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': {
"client_id": config.clientId,
}
};
var response = UrlFetchApp.fetch(authUrl, options);
// Retrieve values from response
var initResp = JSON.parse(response.getContentText());
const deviceCode = initResp.device_code;
const expiry = initResp.expires_in;
const interval = initResp.interval;
// Show the user where to go & what to enter
Logger.log("Go to %s and enter: %s", initResp.verification_url,
initResp.user_code);
// Poll Request - check if the app is authorised yet
var queryTime = 0;
while (queryTime < expiry)
{
var payload = {
"code": deviceCode,
"client_id": config.clientId,
"client_secret": config.clientSecret,
};
options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': payload,
'muteHttpExceptions': true
};
response = UrlFetchApp.fetch(pollUrl, options);
// Logger.log(response);
// If the response was a success, we're authorised
if (response.getResponseCode() == 200)
{
// Grab the values we're looking for and return them
var newTokens = JSON.parse(response.getContentText());
var authInfo = new Object();
authInfo.accessToken = newTokens.access_token;
authInfo.refreshToken = newTokens.refresh_token;
authInfo.expiry = newTokens.created_at + newTokens.expires_in;
return authInfo;
}
// If the response is anything other than 'pending', something's wrong
else if (response.getResponseCode() != 400)
{
throw "Something went wrong polling for authorisation.";
}
// Let them know we're still waiting
Logger.log("Polling countdown: %s / %s ...", queryTime.toString(),
expiry.toString());
// Sleep until we can make another request
Utilities.sleep(interval * 1000);
queryTime += interval;
}
// Polling time expired without success
// Can't do much without authentication
throw "Polling expired without authorisation.";
}
function refreshAuth(refreshToken)
{
// Refresh auth info with refresh token
// Request refreshed tokens
var payload = {
"refresh_token": refreshToken,
"client_id": config.clientId,
"client_secret": config.clientSecret,
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "refresh_token"
};
var options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': payload
};
var response = UrlFetchApp.fetch(refreshUrl, options);
// Grab the values we're looking for and return them
var newTokens = JSON.parse(response.getContentText());
var authInfo = new Object();
authInfo.accessToken = newTokens.access_token;
authInfo.refreshToken = newTokens.refresh_token;
authInfo.expiry = newTokens.created_at + newTokens.expires_in;
return authInfo;
}
function retrieveAuth()
{
// Retrieve refreshable auth info from user properties store
var userProperties = PropertiesService.getUserProperties();
var authInfo = userProperties.getProperties();
// Check if auth info is there
if (!authInfo.hasOwnProperty("refreshToken") ||
!authInfo.hasOwnProperty("accessToken"))
{
// Fall back to getting fresh auth
Logger.log("No access/refresh token. Running first-run authentication.");
authInfo = getFreshAuth(config);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}
// Check if the auth token has expired yet
var now = Date.now() / 1000;
if (now > authInfo.expiry)
{
// Refresh the auth info
Logger.log("Access token expired. Refreshing authentication...");
authInfo = refreshAuth(authInfo.refreshToken);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}
// Return an object with the details we need for retrieving data
authInfo.clientId = config.clientId;
return authInfo;
}
function resetAuth()
{
// Wipe refreshable auth info from user properties store
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty("refreshToken").deleteProperty("accessToken");
// Get fresh auth
Logger.log("Access/refresh token deleted. Running first-run authentication again.");
var authInfo = getFreshAuth(config);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}
function getFilename(path)
{
// Sanitise path to use as filename
var filename = String(path).replace("\/", "_").split("?")[0];
if (filename.length < 1)
{
filename = config.username;
}
filename += ".json";
return filename;
}
function doListsBackup(lists, authInfo, baseUrl, backupFolder)
{
var listList = [];
// Iterate through the lists and retrieve each one
for (let list of lists)
{
// Retrieve list items
let path = list.ids.trakt;
// Logger.log(path);
let url = baseUrl + path;
// Add list items & comments to other list data, if we can get it.
// There's some weird stuff going on here because some lists just
// refuse to return items/comments, e.g. lists get privated
if (list.item_count > 0)
{
let listItems = getData(authInfo, url + "/items?limit=50", true);
try
{
listItems = JSON.parse(listItems)
}
catch { }
list.items = listItems;
}
if (list.comment_count > 0)
{
let listComments = JSON.parse(
getData(authInfo, url + "/comments?limit=50", true));
list.comments = listComments;
}
// Save the json file in the indicated Google Drive folder
let output = JSON.stringify(list, null, 4);
let filename = list.ids.slug + ".json";
let file = common.updateOrCreateFile(backupFolder, filename, output);
listList.push(filename);
}
if (config.removeMissingLists)
{
// Retrieve files from folder
var fileIter = DriveApp.getFolderById(backupFolder).getFiles();
while (fileIter.hasNext())
{
var file = fileIter.next();
// If this file isn't in the list of lists,
if (!listList.includes(file.getName()))
{
// Move it to the trash
file.setTrashed(true);
}
}
}
}
function backupCore()
{
// Retrieve auth
var authInfo = retrieveAuth();
// Iterate over each of the paths to backup
for (const path of pathList)
{
// Logger.log(path);
var url = apiUrl + path;
var data = getData(authInfo, url, false);
var filename = getFilename(path);
var file = common.updateOrCreateFile(config.backupDir, filename, data);
}
// Iterate over each of the paginated paths to backup
for (const path of paginatedPathList)
{
// Logger.log(path);
var url = apiUrl + path;
var data = getData(authInfo, url, true);
var filename = getFilename(path);
var file = common.updateOrCreateFile(config.backupDir, filename, data);
}
}
function backupLists()
{
// Retrieve auth
var authInfo = retrieveAuth();
// Backup our lists into a folder
var backupFolder = common.findOrCreateFolder(config.backupDir, "lists").getId();
var listsBaseUrl = apiUrl + "lists/";
// Retrieve a list of all the lists
var lists = JSON.parse(getData(authInfo, listsBaseUrl));
doListsBackup(lists, authInfo, listsBaseUrl, backupFolder);
// Backup our liked lists into a folder
backupFolder = common.findOrCreateFolder(config.backupDir, "liked_lists").getId();
// Retrieve a list of all the lists
lists = JSON.parse(getData(authInfo, apiUrl + "likes/lists?limit=250", true));
lists = lists.map((x) => x.list);
listsBaseUrl = baseUrl + "lists/";
doListsBackup(lists, authInfo, listsBaseUrl, backupFolder);
}
function main()
{
// Iterate over each of the paths to backup
backupCore();
// Request all lists separately
backupLists();
}