Skip to content

Commit

Permalink
WIP: monster data download script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Sep 7, 2024
1 parent bbc66bc commit c992763
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"pre-commit": "pnpm nano-staged"
},
"devDependencies": {
"@types/node": "20",
"eslint": "^8.57.0",
"eslint-config-foxkit": "4.1.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -35,6 +36,8 @@
"typescript": "5.5.4"
},
"dependencies": {
"@foxkit/list": "^1.2.0",
"@foxkit/node-util": "^0.6.0",
"tsx": "^4.19.0"
}
}
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions src/download-monster-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from "fs/promises";
import { isDirectory, isFile, writeFile } from "@foxkit/node-util/fs";
import { sleep } from "./utils/sleep";
import { List } from "@foxkit/list";

const [, , ...args] = process.argv;
const isForced = args.includes("--force");

async function setupDirs() {
if (!(await isDirectory("data"))) {
await fs.mkdir("data");
}

if (!(await isDirectory("data/monsters"))) {
await fs.mkdir("data/monsters");
}
}

async function fetchMonsterList(): Promise<Array<number>> {
const res = await fetch("https://api.flyff.com/monster");
if (!res.ok) {
throw new Error(`Failed to fetch Monster list: ${res.statusText}`);
}

await sleep(200);

return res.json();
}

async function fetchMonsterData(id: number) {
const res = await fetch(`https://api.flyff.com/monster/${id}`);
if (!res.ok) {
throw new Error(
`Failed to fetch Monster data for id ${id}: ${res.statusText}`
);
}

await sleep(220);

return res.text();
}

async function main() {
if (isForced) {
console.log("Forceing redownloading of all data");
}

await setupDirs();

console.log("Downloading Monster List");
const list = await fetchMonsterList();
console.log(`Found ${list.length} Monster IDs`);

const queue = new List(list);
let id: number | undefined;
while ((id = queue.shift())) {
if (!isForced && (await isFile(`data/monsters/${id}.json`))) {
console.log(`[SKIP] Already have data for id ${id}`);
continue;
}

console.log(`[LOG] Fetching data for id ${id}`);
const data = await fetchMonsterData(id);
await writeFile(`data/monsters/${id}.json`, data);
console.log(`[DONE] completed download of data for id ${id}`);
}

// TODO: fetching skills
// TODO: global constants for paths such as data/monsters/*.json
}

main()
.then(() => console.log("Completed"))
.catch(e => {
console.error(e);
process.exit(1);
});
3 changes: 3 additions & 0 deletions src/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sleep(length: number) {
return new Promise<void>(resolve => setTimeout(() => resolve(), length));
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"strict": true,
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true
"skipLibCheck": true,
"allowSyntheticDefaultImports": true
},
"include": ["**/*.ts"],
"include": ["**/*.ts", "src/types/**/*.d.ts"],
"exclude": ["node_modules", "dist"]
}

0 comments on commit c992763

Please sign in to comment.