bigly-caret/rank.js

122 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-01-29 19:06:26 -05:00
import { writeFile } from "fs/promises";
2025-01-29 18:54:30 -05:00
// derived from https://git.dervland.net/biglyderv/new-bigly-chat/src/branch/master/docs/stats.php
function rankCalc(result, iterations = 10) {
let matrixe = {}
let fng = {};
2025-01-29 19:06:26 -05:00
let fnc = {};
2025-01-29 18:54:30 -05:00
let frs = {};
let msum_old = 0;
let pr = {};
2025-01-29 19:06:26 -05:00
2025-01-29 18:54:30 -05:00
for (let unn in result) {
matrixe[unn] = {};
2025-01-29 19:06:26 -05:00
matrixe[unn][unn] = 1;
2025-01-29 18:54:30 -05:00
frs[unn] = result[unn].followers;
fng[unn] = result[unn].following;
fnc[unn] = fng[unn].length;
pr[unn] = 1;
}
2025-01-29 19:06:26 -05:00
for (let unn in result) {
2025-01-29 18:54:30 -05:00
let fnu = frs[unn];
for (let follow of fnu) {
if (follow == unn) continue;
let dst = fnc[fnu] || 0;
matrixe[unn][follow] = 1 + 0.1 / (dst + 10);
msum_old += matrixe[unn][follow];
}
for (let unn2 in result) {
if (!matrixe[unn][unn2]) {
matrixe[unn][unn2] = 0;
}
}
}
2025-01-29 19:06:26 -05:00
2025-01-29 18:54:30 -05:00
for (let i = 0; i < iterations; i++) {
let prold = structuredClone(pr);
let matrixf = structuredClone(matrixe);
let msum = 1;
for (let una in result) {
pr[una] = 0;
if (frs[una].length == 0) continue;
for (let unb in result) {
2025-01-29 19:07:09 -05:00
let prb = prold[unb];
2025-01-29 19:06:26 -05:00
if (prb < 1 / 1e4 || Object.keys(fng[unb]).length == 0) {
2025-01-29 18:54:30 -05:00
//msum += matrixe[una][unb];
continue;
}
matrixe[una][unb] = 0.03;
for (let unc in result) {
matrixe[una][unb] += matrixf[una][unc] * matrixf[unc][unb];
}
msum += matrixe[una][unb];
pr[una] += prb * matrixe[una][unb];
}
}
for (let una in result) {
if ((frs[una]).length == 0) continue;
for (let unb in result) {
matrixe[una][unb] *= msum_old / msum;
}
}
2025-01-29 19:07:42 -05:00
let new_sum = Object.values(pr).reduce((a, b) => a + b, 0)
2025-01-29 18:54:30 -05:00
for (let unn in result) {
pr[unn] /= new_sum;
}
2025-01-29 19:06:26 -05:00
console.log(`Iteration ${i} calculated`);
2025-01-29 18:54:30 -05:00
}
return pr;
}
2025-01-29 19:06:26 -05:00
(async function () {
let users = [];
let i = 0;
while (true) {
let h1 = await fetch(`https://api.darflen.com/users/paradock/followers/${i}`)
let j1 = await h1.json();
2025-01-29 18:54:30 -05:00
2025-01-29 19:06:26 -05:00
let users2 = j1.followers.map(x => x.profile.username);
2025-01-29 18:54:30 -05:00
2025-01-29 19:06:26 -05:00
users = [...users, ...users2];
if (users2.length == 0) break;
i++;
2025-01-29 18:54:30 -05:00
}
2025-01-29 19:06:26 -05:00
let data = {};
for (let u of users) {
let h1 = await fetch(`https://api.darflen.com/users/${u}/followers`)
let j1 = await h1.json();
let h2 = await fetch(`https://api.darflen.com/users/${u}/following`)
let j2 = await h2.json();
data[u] = {
followers: j1.followers.map(x => x.profile.username),
following: j2.following.map(x => x.profile.username)
}
console.log(`User ${u} calculated`);
}
let dat = Object.entries(rankCalc(data, 100));
dat = dat.sort((a, b) => a[1] - b[1])
let srz = JSON.stringify(dat);
2025-01-29 19:10:55 -05:00
await writeFile(`./users.json`,srz,'utf8');
2025-01-29 19:06:26 -05:00
})()