-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetScripts.js
More file actions
executable file
·138 lines (103 loc) · 4.24 KB
/
getScripts.js
File metadata and controls
executable file
·138 lines (103 loc) · 4.24 KB
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
#!/usr/bin/env node
const fs = require('fs');
const https = require('https');
const puppeteer = require('puppeteer');
//Execute script using node getScripts.js {site url} {fileNameSearchTerm}
//EG: node getScripts.js https://imgur.com css
/* ============================================================
Native Recursive Directory Function
============================================================ */
const path = require('path');
function mkDirByPathSync(targetDir, {
isRelativeToScript = false
} = {}) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : '';
const baseDir = isRelativeToScript ? __dirname : '.';
return targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code === 'EEXIST') { // curDir already exists!
return curDir;
}
// To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows.
if (err.code === 'ENOENT') { // Throw the original parentDir error on curDir `ENOENT` failure.
throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`);
}
const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1;
if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) {
throw err; // Throw if it's just the last created dir.
}
}
return curDir;
}, initDir);
}
/* ============================================================
Promise-Based Download Function
============================================================ */
const download = (url, destination, fileNameSearchTerm, hostname) => new Promise((resolve, reject) => {
mkDirByPathSync(`./files/${hostname}/${fileNameSearchTerm}`);
destination = `./files/${hostname}/${fileNameSearchTerm}/${destination}`
const file = fs.createWriteStream(destination);
https.get(url, response => {
response.pipe(file);
file.on('finish', () => {
file.close(resolve(true));
});
}).on('error', error => {
fs.unlink(destination);
reject(error.message);
});
});
/* ============================================================
Get Files
============================================================ */
function main() {
let url = process.argv[2];
let fileNameSearchTerm = process.argv[3];
let showRejects = process.argv[4];
if (url == null) {
console.log("Please enter a url as the first parameter. e.g. https://imgur.com");
return;
}
if (fileNameSearchTerm == null) {
console.log("Please enter a file Name Search Term as the 2nd parameter. e.g. css, js, bootstrap or jquery");
return;
}
if (showRejects == null) {
console.log("To show all requests in the console enter 1 as the last parameter in the request");
}
console.log(`Searching: ${url}`);
(async () => {
const browser = await puppeteer.launch('headless: "new"');
const page = await browser.newPage();
// Emitted when the page produces a request
console.group(`Files containing the search term: ${fileNameSearchTerm} were requested:`);
page.on('request', request => {
let requestUrl = request.url();
if (!requestUrl.includes(fileNameSearchTerm)) {
if (showRejects == 1) {
console.info("Non " + fileNameSearchTerm + "file found but NOT requested at " + requestUrl)
}
} else if (requestUrl.includes(fileNameSearchTerm) && !requestUrl.includes("base64")) {
let filename = requestUrl.substring(requestUrl.lastIndexOf('/') + 1);
let {
hostname
} = new URL(url);
console.info("Filename " + requestUrl + " Downloaded to " + "./files/" + hostname + "/" + "/" + filename + "." + fileNameSearchTerm)
download(requestUrl, filename, fileNameSearchTerm, hostname);
} else console.info("Something went wrong")
});
console.groupEnd();
// Emitted after the page is closed
page.once('close', () => console.info('Page is closed'));
await page.goto(url);
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.24601')
//const cookies = await page.cookies()
//console.log(cookies)
await browser.close();
})();
}
main();