mkmrare/cmd/buy.ts

42 lines
1.2 KiB
TypeScript

import * as ejs from "ejs";
import { existsSync } from "fs";
import { Article, asyncLoadJSON, CardMarketApi, MCMDB } from "../lib";
import { cubeCards, genCube } from "../lib/cube";
async function run() {
if (process.argv.length < 3) {
console.error("Usage: yarn buy <uid>");
process.exit(1);
return;
}
const uid = process.argv[2];
const uidCards = `${uid}-cards.json`;
if (!existsSync("mcmCards.json")) {
console.error("Card db is missing! Run 'yarn convert-db' first.");
process.exit(1);
}
if (!existsSync(uidCards)) {
console.error(`Could not find ${uidCards}! Run 'yarn fetch ${uid}' first.`);
process.exit(1);
}
let db = await asyncLoadJSON<MCMDB>("mcmCards.json");
let articles = await asyncLoadJSON<Article[]>(`${uid}-cards.json`);
const cards = cubeCards(await genCube(db, articles));
let api = new CardMarketApi();
// Can only list 100 articles per request
let offset = 0;
while (offset < cards.length) {
const xml: string = await ejs.renderFile("templates/buy.ejs", {
cards: cards.slice(offset, offset + 100)
});
await api.put("/shoppingcart", null, xml);
offset += 100;
}
}
run();