From 0a4fd686efde422b68076b02159902c968f0e874 Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Wed, 8 Sep 2021 15:25:20 +0200 Subject: [PATCH] multi deck import in ttsdeck --- fftcgtool/scripts/fftcgtool.py | 7 +------ fftcgtool/ttsdeck.py | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/fftcgtool/scripts/fftcgtool.py b/fftcgtool/scripts/fftcgtool.py index 6c53e65..0b1230d 100755 --- a/fftcgtool/scripts/fftcgtool.py +++ b/fftcgtool/scripts/fftcgtool.py @@ -168,12 +168,7 @@ def ffdecks(deck_ids: list[str]) -> list[fftcgtool.TTSDeck]: DECK_ID: each of the Decks to import """ - decks: list[fftcgtool.TTSDeck] = [] - for deck_id in deck_ids: - # import a deck - decks.append(fftcgtool.TTSDeck.from_ffdecks_deck(deck_id)) - - return decks + return fftcgtool.TTSDeck.from_ffdecks_decks(deck_ids) @main.result_callback() diff --git a/fftcgtool/ttsdeck.py b/fftcgtool/ttsdeck.py index c1a150f..09df70e 100644 --- a/fftcgtool/ttsdeck.py +++ b/fftcgtool/ttsdeck.py @@ -4,6 +4,7 @@ import json import logging import os import re +from typing import Optional, Iterable import requests @@ -51,7 +52,7 @@ class TTSDeck(Cards): __RE_FFDECKS_ID = re.compile(r"((https?://)?ffdecks\.com(/+api)?/+deck/+)?([0-9]+).*", flags=re.UNICODE) @classmethod - def from_ffdecks_deck(cls, deck_id: str) -> TTSDeck: + def from_ffdecks_deck(cls, deck_id: str) -> Optional[TTSDeck]: logger = logging.getLogger(__name__) # check deck id @@ -59,7 +60,7 @@ class TTSDeck(Cards): if match is None: logger.error("Malformed Deck ID for FFDecks API!") - return cls([], "", "", True) + return None else: # extract deck id from match @@ -70,9 +71,13 @@ class TTSDeck(Cards): if not res.ok: logger.error("Invalid Deck ID for FFDecks API!") - return cls([], "", "", True) + return None - logger.info(f"Importing Deck {deck_id}") + # general metadata + name = f"{res.json()['name']}" + description = deck_id + + logger.info(f"Importing Deck {name!r}") # pre-extract the used data deck_cards = [{ @@ -124,13 +129,19 @@ class TTSDeck(Cards): for _ in range(card["count"]) ] - # general metadata - name = f"{res.json()['name']}" - description = deck_id - # create deck object return cls(codes, name, description, True) + @classmethod + def from_ffdecks_decks(cls, deck_ids: Iterable[str]) -> list[TTSDeck]: + return [ + deck + for deck in ( + cls.from_ffdecks_deck(deck_id) + for deck_id in deck_ids + ) if deck is not None + ] + def get_tts_object(self, language: Language) -> dict[str, any]: carddb = CardDB()