-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.js
64 lines (58 loc) · 1.56 KB
/
extract.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
const fs = require("fs");
const https = require("https");
const crypto = require("crypto");
const outputFolder = "output/";
const URL_REGEXP = /https[^"]+/g;
let files = fs.readdirSync(outputFolder);
let hahaha = 20;
let lenlenlen = 39;
extract(hahaha);
function extract(idx) {
if(idx > hahaha + lenlenlen) return;
let filename = files[idx];
console.log(files);
console.log(filename);
console.log(files[hahaha + lenlenlen]);
//return;
let filepath = outputFolder + filename;
let fileText = fs.readFileSync(filepath, "utf8");
let json = JSON.parse(fileText);
if(!fs.existsSync(outputFolder + json.id)) {
fs.mkdirSync(outputFolder + json.id);
}
let urls = fileText.match(URL_REGEXP);
for(let i = 0; i < urls.length; i++) {
console.log(urls[i]);
}
download(urls, 0, outputFolder + json.id + "/")
.then(() => extract(idx + 1))
.catch(console.log);
}
function download(list, idx, folder) {
console.log("Doing", idx, list[idx]);
return new Promise((resolve, reject) => {
let url = list[idx];
if(!url) {
console.log("Done", idx, list.length);
return resolve(); //done
}
let path = folder + encodeURIComponent(url)
if(fs.existsSync(path)) {
return download(list, idx + 1, folder)
.then(resolve)
.catch(console.log);
}
let file = fs.createWriteStream(path);
let request = https.get(url, (response) => {
console.log("Downloading", url);
let stream = response.pipe(file);
stream.on("finish", () => {
setTimeout(() => {
download(list, idx + 1, folder)
.then(resolve)
.catch(console.log);
}, 500);
});
});
});
}