tracker-stats/js/script.js
2024-11-03 23:15:08 +01:00

81 lines
No EOL
2.2 KiB
JavaScript

const parser = new DOMParser();
var host = "stats?mode=%";
if (["localhost", "127.0.0.1", "[::]"].includes(location.hostname)) {
host = "sample/%.txt"
}
fetch(host.replace("%", "torr")).then(r => r.text()).then(r => {
document.querySelector("#footer").textContent = r.split("\n")[2];
});
var i = {
"S": "\uf062",
"L": "\uf063",
"P": "\uf1eb",
}
fetch(host.replace("%", "top10")).then(r => r.text()).then(r => {
var lines = r.split("\n");
var current = "";
var data = {}
var hashes = {}
for (const line of lines) {
if (line.trim() == "") continue;
if (!line.startsWith("\t")) {
current = line.slice(line.indexOf("by ") + 3, -1);
data[current] = [];
} else {
var segments = line.trim().split("\t");
var info = {
num: parseInt(segments[0]),
hash: segments[1]
}
hashes[info.hash] = hashes[info.hash] ?? { peers: 0, seeds: 0, leechers: 0 };
hashes[info.hash][current] = hashes[info.hash][current] ?? 0;
hashes[info.hash][current] += info.num;
data[current].push(info);
}
}
var tr = document.createElement("tr");
var th = document.createElement("th");
th.title = "info hash"
th.textContent = "\uf4df";
tr.appendChild(th);
for (const t of Object.keys(hashes[Object.keys(hashes)[0]])) {
var th = document.createElement("th");
th.title = t;
th.textContent = i[t.slice(0, 1).toUpperCase()];
tr.appendChild(th);
}
document.querySelector("#torrents").appendChild(tr);
for (const hash of Object.keys(hashes)) {
var data = hashes[hash];
var tr = document.createElement("tr");
tr.style = "font-family:monospace";
var td = document.createElement("td");
var a = document.createElement("a");
a.href = `magnet:?xt=urn:btih:${hash}&tr=https://${location.hostname}/announce&tr=http://${location.hostname}/announce&tr=udp://${location.hostname}:6969/announce`;
a.textContent = hash;
td.appendChild(a);
tr.appendChild(td);
for (const key of Object.keys(data)) {
var td = document.createElement("td");
td.textContent = data[key];
tr.appendChild(td);
}
document.querySelector("#torrents").appendChild(tr);
}
});