From c2f0f7f050a602af9bbcf461686d5387335a1cf6 Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Thu, 2 Sep 2021 02:54:16 +0200 Subject: [PATCH] preserve previously generated "face" values --- fftcg/card.py | 4 ++-- fftcg/carddb.py | 3 +++ fftcg/language.py | 16 ++++++++++------ fftcg/opus.py | 16 ++++++++++++---- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/fftcg/card.py b/fftcg/card.py index 7228cf0..f862506 100644 --- a/fftcg/card.py +++ b/fftcg/card.py @@ -4,7 +4,7 @@ import re from dataclasses import dataclass from .code import Code -from .language import Language +from .language import Language, API_LANGS from .utils import encircle_symbol @@ -75,7 +75,7 @@ class Card: content = { language: CardContent(load_name(language), load_text(language), "") - for language in Language.all_api_langs() + for language in API_LANGS } return cls( diff --git a/fftcg/carddb.py b/fftcg/carddb.py index a0d0af9..9a6406c 100644 --- a/fftcg/carddb.py +++ b/fftcg/carddb.py @@ -20,6 +20,9 @@ class CardDB: return CardDB.__instance + def __contains__(self, item: Code) -> bool: + return item in self.__content + def __getitem__(self, code: Code) -> Card: return self.__content[code] diff --git a/fftcg/language.py b/fftcg/language.py index 4a50d26..3f6423f 100644 --- a/fftcg/language.py +++ b/fftcg/language.py @@ -36,9 +36,13 @@ class Language: else: return "_EN" - @classmethod - def all_api_langs(cls) -> set[Language]: - return set([ - cls(short) - for short in ["de", "en", "es", "fr", "it", "ja"] - ]) + +API_LANGS = frozenset([ + Language(short) + for short in ["de", "en", "es", "fr", "it", "ja"] +]) + +IMG_LANGS = frozenset([ + Language(short) + for short in ["de", "en", "es", "fr", "it"] +]) diff --git a/fftcg/opus.py b/fftcg/opus.py index 444687f..af539c5 100644 --- a/fftcg/opus.py +++ b/fftcg/opus.py @@ -1,11 +1,13 @@ import logging +from dataclasses import replace import requests import roman from .card import Card +from .carddb import CardDB from .cards import Cards -from .language import Language +from .language import Language, API_LANGS from .ttsdeck import TTSDeck @@ -14,20 +16,21 @@ class Opus(Cards): def __init__(self, opus_id: str, language: Language): logger = logging.getLogger(__name__) + self.__language = language params: dict[str, any] if opus_id.isnumeric(): - name = f"Opus {opus_id} ({language.short})" + name = f"Opus {opus_id} ({self.__language.short})" self.__number = opus_id params = {"set": [f"Opus {roman.toRoman(int(opus_id)).upper()}"]} elif opus_id == "chaos": - name = f"Boss Deck Chaos ({language.short})" + name = f"Boss Deck Chaos ({self.__language.short})" self.__number = "B" params = {"set": ["Boss Deck Chaos"]} elif opus_id == "promo": - name = f"Promo ({language.short})" + name = f"Promo ({self.__language.short})" self.__number = "PR" params = {"rarity": ["pr"]} @@ -49,6 +52,7 @@ class Opus(Cards): # get cards from square api req = requests.post(Opus.__SQUARE_API_URL, json=params) + carddb = CardDB() cards = [ Card.from_square_api_data(card_data) for card_data in req.json()["cards"] @@ -66,6 +70,10 @@ class Opus(Cards): self.sort(key=lambda x: x.code.opus) for card in self: + if card.code in carddb: + for lang in API_LANGS: + card[lang] = replace(card[lang], face=carddb[card.code][lang].face) + logger.debug(f"imported card {card}") @property