improve JSON data, restructure JSON, and add util scripts
This commit is contained in:
parent
483a51f854
commit
c427ddec81
4 changed files with 320 additions and 193 deletions
50
data_fill.js
Normal file
50
data_fill.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
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;
|
||||
}
|
||||
console.log(jason)
|
||||
if (jason && jason.author) {
|
||||
j.creator = jason.author.username
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await writeFile('list.json', JSON.stringify(jsoned, null, "\t"));
|
||||
})()
|
Loading…
Add table
Add a link
Reference in a new issue