-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·84 lines (71 loc) · 2.36 KB
/
index.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
#!/usr/bin/env node
const phantom = require('phantom');
const argparse = require('argparse');
const fs = require('fs');
// Parse URL from CLI arguments
const ArgumentParser = argparse.ArgumentParser;
const parser = new ArgumentParser({
addHelp: true,
description: 'Scrapes image preview URLs from Google Photo albums.',
version: '1.0.0'
});
parser.addArgument('url', { help: 'Google Photos album URL' });
parser.addArgument(['-w', '--write'], {
action: 'storeTrue',
help: 'Write photo URLs to file'
});
parser.addArgument(['-f', '--file'], {
defaultValue: 'photos.js',
help: 'Name of file to create'
});
parser.addArgument(['-mw', '--maxWidth'], {
help: 'Max width of each photo in pixels'
});
parser.addArgument(['-mh', '--maxHeight'], {
help: 'Max height of each photo in pixels'
});
const { url, write, file, maxWidth, maxHeight } = parser.parseArgs();
// Intercept image preview URLs from album's network calls using PhantomJS
(async () => {
// Instantiate PhantomJS page
const instance = await phantom.create();
const page = await instance.createPage();
await page.property('viewportSize', { width: 1024, height: 100000 });
// Save requested URLs
let requestedUrls = [];
await page.on('onResourceRequested', requestData => {
requestedUrls.push(requestData.url);
});
// Open Google Photos album
await page.open(url);
await page.property('content');
// Filter URLs to include only photos
const photos = requestedUrls
.filter(requestedUrl => {
return requestedUrl.includes('https://lh3.');
})
.map(photo => {
const end = photo.lastIndexOf('=w');
return photo.substring(0, end);
});
// Handle output
let content = '';
photos.forEach((photo, index) => {
content += ' ' + '\'' + photo + (maxWidth != null || maxHeight != null ? '=' : '' ) + ( maxWidth != null ? ('w' + maxWidth) : '' ) + (maxWidth != null && maxHeight != null ? '-' : '' ) + ( maxHeight != null ? ('h' + maxHeight) : '' ) + '\'';
content += index === photos.length - 1 ? '\n' : ',\n';
});
if (write) {
content = 'module.exports = [\n' + content;
content += '];\n';
fs.writeFile(file, content, err => {
if (err) {
console.log(err);
} else {
console.log (`Created file: "${file}"!`);
}
});
} else {
console.log(content);
}
await instance.exit();
})();