1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-23 19:32:11 +00:00
cards-database/interfaces/Illutrator.ts
Florian Bouillon 0d2a757cae
base DB
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
2020-02-03 15:18:40 +01:00

54 lines
1.1 KiB
TypeScript

import { Page } from "puppeteer-core"
import { promises as fs } from 'fs'
interface Illustrator {
id: number
name: string
}
const file = "./generated/illustrators.json"
let illustrators: Array<string> = []
namespace Illustrator {
export async function fetchIllustrators(): Promise<void> {
const illus = await fs.readFile(file)
illustrators = JSON.parse(illus.toString())
}
export function processIllustrator(str: string): Illustrator {
if (!illustrators.includes(str)) {
illustrators.push(str)
}
return {
id: illustrators.indexOf(str),
name: str
}
}
export async function getIllustrator(tab: Page): Promise<Illustrator> {
const illustrator = await getIllustratorFromPage(tab)
if (!illustrators.includes(illustrator)) {
illustrators.push(illustrator)
}
return {
id: illustrators.indexOf(illustrator),
name: illustrator
}
}
export function getIllustratorFromPage(tab: Page): Promise<string> {
return tab.$eval("h4.highlight a", (el:HTMLElement) => {
return el.innerText
})
}
export function save(): Promise<void> {
return fs.writeFile(file, JSON.stringify(illustrators))
}
}
export default Illustrator