From 8c88553d29fbed162493ce524acad88b65db37ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Wed, 18 Aug 2021 16:52:32 +0200 Subject: [PATCH] face_url upload prompt --- fftcg/book.py | 28 +++++++++++++++++++++------- fftcg/card.py | 31 +++++++++++++++++++++++-------- fftcg/carddb.py | 27 +++++++-------------------- fftcg/cards.py | 18 +++++++++--------- fftcg/code.py | 14 +++++++++----- fftcg/ttsdeck.py | 30 +++++++++++++++++------------- main.py | 2 +- 7 files changed, 87 insertions(+), 63 deletions(-) diff --git a/fftcg/book.py b/fftcg/book.py index f7dfdae..57ea116 100644 --- a/fftcg/book.py +++ b/fftcg/book.py @@ -15,6 +15,7 @@ class Book: # sort cards by element, then alphabetically cards.sort(key=lambda x: x.name) cards.sort(key=lambda x: "Multi" if len(x.elements) > 1 else x.elements[0]) + self.__file_name = cards.file_name # all card face URLs urls = [f"https://fftcg.cdn.sewest.net/images/cards/full/{card.code}_{language}.jpg" for card in cards] @@ -27,6 +28,9 @@ class Book: back_image = images.pop(-1) self.__pages = [] + + page_images: list[Image.Image] + page_cards: Cards for page_images, page_cards in zip(GRID.chunks(images), GRID.chunks(cards)): # create book page Image page_image = Image.new("RGB", GRID * RESOLUTION) @@ -39,16 +43,24 @@ class Book: # paste card back in last position GRID.paste(page_image, GRID.capacity, back_image) + # set card indices + for i, card in enumerate(page_cards): + card.index = i + # save page self.__pages.append({ "image": page_image, "cards": page_cards, }) - def save(self, file_name: str) -> None: - book: dict[str, dict[str, any]] + def save(self) -> None: + # save images + for i, page in enumerate(self.__pages): + fn = f"{self.__file_name}_{i}.jpg" + # save page image + page["image"].save(fn) - # load book.yml file + book: dict[str, Cards] try: with open(BOOK_YML_NAME, "r") as file: book = yaml.load(file, Loader=yaml.Loader) @@ -57,11 +69,13 @@ class Book: # save book for i, page in enumerate(self.__pages): - fn = f"{file_name}_{i}.jpg" - # save page image - page["image"].save(fn) + fn = f"{self.__file_name}_{i}.jpg" + # ask for upload + face_url = input(f"Upload '{fn}' and paste URL: ") + for card in page["cards"]: + card.face_url = face_url # add contents of that image - book[fn] = {"cards": page["cards"]} + book[fn] = page["cards"] # update book.yml file with open(BOOK_YML_NAME, "w") as file: diff --git a/fftcg/card.py b/fftcg/card.py index 04edd83..c166e87 100644 --- a/fftcg/card.py +++ b/fftcg/card.py @@ -2,15 +2,11 @@ from __future__ import annotations import re -import yaml - from .code import Code from .utils import encircle_symbol -class Card(yaml.YAMLObject): - yaml_tag = u'!Card' - +class Card: __ELEMENTS_MAP = { "火": "Fire", "氷": "Ice", @@ -22,20 +18,23 @@ class Card(yaml.YAMLObject): "闇": "Darkness" } - def __init__(self, code, elements, name, text): + def __init__(self, code: Code, elements: list[str], name: str, text: str, face_url: str = "", index: int = 0): self.__code = code self.__elements = elements self.__name = name self.__text = text + self.__face_url = face_url + self.__index = index + @classmethod def from_data(cls, data: dict[str, any], language: str) -> Card: if not data: return cls( code=Code(""), elements=[], - name=None, - text=None, + name="", + text="", ) else: @@ -88,3 +87,19 @@ class Card(yaml.YAMLObject): @property def elements(self) -> list[str]: return self.__elements + + @property + def face_url(self) -> str: + return self.__face_url + + @face_url.setter + def face_url(self, face_url: str) -> None: + self.__face_url = face_url + + @property + def index(self) -> int: + return self.__index + + @index.setter + def index(self, index: int) -> None: + self.__index = index diff --git a/fftcg/carddb.py b/fftcg/carddb.py index 4f2411a..9e9805a 100644 --- a/fftcg/carddb.py +++ b/fftcg/carddb.py @@ -2,6 +2,7 @@ from __future__ import annotations import yaml +from fftcg import Card from fftcg.code import Code from fftcg.utils import BOOK_YML_NAME @@ -17,10 +18,10 @@ class CardDB: return CardDB.__instance def __init__(self): - self.__content: dict[Code, dict[str, any]] = {} + self.__content: dict[Code, Card] = {} - def __getitem__(self, code: Code) -> dict[str, any]: - return self.__content[str(code)] + def __getitem__(self, code: Code) -> Card: + return self.__content[code] def load(self): # load book.yml file @@ -35,26 +36,12 @@ class CardDB: # every card is indexable by its code self.__content.clear() - for file_name, content in book.items(): + for file_name, cards in book.items(): self.__content |= { - str(card.code): { - "card": card, - "file": file_name, - "index": i, - } for i, card in enumerate(content["cards"]) + card.code: card + for card in cards } # write carddb.yml file with open("carddb.yml", "w") as file: yaml.dump(self.__content, file, Dumper=yaml.Dumper) - - # def make_deck(self, filters): - # # filter codes by card criteria - # codes = [ - # content["card"].code - # for content in self.__content.values() - # if all([f(content["card"]) for f in filters]) - # ] - # - # from .ttsdeck import TTSDeck - # return TTSDeck(codes) diff --git a/fftcg/cards.py b/fftcg/cards.py index eb4c27e..8dc566e 100644 --- a/fftcg/cards.py +++ b/fftcg/cards.py @@ -14,15 +14,7 @@ class Cards(list[Card]): def __str__(self) -> str: return f"[{', '.join(str(card) for card in self)}]" - @property - def name(self) -> str: - return self.__name - - @property - def file_name(self) -> str: - return self.name.lower().replace(" ", "_") - - def _load(self, params: dict[str, any]): + def _load(self, params: dict[str, any]) -> None: # required params: # text # supported params: @@ -36,3 +28,11 @@ class Cards(list[Card]): req = requests.post(Cards.__API_URL, json=params) self.clear() self.extend([Card.from_data(card_data, "EN") for card_data in req.json()["cards"]]) + + @property + def name(self) -> str: + return self.__name + + @property + def file_name(self) -> str: + return self.name.lower().replace(" ", "_") diff --git a/fftcg/code.py b/fftcg/code.py index 0b7a5b8..bc57360 100644 --- a/fftcg/code.py +++ b/fftcg/code.py @@ -1,11 +1,9 @@ +from __future__ import annotations + import re -import yaml - - -class Code(yaml.YAMLObject): - yaml_tag = u'!Code' +class Code: __RE_NUM = re.compile(r"([0-9]+)-([0-9]+)([CRHLS])") __RE_PROMO = re.compile(r"(PR)-([0-9]+)") __RE_BOSS = re.compile(r"(B)-([0-9]+)") @@ -36,6 +34,12 @@ class Code(yaml.YAMLObject): def __str__(self) -> str: return f"{self.__opus}-{self.__serial}{self.__rarity}" + def __hash__(self) -> hash: + return hash(str(self)) + + def __eq__(self, other: Code): + return str(self) == str(other) + @property def opus(self) -> str: return self.__opus diff --git a/fftcg/ttsdeck.py b/fftcg/ttsdeck.py index eef0897..2036257 100644 --- a/fftcg/ttsdeck.py +++ b/fftcg/ttsdeck.py @@ -1,20 +1,24 @@ import json from .carddb import CardDB +from .cards import Cards from .code import Code from .utils import CARD_BACK_URL -class TTSDeck(list[dict[str, any]]): +class TTSDeck(Cards): def __init__(self, codes: list[Code], name: str, description: str): - carddb = CardDB.get() - super().__init__([carddb[code] for code in codes]) - - self.__name = name + super().__init__(name) self.__description = description + carddb = CardDB.get() + self.extend([carddb[code] for code in codes]) + def __str__(self) -> str: - face_urls = list(set([entry["file"] for entry in self])) + face_urls = list(set([ + card.face_url + for card in self + ])) custom_deck = { str(i + 1): { @@ -49,14 +53,14 @@ class TTSDeck(list[dict[str, any]]): contained_objects = [ { "Name": "Card", - "Nickname": entry["card"].name, - "Description": entry["card"].text, + "Nickname": card.name, + "Description": card.text, - "CardID": 100 * custom_deck_inv[entry["file"]] + entry["index"], + "CardID": 100 * custom_deck_inv[card.face_url] + card.index, "Hands": True, "SidewaysCard": False, - } | common_dict for entry in self + } | common_dict for card in self ] deck_ids = [ @@ -64,10 +68,10 @@ class TTSDeck(list[dict[str, any]]): for contained_object in contained_objects ] - jsondict = {"ObjectStates": [ + json_dict = {"ObjectStates": [ { "Name": "Deck", - "Nickname": self.__name, + "Nickname": self.name, "Description": self.__description, "Hands": False, @@ -79,7 +83,7 @@ class TTSDeck(list[dict[str, any]]): } | common_dict ]} - return json.dumps(jsondict, indent=2) + return json.dumps(json_dict, indent=2) def save(self, file_name: str) -> None: pass diff --git a/main.py b/main.py index 11ede61..ab99cc4 100755 --- a/main.py +++ b/main.py @@ -41,7 +41,7 @@ def main() -> None: # main program opus = fftcg.Opus(args.opus_id) book = fftcg.Book(opus, "eg", args.num_threads) - book.save(opus.file_name) + book.save() # load the current carddb carddb = fftcg.CardDB.get()