1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-23 19:32:11 +00:00

Merge branch 'master' of git.delta-wings.net:tcgdex/db

This commit is contained in:
Florian Bouillon 2020-02-08 00:53:16 +01:00
commit 5736230d3b
5 changed files with 80 additions and 276 deletions

View File

@ -5,7 +5,31 @@ enum AbilityType {
ANCIENTTRAIT ANCIENTTRAIT
} }
const en = [
"Poké-Body",
"Poké-Power",
"Ability",
"Ancient Trait"
]
const fr = [
"Poké-Body",
"Poké-Power",
"Talent",
"Trait Ancien"
]
namespace AbilityType { namespace AbilityType {
export function toLang(a: AbilityType, lang: string): string {
switch (lang) {
case "en":
return en[a]
case "fr":
return fr[a]
}
throw new Error(`Error , abilityType not translated! (${lang})`)
}
export function getFromText(txt: string): AbilityType { export function getFromText(txt: string): AbilityType {
switch (txt) { switch (txt) {
case "Ability": case "Ability":

View File

@ -2,16 +2,11 @@ import Type from "./Type";
import Category from "./Category"; import Category from "./Category";
import Tag from "./Tag"; import Tag from "./Tag";
import LangList from "./LangList"; import LangList from "./LangList";
import AbilityType from "./AbilityType";
import Rarity from "./Rarity"; import Rarity from "./Rarity";
import Attack from "./Attack"; import Attack from "./Attack";
import { Page } from "puppeteer-core";
import Illustrator from "./Illutrator"; import Illustrator from "./Illutrator";
import { promises } from "fs";
import Ability from "./Ability"; import Ability from "./Ability";
let abilities: Array<string> = []
interface Card { interface Card {
// global id made of setid and localId // global id made of setid and localId
@ -63,220 +58,4 @@ interface Card {
} }
} }
namespace Card {
export async function generate(tab: Page, existingCard: Card, lang: string, set: {name:string, code: string}): Promise<Card> {
const card: Card = Object.assign(n(), existingCard)
card.set = set
// LocalID
card.localId = await getId(tab)
card.id = `${card.set.code}-${card.localId}`
// first line
card.name = LangList.insert(card.name, await getName(tab), lang)
try {
card.hp = await getHP(tab)
} catch {}
try {
card.type = await getType(tab)
} catch {}
card.image.low = LangList.insert(card.image.low, await getImage(tab), lang)
try {
const from = await getEvolveFrom(tab)
card.evolveFrom = LangList.insert(card.evolveFrom || {}, from, lang)
} catch {}
card.tags = await Tag.get(tab)
try {
card.illustrator = await Illustrator.getIllustrator(tab)
} catch {}
try {
const ability = await getAbility(tab, lang)
if (!card.abilities) card.abilities = []
if (!card.abilities[0]) card.abilities[0] = {id: -1, type: -1, name: {}, text: {}}
if (ability.id) card.abilities[0].id = ability.id
try {
card.abilities[0].type = AbilityType.getFromText(ability.type)
} catch (e) {
console.log(`Error ${tab.url()}`)
console.log(e)
}
card.abilities[0].name = LangList.insert(card.abilities[0].name, ability.name, lang)
card.abilities[0].text = LangList.insert(card.abilities[0].text, ability.text, lang)
} catch (e) {}
try {
card.attacks = await Attack.getAttacks(tab, lang, card.attacks)
} catch (e){
console.log(e)
}
try {
card.weaknesses = await getWeaknessOrResistance(tab, 1)
} catch {}
try {
card.resistances = await getWeaknessOrResistance(tab, 2)
} catch {}
// Category
card.category = await Category.detect(tab)
try {
card.retreat = await getRetreat(tab)
} catch {
if (card.category === Category.POKEMON) {
card.retreat = 0
}
}
card.rarity = await Rarity.getRarity(tab)
return card
}
export function n() {
return {
name: {},
evolveFrom: {},
tags: [],
image: {}
}
}
export async function fetchAbilities() {
abilities = JSON.parse((await promises.readFile("./generated/abilities.json")).toString())
}
export function processAbility(str: string): number {
if (!abilities.includes(str)) {
abilities.push(str)
}
return abilities.indexOf(str)
}
export async function saveAbilities() {
await promises.writeFile("./generated/abilities.json", JSON.stringify(abilities))
}
function getEvolveFrom(tab: Page): Promise<string> {
return tab.$eval(".card-type h4 a", (el: HTMLElement) => {
return el.innerText
})
}
function getImage(tab: Page): Promise<string> {
return tab.$eval(".card-image img", (el: HTMLImageElement) => {
return el.src
})
}
async function getType(tab: Page): Promise<Array<Type>> {
try {
const types = await tab.$$eval(".card-basic-info .right a i", (els: Array<HTMLElement>) => {
const l = []
for (const el of els) {
el.classList.remove("energy")
l.push(
el.classList.item(0)
)
}
return l
})
const res = []
for (const t of types) {
res.push(
Type.getFromClass(t)
)
}
return res
} catch {
return []
}
}
export function getId(tab: Page): Promise<string|number> {
try {
return tab.$eval(".stats-footer span", (el: HTMLElement) => {
const g = /[\/ ]/g
const arr = el.innerText.split(g)
const r = /^[0-9]+$/
return r.test(arr[0]) ? parseInt(arr[0]) : arr[0]
})
} catch (e) {
console.log(tab.url())
console.log(e)
process.exit(1)
}
}
function getHP(tab: Page): Promise<number> {
return tab.$eval(".card-basic-info .card-hp", (el: HTMLElement) => {
const r = /[^0-9]/g
return parseInt(el.innerText.replace(r, ""))
})
}
function getName(tab: Page): Promise<string> {
return tab.$eval("h1", (el: HTMLElement) => {
return el.innerText
})
}
async function getAbility(tab: Page, lang: string): Promise<{id?: number, type: string, name: string, text: string}> {
const {type, name} = await tab.$eval(".pokemon-abilities h3", (el: HTMLElement) => {
return {
type: (el.querySelector("div[class]") as HTMLElement)?.innerText,
name: (el.querySelector("div:not([class])") as HTMLElement)?.innerText
}
})
const text = await tab.$eval(".pokemon-abilities h3 ~ p", (el: HTMLElement) => {
return el.innerText
})
let id = undefined
if (lang === "en") {
if (!abilities.includes(name)) {
abilities.push(name)
}
id = abilities.indexOf(name)
}
return {
id,
type,
name,
text
}
}
async function getWeaknessOrResistance(tab: Page, childNth: number): Promise<Array<{type: Type, value: string}>> {
const t = await tab.$eval(`.pokemon-stats .stat:nth-child(${childNth})`, (el: HTMLElement) => {
if (el.childElementCount === 1) return
const energy = el.querySelector("ul li a i")
const txt = (el.querySelector("ul li") as HTMLElement).innerText
energy.classList.remove("energy")
return {
energy: energy.classList.item(0),
txt
}
})
return [{
type: Type.getFromClass(t.energy),
value: t.txt
}]
}
function getRetreat(tab: Page): Promise<number> {
return tab.$eval(".stat.last .card-energies", (el: HTMLElement) => {
return el.childElementCount
})
}
}
export default Card export default Card

View File

@ -1,5 +1,3 @@
import { Page } from "puppeteer-core"
enum Category { enum Category {
POKEMON, POKEMON,
TRAINER, TRAINER,
@ -12,6 +10,12 @@ const en = [
"Energy" "Energy"
] ]
const fr = [
"Pokémon",
"Dresseur",
"Énergie"
]
namespace Category { namespace Category {
export function fromEnglish(str: string) { export function fromEnglish(str: string) {
let i = en.indexOf(str) let i = en.indexOf(str)
@ -20,38 +24,14 @@ namespace Category {
return i return i
} }
export function toLang(i: Category, lang: string) { export function toLang(i: Category, lang: string): string {
switch (lang) { switch (lang) {
case "en": case "en":
return en[i] return en[i]
default: case "fr":
break; return fr[i]
} }
} throw new Error(`Error, Language not implemented! (${lang})`)
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 !")
} }
} }

View File

@ -1,5 +1,3 @@
import { Page } from "puppeteer-core"
enum Rarity { enum Rarity {
Common, Common,
Uncommon, Uncommon,
@ -26,15 +24,54 @@ enum Rarity {
RareVMAX, RareVMAX,
} }
const en = [
"Common",
"unCommon",
"Rare",
"Rare Holo",
"Rare Holo EX",
"Rare Holo GX",
"Rare Holo Lv.X",
"Rare Ultra",
"Rare Prime",
"Rare ACE",
"Rare BREAK",
"Rainbow Rare",
"LEGEND"
]
const fr = [
"Commun",
"Non Commun",
"Rare",
"Rare Holo",
"Rare Holo EX",
"Rare Holo GX",
"Rare Holo Lv.X",
"Rare Ultra",
"Rare Prime",
"Rare ACE",
"Rare BREAK",
"Rainbow Rare",
"LEGEND"
]
namespace Rarity { namespace Rarity {
export async function getRarity(tab: Page): Promise<Rarity> { export function toLang(r: Rarity, lang: string): string {
const rarity = await tab.$eval(".stats-footer span", (el: HTMLElement) => { switch (lang) {
const arr = el.innerText.split(" ") case "en":
arr.shift() return en[r]
return arr.join(" ") case "fr":
}) return fr[r]
return getLocalRarity(rarity) }
throw new Error(`Error, rarity lang not found! (${lang})`)
} }
export function getLocalRarity(str?: string): Rarity { export function getLocalRarity(str?: string): Rarity {

View File

@ -1,5 +1,3 @@
import { Page } from "puppeteer-core";
enum Tag { enum Tag {
BASIC, BASIC,
BASICENERGY, BASICENERGY,
@ -84,20 +82,6 @@ namespace Tag {
break; break;
} }
} }
export async function get(tab: Page): Promise<Array<Tag>> {
const h2 = await tab.$eval(".card-type h2", (el: HTMLElement) => {
return el.innerText
})
const list: Array<Tag> = lookup(h2)
// SP
const isNull = await tab.$('h1 img[alt="[G]"]')
if (isNull !== null) {
list.push(Tag.SP)
}
return list
}
export function lookup(str: string): Array<Tag> { export function lookup(str: string): Array<Tag> {
const list: Array<Tag> = [] const list: Array<Tag> = []