52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { NumistaType } from '@/types/numista';
|
|
import { asyncSpawn } from './async-spawn';
|
|
import { fetchTextures } from './fetch-textures';
|
|
import { db } from './db';
|
|
import { handleInsertType, handleUpdateTypeCount } from './insert';
|
|
import { readFile, rm } from 'fs/promises';
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
let [id, count] = process.argv.slice(2, 4).map(value => Number(value));
|
|
|
|
if (!id) {
|
|
throw new Error('Invalid id ' + id);
|
|
}
|
|
|
|
if (!count) {
|
|
count = 1;
|
|
}
|
|
|
|
const query = db.query('SELECT * FROM NumistaType WHERE id = ' + id + ';');
|
|
let type = query.get() as NumistaType | null;
|
|
|
|
if (type) {
|
|
console.log(type.title);
|
|
type.count = (type.count ?? 1) + count;
|
|
handleUpdateTypeCount(type);
|
|
} else {
|
|
await asyncSpawn('sh', [
|
|
'-c',
|
|
`curl https://api.numista.com/api/v3/types/${id} -H Numista-API-Key:egr10utzWmYztFrZhJSRGF7Tv64RA3b2S4xdz4di | jq '.count='${count} > tmp-${id}.json`
|
|
]);
|
|
|
|
type = JSON.parse(await readFile(`tmp-${id}.json`, { encoding: 'utf-8' }));
|
|
|
|
if (!type) {
|
|
throw new Error('Invalid response for id ' + id);
|
|
}
|
|
|
|
console.log(type.title);
|
|
|
|
await rm(`tmp-${id}.json`);
|
|
|
|
type = await fetchTextures(type);
|
|
|
|
if (!type) {
|
|
throw new Error('Error processing textures for id ' + id);
|
|
}
|
|
|
|
handleInsertType(type);
|
|
}
|
|
|
|
db.close();
|