mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-07-29 11:09:51 +00:00
58
interfaces/Category.ts
Normal file
58
interfaces/Category.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { Page } from "puppeteer-core"
|
||||
|
||||
enum Category {
|
||||
POKEMON,
|
||||
TRAINER,
|
||||
ENERGY
|
||||
}
|
||||
|
||||
const en = [
|
||||
"Pokémon",
|
||||
"Trainer",
|
||||
"Energy"
|
||||
]
|
||||
|
||||
namespace Category {
|
||||
export function fromEnglish(str: string) {
|
||||
let i = en.indexOf(str)
|
||||
if (str === "Pok<6F><6B>mon") i = 1
|
||||
if (i < 0) throw new Error(`Cannot get the category (${str})`)
|
||||
return i
|
||||
}
|
||||
|
||||
export function toLang(i: Category, lang: string) {
|
||||
switch (lang) {
|
||||
case "en":
|
||||
return en[i]
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
export async function detect(tab: Page): Promise<Category> {
|
||||
const type = await tab.$eval(".card-basic-info .card-type h2", (el: HTMLElement) => {
|
||||
return el.innerText
|
||||
})
|
||||
if (
|
||||
type.startsWith("Dresseur") ||
|
||||
type.startsWith("Trainer") ||
|
||||
type.startsWith("Entrenador")
|
||||
) {
|
||||
return Category.TRAINER
|
||||
}
|
||||
if (
|
||||
type.startsWith("Énergie") ||
|
||||
type.endsWith("Energy") ||
|
||||
type.startsWith("Energía")
|
||||
) {
|
||||
return Category.ENERGY
|
||||
}
|
||||
try {
|
||||
await tab.$(".pokemon-stats .stat:nth-child(3)")
|
||||
return Category.POKEMON
|
||||
} catch {}
|
||||
console.log(tab.url())
|
||||
throw new Error("Pokemon Category not found !")
|
||||
}
|
||||
}
|
||||
|
||||
export default Category
|
Reference in New Issue
Block a user