diff --git a/fftcg/book.py b/fftcg/book.py index 53b51e0..df41a58 100644 --- a/fftcg/book.py +++ b/fftcg/book.py @@ -20,12 +20,12 @@ class Book: for card in cards ] # card back URL - urls.append((CARD_BACK_URL, "", "")) + urls.insert(0, (CARD_BACK_URL, "", "")) # multi-threaded download - images = ImageLoader.load(urls, num_threads) + images = iter(ImageLoader.load(urls, num_threads)) # card back Image - back_image = images.pop(-1) + back_image = next(images) self.__pages = [] @@ -63,5 +63,6 @@ class Book: # save images for page in self.__pages: - page["file_name"] = os.path.join(IMAGES_DIR_NAME, page["file_name"]) - page["image"].save(page["file_name"]) + page["image"].save( + os.path.join(IMAGES_DIR_NAME, page["file_name"]) + ) diff --git a/fftcg/opus.py b/fftcg/opus.py index 0bb2f64..adb74e0 100644 --- a/fftcg/opus.py +++ b/fftcg/opus.py @@ -1,6 +1,6 @@ import logging from dataclasses import replace -from typing import Callable, Iterator +from typing import Callable, Iterable import requests import roman @@ -41,6 +41,8 @@ class Opus(Cards): self.__filename = "?" params = {"set": "?"} + logger.info(f"Importing Opus {self.__number}") + # required params: # text # supported params: @@ -82,7 +84,7 @@ class Opus(Cards): return self.__number @property - def elemental_decks(self) -> Iterator[TTSDeck]: + def elemental_decks(self) -> Iterable[TTSDeck]: if self.number in ["PR", "B"]: return [TTSDeck( codes=[ diff --git a/fftcg/ttsdeck.py b/fftcg/ttsdeck.py index 462e818..6998b91 100644 --- a/fftcg/ttsdeck.py +++ b/fftcg/ttsdeck.py @@ -72,6 +72,8 @@ class TTSDeck(Cards): logger.error("Invalid Deck ID for FFDecks API!") return cls([], "", "", True) + logger.info(f"Importing Deck {deck_id}") + # pre-extract the used data deck_cards = [{ "code": card["card"]["serial_number"], diff --git a/fftcg/utils.py b/fftcg/utils.py index 849bc9b..a5faf30 100644 --- a/fftcg/utils.py +++ b/fftcg/utils.py @@ -1,4 +1,5 @@ -from typing import Iterator +import itertools +from typing import Iterator, Generator, Iterable from PIL import Image @@ -39,13 +40,17 @@ def encircle_symbol(symbol: str, negative: bool) -> str: return chr(ord(base_symbols[1]) + symbol_num) -def chunks(chunk_size: int, whole: list) -> Iterator[list]: - # while there are elements - while whole: - # get a chunk - yield whole[:chunk_size] - # remove that chunk - whole = whole[chunk_size:] +def chunks(chunk_size: int, whole: Iterable) -> Generator[Iterator, None, None]: + it = iter(whole) + # get chunk + while chunk := itertools.islice(it, chunk_size): + # stop if no first element was found + try: + first_el = next(chunk) + except StopIteration: + return + # reattach first element + yield itertools.chain((first_el,), chunk) def grid_paste(page: Image.Image, index: int, card: Image.Image) -> None: diff --git a/fftcgtool.py b/fftcgtool.py index ae6355c..4ddad76 100755 --- a/fftcgtool.py +++ b/fftcgtool.py @@ -12,10 +12,7 @@ OUT_DIR_NAME = "out" # name of output directory def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]: - # load the current carddb carddb = fftcg.CardDB() - carddb.load() - decks: list[fftcg.TTSDeck] = [] for opus_id in args.opus_ids: # import an opus @@ -33,15 +30,11 @@ def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]: def ffdecks_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]: - # load the current carddb - carddb = fftcg.CardDB() - carddb.load() - decks: list[fftcg.TTSDeck] = [] for deck_id in args.deck_ids: + # import a deck decks.append(fftcg.TTSDeck.from_ffdecks_deck(deck_id)) - # import a deck return decks @@ -142,6 +135,7 @@ def main() -> None: ) logger = logging.getLogger(__name__) + logger.info("fftcgtool started.") logger.debug(f"{args = }") # output directory @@ -150,6 +144,10 @@ def main() -> None: os.chdir(OUT_DIR_NAME) + # load the current carddb + carddb = fftcg.CardDB() + carddb.load() + # call function based on args decks = args.func(args)