-
Notifications
You must be signed in to change notification settings - Fork 8
/
ustclug-v1.js
106 lines (99 loc) · 2.93 KB
/
ustclug-v1.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
105
106
const cname = require("./utils").cname;
const size = function (bytes) {
const mib = bytes / 1024 / 1024;
if (mib < 1024) {
return mib.toFixed(2) + " MiB";
}
const gib = mib / 1024;
if (gib < 1024) {
return gib.toFixed(2) + " GiB";
}
const tib = gib / 1024;
return tib.toFixed(2) + " TiB";
};
module.exports = async function (homepageURL, yukiURL) {
const name_func = await cname();
const homepageHTML = await (await fetch(homepageURL)).text();
// yukiMeta = await (await fetch(yukiURL)).json();
let yukiMeta = await fetch(yukiURL);
if (yukiMeta.ok) {
yukiMeta = await yukiMeta.json();
} else {
yukiMeta = null;
}
// finding `var isoinfo`
const htmlLines = homepageHTML.split("\n");
const isoSign = "var isoinfo = ";
let isoinfo = [];
for (let line of htmlLines) {
let pos = line.indexOf(isoSign);
if (pos !== -1) {
pos += isoSign.length;
isoinfo = JSON.parse(line.substring(pos, line.length - 1));
}
}
for (let item of isoinfo) {
item["distro"] = name_func(item["distro"]);
item["category"] = "os";
}
const parser = new DOMParser();
const doc = parser.parseFromString(homepageHTML, "text/html");
const items = Array.from(doc.querySelectorAll(".filelist tbody tr"));
// handling HTML
const hashtable = {};
const mirrors = items.map((item, index) => {
let url = item.querySelector(".filename a").getAttribute("href");
if (!url.startsWith("http:") && !url.startsWith("https:")) {
url = "/" + url;
}
const status = "U";
const help = item.querySelector(".help a").getAttribute("href");
const name = name_func(url.replace(/\//g, "")); // remove all '/' in href
const desc = ""; // no desc in ustclug-v1
hashtable[name.toLowerCase()] = index;
return {
cname: name,
url,
status,
help,
desc,
};
});
// handling yuki
if (yukiMeta) {
yukiMeta.map((item) => {
const name = name_func(item.name).toLowerCase();
const index = hashtable[name] ?? hashtable[name.split(".")[0]];
if (index === undefined) {
return;
}
const next_run = item.nextRun;
const last_success = item.lastSuccess;
const prev_run = item.prevRun;
if (next_run < 0) {
mirrors[index]["status"] = "P";
} else if (item.syncing) {
mirrors[index]["status"] = "Y" + prev_run;
if (last_success) {
mirrors[index]["status"] += "O" + last_success;
}
} else if (item.exitCode == 0) {
mirrors[index]["status"] = "S" + last_success;
} else {
mirrors[index]["status"] = "F" + prev_run;
if (last_success) {
mirrors[index]["status"] += "O" + last_success;
}
}
if (next_run > 0) {
mirrors[index]["status"] += "X" + next_run;
}
mirrors[index]["size"] = size(item.size);
mirrors[index]["upstream"] = item.upstream;
});
}
return {
info: isoinfo,
mirrors,
};
};