From 58c16d061e3a77698c03233515dfd760e09d1b9f Mon Sep 17 00:00:00 2001 From: Haoyu Xu Date: Sat, 24 Jun 2023 09:18:06 -0400 Subject: [PATCH] feat(aklive2d): add a downloader lib --- .vscode/launch.json | 7 +++++++ libs/charword_table.js | 27 ++------------------------- libs/downloader.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 libs/downloader.js diff --git a/.vscode/launch.json b/.vscode/launch.json index 6859baa..5af9246 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -48,6 +48,13 @@ "request": "launch", "command": "pnpm run vite:directory:dev", "cwd": "${workspaceFolder}" + }, + { + "type": "node-terminal", + "name": "Run Script: charword", + "request": "launch", + "command": "pnpm run charword", + "cwd": "${workspaceFolder}" } ] } diff --git a/libs/charword_table.js b/libs/charword_table.js index 6b274b6..2279823 100644 --- a/libs/charword_table.js +++ b/libs/charword_table.js @@ -1,8 +1,8 @@ /* eslint-disable no-undef */ -import fetch from "node-fetch" import path from "path" import dotenv from "dotenv" import { exists, writeSync, readdirSync, rm, readSync } from "./file.js" +import Downloader from "./downloader.js" dotenv.config() @@ -121,30 +121,7 @@ export default class CharwordTable { } async #download(region) { - const historyResponse = await fetch(`https://api.github.com/repos/Kengxxiao/ArknightsGameData/commits?path=${region}/gamedata/excel/charword_table.json`) - const historyData = await historyResponse.json() - const lastCommit = historyData[0] - const lastCommitDate = new Date(lastCommit.commit.committer.date) - const filepath = path.join(this.#charwordTablePath, `charword_table_${region}_${lastCommitDate.getTime()}.json`) - console.log(`Last commit date: ${lastCommitDate.getTime()}`) - - if (exists(filepath)) { - console.log(`charword_table_${region}.json is the latest version.`) - return JSON.parse(readSync(filepath)) - } - const response = await fetch(`https://raw.githubusercontent.com/Kengxxiao/ArknightsGameData/master/${region}/gamedata/excel/charword_table.json`) - const data = await response.json() - writeSync(JSON.stringify(data), filepath) - console.log(`charword_table_${region}.json is updated.`) - - // remove old file - const files = readdirSync(path.join(__projectRoot, __config.folder.operator, __config.folder.share)) - for (const file of files) { - if (file.startsWith(`charword_table_${region}`) && file !== path.basename(filepath)) { - rm(path.join(__projectRoot, __config.folder.operator, __config.folder.share, file)) - } - } - return data + return await (new Downloader()).github(`https://api.github.com/repos/Kengxxiao/ArknightsGameData/commits?path=${region}/gamedata/excel/charword_table.json`, `https://raw.githubusercontent.com/Kengxxiao/ArknightsGameData/master/${region}/gamedata/excel/charword_table.json`, path.join(this.#charwordTablePath, `charword_table_${region}.json`)) } async #zhCNLoad() { diff --git a/libs/downloader.js b/libs/downloader.js new file mode 100644 index 0000000..32a14de --- /dev/null +++ b/libs/downloader.js @@ -0,0 +1,36 @@ +/* eslint-disable no-undef */ +import fetch from "node-fetch" +import path from "path" +import { exists, writeSync, readdirSync, rm, readSync } from "./file.js" + +export default class Downloader { + + async github(history_url, raw_url, filepath) { + const historyResponse = await fetch(history_url) + const historyData = await historyResponse.json() + const lastCommit = historyData[0] + const lastCommitDate = new Date(lastCommit.commit.committer.date) + console.log(`Last commit date: ${lastCommitDate.getTime()}`) + const basename = path.basename(filepath) + const ext = path.extname(filepath) + filepath = path.join(filepath, "..", `${basename}_${lastCommitDate.getTime()}${ext}`) + + if (exists(filepath)) { + console.log(`${basename} is the latest version.`) + return JSON.parse(readSync(filepath)) + } + const response = await fetch(raw_url) + const data = await response.json() + writeSync(JSON.stringify(data), filepath) + console.log(`${basename} is updated.`) + + // remove old file + const files = readdirSync(path.join(__projectRoot, __config.folder.operator, __config.folder.share)) + for (const file of files) { + if (file.startsWith(basename) && file !== path.basename(filepath)) { + rm(path.join(__projectRoot, __config.folder.operator, __config.folder.share, file)) + } + } + return data + } +} \ No newline at end of file