gridclose-list/data_fill.js
2025-05-10 23:54:59 -04:00

49 lines
No EOL
1.6 KiB
JavaScript

import { readFile, writeFile } from "fs/promises";
function sorter(a, b) {
let ac = isNaN(a.count * 1) ? -1 : a.count;
let bc = isNaN(b.count * 1) ? -1 : b.count;
if (ac != bc) return bc - ac;
let h = a.type.localeCompare(b.type);
if (h != 0) return h;
return a.name.localeCompare(b.name);
}
(async function () {
let jsoned = JSON.parse(await readFile('list.json', 'utf-8'));
jsoned.sort(sorter);
for (let j of jsoned) {
if (!j.link && j.type.startsWith('scratch')) {
let data = await fetch("https://api.scratch.mit.edu/search/projects?limit=16&offset=0&language=en&mode=popular&q=" + encodeURIComponent(j.name));
let jason = await data.text();
try {
jason = JSON.parse(jason);
} catch (err) {
jason = false;
}
if (jason && jason[0]) {
j.link = `https://scratch.mit.edu/projects/${jason[0].id}`
}
}
if (!j.creator && j.type.startsWith('scratch') && j.link) {
if (j.link.endsWith('/')) {
j.link = j.link.slice(0,-1);
}
let splitted = j.link.split('/');
let data = await fetch("https://api.scratch.mit.edu/projects/" + splitted.pop());
let jason = await data.text();
try {
jason = JSON.parse(jason);
} catch (err) {
jason = false;
}
if (jason && jason.author) {
j.creator = jason.author.username
}
}
}
await writeFile('list.json', JSON.stringify(jsoned, null, "\t"));
})()