diff --git a/endpoints/categories/index.ts b/endpoints/categories/index.ts new file mode 100644 index 0000000..922f631 --- /dev/null +++ b/endpoints/categories/index.ts @@ -0,0 +1,30 @@ +import { getBaseFolder } from "../util" +import Category from '@tcgdex/sdk/interfaces/Category' +import { categorySimple, categoryList } from "./category" +import TranslationUtil from "@tcgdex/sdk/TranslationUtil" +import { Langs } from "@tcgdex/sdk/interfaces/LangList" +import { promises as fs } from 'fs' + +const lang = process.env.CARDLANG as Langs || "en" +const endpoint = getBaseFolder(lang, "categories") + +const btsp = async () => { + + const list: Array = [] + for (const cat of Object.values(Category)) { + if (typeof cat !== "number") continue + list.push({ + id: cat, + name: TranslationUtil.translate("category", cat, lang) + }) + } + + const res: categoryList = { + 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/categories/item.ts b/endpoints/categories/item.ts new file mode 100644 index 0000000..71dd23b --- /dev/null +++ b/endpoints/categories/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 Category, { CategorySingle } from "@tcgdex/sdk/interfaces/Category" + +type categoryCards = { + [key in Category]?: Array +} + +const lang = process.env.CARDLANG as Langs || "en" +const endpoint = getBaseFolder(lang, "categories") + + +const btsp = async () => { + const list = getAllCards() + const arr: categoryCards = {} + for (const i of list) { + const card = await fetchCard(i) + + if (!isCardAvailable(card, lang)) continue + + const c = card.category + + 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: Category = parseInt(cat) + const toSave: CategorySingle = { + id: rCat, + name: TranslationUtil.translate("category", 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()