From 74b946dc696bab02065baf585c98d8127fd18b0a Mon Sep 17 00:00:00 2001 From: thekingofcity <3353040+thekingofcity@users.noreply.github.com> Date: Fri, 19 Jul 2024 14:57:34 +0800 Subject: [PATCH] #1603 Only get missing logins --- scripts/loginbot.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/scripts/loginbot.js b/scripts/loginbot.js index e08b91c2..9a3e1346 100644 --- a/scripts/loginbot.js +++ b/scripts/loginbot.js @@ -3,7 +3,7 @@ import { resolve } from 'path'; const sleep = ms => new Promise(r => setTimeout(r, ms)); -const getLoginsByType = async type => { +const getLoginsByType = async (type, cachedLogins) => { // const data = JSON.parse(await readFile(`${type}.json`, 'utf-8')); const data = JSON.parse(await readFile(resolve('..', 'public', 'resources', `${type}.json`), 'utf-8')); const ids = new Set( @@ -11,8 +11,12 @@ const getLoginsByType = async type => { .map(_ => _.contributors) .flat() ); - const ret = {}; + const ret = cachedLogins[type]; + type = type === 'real_world' ? 'realWorld' : type; // real_world -> realWorld for (const id of ids) { + if (id in cachedLogins[type]) { + continue; + } const rep = await (await fetch(`https://api.github.com/user/${id}`)).json(); ret[id] = rep.login; console.log(`login for id: ${id} is ${ret[id]}`); @@ -21,18 +25,18 @@ const getLoginsByType = async type => { return ret; }; -export const getLogins = async () => { +export const makeLogins = async () => { + const loginPath = resolve('..', 'public', 'resources', 'logins.json'); + const cachedLogins = JSON.parse(await readFile(loginPath, 'utf-8')); + const logins = { - realWorld: await getLoginsByType('real_world'), - fantasy: await getLoginsByType('fantasy'), + realWorld: await getLoginsByType('real_world', cachedLogins), + fantasy: await getLoginsByType('fantasy', cachedLogins), }; - // await writeFile('logins.json', JSON.stringify(logins, null, 4), { - // encoding: 'utf-8', - // }); - await writeFile(resolve('..', 'public', 'resources', 'logins.json'), JSON.stringify(logins, null, 4), { + await writeFile(loginPath, JSON.stringify(logins, null, 4), { encoding: 'utf-8', }); }; -await getLogins(); +await makeLogins();