-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawl.mjs
130 lines (121 loc) · 3.67 KB
/
crawl.mjs
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
/*
"axios": "^1.2.3",
"cheerio": "1.0.0-rc.12",
*/
import axios from "axios";
import { AxiosError } from "axios";
import * as cheerio from "cheerio";
import * as fs from "fs";
import { env, argv } from "process";
const ARIA2_API = env?.ARIA2_API
? env.ARIA2_API
: "http://localhost:6800/jsonrpc";
const TARGET_PAGE = env?.TARGET_PAGE
? env.TARGET_PAGE
: "https://www.dbmp4.com/detail/29894.html";
let recordMap = {};
const makeRPC = async (method, params) => {
try {
const resp = await axios.post(
ARIA2_API,
{ jsonrpc: "2.0", id: "CRAWLER", method, params },
{ timeout: 10000, headers: { "Content-Type": "application/json" } }
);
const { request, config, ...others } = resp;
return others;
} catch (err) {
console.log("!ERROR: ", err.message);
throw err;
}
};
(async () => {
try {
const records = JSON.parse(fs.readFileSync("records.json"));
recordMap = records.reduce(
(d, v) => {
return { ...d, [v.name]: v };
},
{ [records[0].name]: records[0] }
);
} catch (error) {
fs.writeFileSync("records.json", "[]");
console.log("!WARN: create records.json");
}
try {
const $ = await axios
.get(TARGET_PAGE, {
timeout: 10000,
headers: { "User-Agent": "Mozilla/5.0 Chrome/109.0.0.0" },
})
.then((resp) => {
if (resp.status === 200) {
return cheerio.load(resp.data);
}
console.log("!ERROR: ", `code ${resp.status}; ${resp.message}`);
throw new AxiosError({ response: resp });
})
.catch((err) => {
console.log("!ERROR: ", `Fetch target page failed; ${err.message}.`);
throw err;
});
const script = $("div.article.content > script").text();
if (!/\|EP\d{2}\|/.test(script)) {
console.log("!ABORT: Something wrong");
return;
}
let to_eval;
eval("to_eval=String" + script.slice(4)); // dean.edwards.name/unpacker/
const { Data } = eval(
`JSON.parse(${to_eval.match(/^var \w+=('.*');?$/)[1]})`
); // replace variable name
let eps = Data[0].downurls;
eps = eps.map((ep) => {
const [name, magnet] = ep.split("$");
return { name, magnet, done: false };
});
const todo = eps.filter(
(ep) =>
!(ep.name in recordMap) || // not recorded
!recordMap[ep.name].done // or not done
);
recordMap = todo.reduce((d, v) => {
return { ...d, [v.name]: v };
}, recordMap);
if (!todo.length) {
console.log("!INFO: Finished crawling with nothing to do");
return;
}
console.log("!INFO: Got results.\nTODO =", todo);
fs.writeFileSync(
"records.json",
JSON.stringify(Object.keys(recordMap).map((k) => recordMap[k]))
);
console.log("!INFO: records.json updated.");
if (process.argv[2] === "--dry-run") {
console.log("!WARN: Dry run, exit.");
return;
}
const resp = await makeRPC("aria2.getGlobalStat");
if (resp.data?.result?.numActive) {
console.log("!INFO: aria2 active:\n", resp.data.result);
todo.forEach((item, i) => {
makeRPC("aria2.addUri", [[item.magnet]]).then((resp) => {
resp.status === 200 && console.log(`!INFO: ${item.name} added.`);
recordMap[item.name].done = true;
});
});
console.log(
"!INFO: Check status:\n",
(await makeRPC("aria2.getGlobalStat")).data.result
);
fs.writeFileSync(
"records.json",
JSON.stringify(Object.keys(recordMap).map((k) => recordMap[k]))
);
console.log("!INFO: records.json updated.");
}
} catch (err) {
// console.log(err);
console.log("!ABORT: ", `Error happened.(${err.code})`);
}
})();