-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (52 loc) · 1.61 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
const fs = require("fs");
const proc = require("process");
const _ = require("lodash");
const Promise = require("bluebird");
const Crawler = require("./src/Crawler.js");
const Scheduler = require("./src/Scheduler.js");
const Pool = require("./src/Pool.js");
const config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
const ips = proc.argv[2].split(",");
const pool = new Pool(_.map(ips, (ip) => ({ localAddress: ip, family: 6 })));
const crawler = new Crawler({
pool: pool,
mainPath: config.site,
torrentPath: config.site + config.torrentPath,
pagePath: config.site + config.pagePath,
savePagePath: config.pageSavePath,
saveTorrentPath: config.torrentSavePath
});
const status = JSON.parse(fs.readFileSync(config.statusPath));
if (!status.items) {
status.items = [];
}
const sched = new Scheduler({
crawler: crawler,
amountParallel: 100,
}, status);
proc.on("exit", () => {
console.log("Writing last changes!");
fs.writeFileSync(config.statusPath, JSON.stringify(sched.status, null, 4));
});
proc.on('SIGINT', function () {
proc.exit();
});
sched.init()
.then(() => {
console.log("Succesfully init'd");
saveLoop();
sched.tick();
})
.catch((e) => {
console.log("init failed", e);
});
let writeVal = setInterval(() => sched.writeSimpleStatus(), 500);
function saveLoop() {
fs.writeFile(config.statusPath, JSON.stringify(sched.status, null, 4), () => {
if (sched.isDepleted && sched.running.length === 0) {
clearInterval(writeVal);
return;
}
setTimeout(saveLoop, 5000);
});
}