-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.js
50 lines (42 loc) · 1.13 KB
/
fetch.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
const UserAgent = require("user-agents");
const axios = require("axios");
const { pathExist, getArg, defaultLang, insert } = require("./global");
const lang = getArg("lang") || defaultLang;
const husnKey = Object.keys(require(`./data/husn_${lang}.json`))[0];
const husn = require(`./data/husn_${lang}.json`)[husnKey];
const wget = require("node-wget");
const dest = `./data/${lang}/`;
const audioDest = "./audio/";
const delay = 3000;
pathExist(dest);
pathExist(audioDest);
let limit;
if (getArg("mode") === "prod") {
limit = husn.length - 1;
} else {
limit = 3;
}
let i = 0;
let reqTimer = setInterval(async () => {
if (i <= limit) {
const url = husn[i].TEXT;
const fileName = dest + url.split("/").pop();
await axios
.get(url, {
headers: {
"User-Agent": new UserAgent().toString(),
},
})
.then(({ data }) => {
insert({ fileName, object: data });
wget({ url, timeout: delay, dest: audioDest });
i++;
})
.catch((err) => {
console.log(err);
});
} else {
clearInterval(reqTimer);
console.log("fetching complete");
}
}, delay);