bigly-caret/index.js
2025-02-12 00:59:27 -05:00

89 lines
No EOL
2.3 KiB
JavaScript

import { main, rankCalc } from "./site.js";
import e from "express";
let {
site = 'darflen',
route = './test.txt',
pageLimit = 0.1,
blackList = '',
greyList = '',
discardThreshold = 1,
delay = 100,
depth = 1,
isRelative = false,
fetchRate = 15,
user = 'paradock',
matrixIterations = 3,
useArchive = true,
isGpu = false,
mode = 'cli',
port = 6952,
arrayMax = 800
} = process.env;
let settings = {
site, route, pageLimit, blackList, greyList, discardThreshold, delay,
depth, isRelative, fetchRate, user, matrixIterations, useArchive, isGpu,
arrayMax
};
let cache = {};
let cacheDate = 0;
if (mode == 'cli') {
main(settings);
} else if (mode == 'site') {
const app = e();
app.get('/top', async (req, res) => {
if ((new Date) - cacheDate < 1000*1) {
res.send(cache);
return;
}
let json = await fetch("https://nbg.dervland.net/api/followjson.php");
try {
json = await json.json();
} catch (err) {
json = {};
res.send(JSON.stringify({}));
return;
}
let { ref } = req.query;
let data = {};
for (let entryI in json) {
let entry = json[entryI];
let user = data[entry.username] || {};
let target = data[entry.target] || {};
let f1 = user.following = user.following || [];
let f2 = user.followers = user.followers || [];
let f3 = target.following = target.following || [];
let f4 = target.followers = target.followers || [];
f3.push(entry.username);
f2.push(entry.target);
data[entry.username] = user;
data[entry.target] = target;
}
let calcedRank = rankCalc(data, matrixIterations, ref ? [ref] : [], false, settings.isGpu, settings.arrayMax);
let dat = Object.entries(calcedRank);
dat = dat.sort((a, b) => b[1] - a[1]);
console.log(`Graph is calculated with ${dat.length} entries`);
let dat2 = {};
for (let d of dat) {
dat2[d[0]] = d[1] * 100 + "%";
}
cache = JSON.stringify(dat2);
cacheDate = +(new Date);
res.send(cache)
})
app.listen(port);
console.log(`App listening on port ${port}`)
}