forked from medialab/drive-out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive-api.js
475 lines (366 loc) · 13.1 KB
/
drive-api.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
'use-strict';
var extend = require('util')._extend,
fs = require('fs'),
Promise = require('promise'),
colors = require('cli-color'),
request = require('request'),
request_sync = require('request-sync'),
google = require('googleapis'),
cheerio = require('cheerio'),
css = require('css'),
settings, // cfr module.exports function
secrets, // the content of SECRETS_PATH .json file
drive = {},
google_drive = {};
/*
Some utils.
===
*/
drive.utils = {};
drive.utils.slugify = function(text) {
return text.toString().toLowerCase()
.replace(/[\s_]+/g, '-') // Replace spaces and underscore with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
};
/*
function utils.getFileId
---
get drive id from a google drive share link,
e.g "https://drive.google.com/a/sciencespo.fr/folderview?id=0ByZTyEnzm9qqdVkwUWtqa2k1MFU&usp=sharing".match(/ids=(.*?)&/)[1]
*/
drive.utils.getFileId = function(url) {
var fileId;
try {
fileId = url.match(/id=(.*?)&/)[1]
} catch(err) {
console.log(err);
throw 'drive.utils.getFileId failed. Unable to find fileId form the given url: ' + url
}
return fileId
};
/*
function utils.write
---
fs write a file in sync
*/
drive.utils.write = function(filepath, contents) {
console.log(colors.white('writing file'), colors.inverse(filepath));
var fd = fs.openSync(filepath, 'w');
fs.writeSync(fd, contents);
fs.closeSync(fd);
};
/*
Clean html from sapn and style attributes. clean title and subtitle.
@return cleaned text
*/
drive.utils.clean = function(html){
if(!html) return html;
var c = html
.replace(/<sup(.*?)href="#cmnt(.*?)<\/sup>/g, '')
.replace(/<div(.*?)href="#cmnt(.*?)<\/div>/g, '') // avoid comment in text
.replace(/<span(.*?)>/g,'')
.replace(/<\/span(.*?)>/g,'')
.replace(/name="(.*?)"/g,'')
.replace(/style="(.*?)"/g,'')
.replace(/class="(.*?)"/g,'')
.replace(/<table(.*?)>/g, function(d, attrs){ return '<table class="table" ' + attrs + '>';})
.replace(/<a><\/a>/g,'')
.replace(/<p\s+><\/p>/g,'')
.replace(/<p\s+>/g,'<p>');
return c;
}
/*
Return a html string well cleaned with cheerio and css
*/
drive.utils.parse = function(html) {
var $ = cheerio.load(html),
style,
bolds,
italics;
// get css rules from the tag style inside the html
style = css.parse($('style').text());
//get rules matching any span or p element having BOLD
bolds = style.stylesheet.rules.filter(function(rule) {
if(!rule || !rule.selectors)
return false;
var rule_for_minor_selectors = rule.selectors.some(function(selector) {
return !selector.match(/h(\d+)/)
});
if(rule_for_minor_selectors)
return rule.declarations.some(function(d){
return d.property == 'font-weight' && (d.value == 'bold' || +d.value > 300);
});
else
return false;
});
// transform bold into strong element
for(var i =0; i<bolds.length; i++) {
bolds[i].selectors.forEach(function(d) {
$(d).replaceWith('<strong>' + $(d).html() + '</strong>')//console.log('apdjapodjaopdjaposdjpiajd', d)
})
};
// transform stuffs
return $;
};
/*
function start
---
Core function, return a promise.
It handle token refreshing and write secrets received in th fiel specified via settings
*/
drive.start = function() {
console.log(" __ _ __ \n ____/ /____(_) _____ ____ __ __/ /_\n / __ / ___/ / | / / _ \\______/ __ \\/ / / / __/\n/ /_/ / / / /| |/ / __/_____/ /_/ / /_/ / /_ \n\\__,_/_/ /_/ |___/\\___/ \\____/\\__,_/\\__/ \n");
return new Promise(function (resolve, reject) {
var oauth2Client = new google.auth.OAuth2(settings.CLIENT_ID, settings.CLIENT_SECRET, settings.REDIRECT_URL);
var flush = function() {
secrets = require(settings.SECRETS_PATH);
oauth2Client.setCredentials(secrets);
var expiry_date = new Date(secrets.expiry_date);
console.log('date this token will expire:', colors.inverse(new Date(secrets.expiry_date)));
if((new Date()).getTime() - expiry_date.getTime() > 60000) { // that is10 min to expiry
console.log(colors.bold('token expired...'));
return new Promise(function (_resolve, _reject) { // return a return ... mah
oauth2Client.refreshAccessToken(function(err, tokens) {
if(err)
return reject(err);
drive.utils.write(settings.SECRETS_PATH, JSON.stringify(tokens));
console.log('date this *new* token will expire:', colors.inverse(new Date(secrets.expiry_date)));
secrets = tokens;
console.log('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n');
return resolve();
});
})
} else {
console.log('\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n');
return resolve();
}
};
if(fs.existsSync(settings.SECRETS_PATH)){
console.log('secrets file found in ', colors.inverse(settings.SECRETS_PATH));
return flush();
};
var readline = require('readline'),
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}),
url = oauth2Client.generateAuthUrl({
access_type: 'offline', // will return a refresh token
scope: settings.SCOPES // can be a space-delimited string or an array of scopes
});
console.log('Please visit the', colors.bold('following url'), 'and authenticate with your', colors.cyan('google drive'),'credentials: ');
console.log(url);
rl.question('Enter the code here:', function(code) {
console.log('Thanks!\nCode:',colors.green(code));
oauth2Client.getToken(code, function(err, tokens) {
if(err)
return reject(err)
rl.close()
console.log('gettin\' token from code', tokens);
drive.utils.write(settings.SECRETS_PATH, JSON.stringify(tokens));
google_drive = google.drive({ version: 'v2', auth: oauth2Client });
return flush();
});
});
});
};
/*
Response handler for googleapi calls
===
*/
drive.response = function(res) {
console.log(colors.white(' status'), res.statusCode==200? colors.greenBright(res.statusCode): colors.red(res.statusCode));
var contents = JSON.parse(res.body);
if(res.statusCode!= 200) {
console.log(' error ', colors.red(contents.error.message));
throw 'googleapi response does not allow to proceed...';
};
return contents;
}
drive.iterators = {};
drive.iterators.basic = function(file) {
console.log('drive.iterators.basic', file.title)
return {
id: file.id,
title: file.title,
mimeType: file.mimeType
};
};
/*
This special iterator download the content of googleDdocuments
*/
drive.iterators.flatten = function(file, options, results) {
console.log('drive.iterators.flatten', file.title, file.id);
var result = {
id: file.id,
title: file.title,
slug: drive.utils.slugify(file.title),
mimeType: file.mimeType
};
if(file.mimeType == 'application/vnd.google-apps.document') {
var html = drive.files.getHtml({fileId:file.id}),
body = html.match(/<body[^>]*>(.*?)<\/body>/i)[1],
$ = cheerio.load(body);
result.title = $('.title').html() || file.title;
result.subtitle = $('.subtitle').html();
// clean first title and first subtitle
$('.title').first().text('');
$('.subtitle').first().text('')
result.html = drive.utils.clean($.html());
result.type = 'document';
}
if(file.mimeType == 'image/jpeg' || file.mimeType == 'image/png') {
drive.files.download({
downloadUrl: file.downloadUrl,
filepath: options.mediapath + '/' + file.id + '.' + file.fileExtension
})
result.type = 'image';
result.src = file.id + '.' + file.fileExtension;
result.bounds = file.imageMediaMetadata // : { width: 930, height: 561 } }
}
if(result.slug.indexOf('-metadata')!= -1) {
result.type = "metadata";
result.target = result.slug.replace('-metadata', '');
}
if(file.mimeType == 'application/vnd.google-apps.folder') {
result.type = 'folder';
result.items = drive.files.walk(extend(options,{fileId: file.id}), drive.iterators.flatten);
}
return result;
};
/*
Pseudo-api modules (sync, not async!) meant for drivein specific usage. They do not require Oauth, but the google folder should be publibly available
===
*/
drive.open = {};
drive.open.files = {};
drive.open.files.get = function(options) {
};
/*
get html contents
*/
drive.open.files.getHtml = function(options) {
};
drive.open.files.list = function(options) {
};
/*
Api modules (sync, not async!)
===
*/
drive.files = {}
drive.files.get = function(options) {
if(!options || !options.fileId)
throw 'files.list interrupted: please specify a "fileId" field ...';
console.log(colors.white('files.get'), colors.greenBright(options.fileId));
var res = request_sync({
url: 'https://www.googleapis.com/drive/v2/files/' + options.fileId,
headers: {
'Authorization' : 'Bearer ' + secrets.access_token
}
});
return drive.response(res);
};
drive.files.list = function(options) {
if(!options || !options.fileId)
throw 'files.list interrupted: please specify a "fileId" field ...';
console.log(colors.white('files.list'), colors.greenBright(options.fileId));
var res = request_sync({
url: 'https://www.googleapis.com/drive/v2/files',
qs:{
q: '"'+options.fileId + '" in parents'
},
headers: {
'Authorization' : 'Bearer ' + secrets.access_token
}
});
return drive.response(res);
};
drive.files.getHtml = function(options) {
if(!options || !options.fileId)
throw 'files.getHtml interrupted: please specify a "fileId" field ...';
var f = options.format || 'html'; // format can either be html or txt
console.log(' ',colors.cyan('get '+f), 'of', options.fileId);
var response = request_sync({
url: 'https://docs.google.com/feeds/download/documents/export/Export?id='+options.fileId+'&exportFormat=' + f,
method: 'GET',
headers: {
'Authorization' : 'Bearer ' + secrets.access_token
}
});
return response.body;
}
/*
Download asyncrh the url specified in options.url to options.filepath.
Use only with downloadUrl fields.
@return Promise
*/
drive.files.download =function(options) {
if(!options || !options.downloadUrl || !options.filepath)
throw 'files.download interrupted: please specify a "fileId" field AND a "filepath" field...';
console.log(' ',colors.cyan('download'), 'to', options.filepath);
var ws = fs.createWriteStream(options.filepath);
ws.on('error', function(err) {
console.log(err);
console.log(colors.red('cannot write at options.filepath'), colors.inverse(options.filepath));
throw 'error on writing file';
});
request({
url: options.downloadUrl,
headers: {
'Authorization' : 'Bearer ' + secrets.access_token
}
}).pipe(ws) // save image
};
/*
Recursively look for items in files.list. Then apply iterator function on each item.
@param fileId - google drive folder fileId
@param iterator - transform the object, e-g according to the type. There is a short lint under drive.iterators
*/
drive.files.walk = function(options, iterator){
if(!options || !options.fileId)
throw 'drive.files.walk interrupted: please specify a "options.fileId" field ...';
if(typeof iterator != 'function')
iterator = drive.iterators.basic;
console.log('drive.files.walk ...');
var files = drive.files.list({fileId: options.fileId}),
results = [];
if(!files.items)
return results;
// sort files here by function, @todo
files.items.sort(function(a, b) {
if (a.title > b.title)
return 1;
if (a.title < b.title)
return -1;
// a doit être égale à b
return 0;
})
for(var i=0; i<files.items.length; i++) {
var file = iterator(files.items[i], options, results, files.items); // if iterator function return null or undefined
if(file)
results.push(file);
};
return results;
}
/*
Main export module.
===
*/
module.exports = function(options) {
settings = extend({
CLIENT_ID: '',
CLIENT_SECRET: '',
REDIRECT_URL: '',
YQL_URL: 'https://query.yahooapis.com/v1/public/yql',
DRIVE_FOLDERVIEW_URL: 'https://drive.google.com/folderview?id=',
SECRETS_PATH: false,
SCOPES: 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.metadata.readonly'
}, options);
// check secrets path
if(!settings.SECRETS_PATH)
throw 'settings.SECRETS_PATH must be specified with a valid path. Plase note that Driveout will override that file!'
return drive;
};