From 2ed6377d2d16a3c3ae95efefbbd9180344528dec Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 11 Mar 2020 11:36:14 +0100 Subject: [PATCH] Added Rarities endpoint Signed-off-by: Avior --- endpoints/rarities/index.ts | 29 ++++++++++++++++++++ endpoints/rarities/item.ts | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 endpoints/rarities/index.ts create mode 100644 endpoints/rarities/item.ts diff --git a/endpoints/rarities/index.ts b/endpoints/rarities/index.ts new file mode 100644 index 0000000..eb0d20c --- /dev/null +++ b/endpoints/rarities/index.ts @@ -0,0 +1,29 @@ +import { getBaseFolder } from "../util" +import TranslationUtil from "@tcgdex/sdk/TranslationUtil" +import { Langs } from "@tcgdex/sdk/interfaces/LangList" +import { promises as fs } from 'fs' +import Rarity, { RaritySimple, RarityList } from "@tcgdex/sdk/interfaces/Rarity" + +const lang = process.env.CARDLANG as Langs || "en" +const endpoint = getBaseFolder(lang, "rarities") + +const btsp = async () => { + + const list: Array = [] + for (const cat of Object.values(Rarity)) { + if (typeof cat !== "number") continue + list.push({ + id: cat, + name: TranslationUtil.translate("rarity", cat, lang) + }) + } + + const res: RarityList = { + count: list.length, + list: list + } + + await fs.mkdir(endpoint, {recursive: true}) + await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(res)) +} +btsp() diff --git a/endpoints/rarities/item.ts b/endpoints/rarities/item.ts new file mode 100644 index 0000000..eb2f809 --- /dev/null +++ b/endpoints/rarities/item.ts @@ -0,0 +1,54 @@ +import { getAllCards, getBaseFolder, urlize } from "../util" +import { fetchCard, isCardAvailable, cardToCardSimple } from "../cardUtil" +import Card from "@tcgdex/sdk/interfaces/Card" +import { Langs } from "@tcgdex/sdk/interfaces/LangList" +import TranslationUtil from "@tcgdex/sdk/TranslationUtil" +import { promises } from "fs" +import Rarity, { RaritySingle } from "@tcgdex/sdk/interfaces/Rarity" + +type rarityCards = { + [key in Rarity]?: Array +} + +const lang = process.env.CARDLANG as Langs || "en" +const endpoint = getBaseFolder(lang, "rarities") + + +const btsp = async () => { + const list = getAllCards() + const arr: rarityCards = {} + for (const i of list) { + const card = await fetchCard(i) + + if (!isCardAvailable(card, lang)) continue + + const c = card.rarity + + if (!(c in arr)) arr[c] = [] + arr[c].push(card) + + } + + for (const cat in arr) { + if (arr.hasOwnProperty(cat)) { + const cards: Array = arr[cat]; + const rCat: Rarity = parseInt(cat) + const toSave: RaritySingle = { + id: rCat, + name: TranslationUtil.translate("rarity", rCat, lang), + cards: cards.map(el => cardToCardSimple(el, lang)) + } + + const index = `${endpoint}/${toSave.id}` + const name = `${endpoint}/${urlize(toSave.name)}` + + await promises.mkdir(index, {recursive: true}) + await promises.mkdir(name, {recursive: true}) + + await promises.writeFile(`${index}/index.json`, JSON.stringify(toSave)) + await promises.writeFile(`${name}/index.json`, JSON.stringify(toSave)) + } + } +} + +btsp()