1
0
Files
jecnarozvrh/scrape/parse.ts
T
jzitnik 8fe2bd3bf2
Remote Deploy / deploy (push) Successful in 1m28s
refactor: Some minor refinements
2026-06-05 18:24:54 +02:00

64 lines
1.8 KiB
TypeScript

/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import ExcelJS from "exceljs"
import generateArchivedV1_V2 from "./parse/archived/v1_v2.js";
import parseV3 from "./parse/v3.js";
import fs from "fs/promises";
import { fileURLToPath } from "url";
type Save = {
filePath: string | string[],
parser: (workbook: ExcelJS.Workbook, downloadedFilePath: string) => Promise<any>
}
async function doAll(saves: Save[], workbook: ExcelJS.Workbook, downloadedFilePath: string) {
const promises = saves.map(save => {
return (async function() {
const res = await save.parser(workbook, downloadedFilePath);
if (typeof save.filePath === "string") {
save.filePath = [save.filePath];
}
await Promise.all(
save.filePath.map(filePath => fs.writeFile(filePath, JSON.stringify(res)))
);
})()
})
await Promise.all(promises);
}
export default async function parseAll(downloadedFilePath: string) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(downloadedFilePath);
await doAll([
{
filePath: ["volume/db/v1.json", "volume/db/v2.json"],
parser: generateArchivedV1_V2,
},
{
filePath: "volume/db/v3.json",
parser: parseV3
},
], workbook, downloadedFilePath);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
await parseAll("volume/db/current.xlsx");
}