-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (83 loc) · 2.56 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const fs = require('fs');
const nodeurl = require('url');
const path = require('path');
const axios = require('axios');
const cheerio = require('cheerio');
const ProgressBar = require('progress');
const readline = require('readline-sync');
const timestamp = Date.now();
let folder = '';
async function start() {
const indexUrl = readline.question('URL: ');
if (!indexUrl) {
console.log('No url provided, exiting...');
process.exit(1);
}
let urls = await collectLinks(indexUrl);
for ([index, url] of urls.entries()) {
console.log('---------------------');
console.log(`Downloading # ${index}/${urls.length}`);
await download(indexUrl, url);
}
console.log('********** Downloads Completed! **********');
}
async function collectLinks(url) {
let urls = [];
const invalidUrls = [
'?C=N;O=D',
'?C=M;O=A',
'?C=S;O=A',
'?C=D;O=A',
'?C=N;O=A',
'/wp-content/',
'/',
'../',
' ',
''
];
const { data } = await axios({
url,
method: 'get',
responseType: 'document'
});
let $ = cheerio.load(data);
$('a').each(function(i, element) {
let ref = $(element).attr('href');
if (invalidUrls.indexOf(ref) === -1 && !ref.includes('/wp-content/uploads')) {
urls.push($(element).attr('href'));
}
});
let info = nodeurl.parse(url, true);
fs.mkdirSync(`./${info.host}-${timestamp.toString()}`);
folder = `./${info.host}-${timestamp.toString()}`;
return urls;
}
async function download(host, url) {
return new Promise(async function(resolve, reject) {
console.log(host, url);
console.log('Connecting …');
const { data, headers } = await axios({
url: host + url,
method: 'get',
responseType: 'stream'
});
const totalLength = headers['content-length'];
const progressBar = new ProgressBar('-> downloading [:bar] :percent :etas', {
width: 40,
complete: '=',
incomplete: ' ',
renderThrottle: 1,
total: +totalLength || 100
});
const writer = fs.createWriteStream(path.resolve(__dirname, folder.toString(), url));
data.on('data', function(chunk) {
progressBar.tick(chunk.length);
if (progressBar.complete) {
console.log('** Completed **');
resolve(true);
}
});
data.pipe(writer);
});
}
start();