diff --git a/fftcg/ttsdeck.py b/fftcg/ttsdeck.py index 01e89a1..462e818 100644 --- a/fftcg/ttsdeck.py +++ b/fftcg/ttsdeck.py @@ -43,6 +43,10 @@ class TTSDeck(Cards): for code in codes ]) + @property + def file_name(self) -> str: + return f"{super().file_name}.json" + __FFDECKS_API_URL = "https://ffdecks.com/api/deck" __RE_FFDECKS_ID = re.compile(r"((https?://)?ffdecks\.com(/+api)?/+deck/+)?([0-9]+).*", flags=re.UNICODE) @@ -125,7 +129,7 @@ class TTSDeck(Cards): # create deck object return cls(codes, name, description, True) - def tts_object(self, language: Language) -> dict[str, any]: + def get_tts_object(self, language: Language) -> dict[str, any]: carddb = CardDB() # unique face urls used @@ -207,11 +211,14 @@ class TTSDeck(Cards): return deck_dict + def get_json(self, language: Language) -> str: + return json.dumps(self.get_tts_object(language), indent=2) + def save(self, language: Language) -> None: # only save if the deck contains cards if self: if not os.path.exists(DECKS_DIR_NAME): os.mkdir(DECKS_DIR_NAME) - with open(os.path.join(DECKS_DIR_NAME, f"{self.file_name}.json"), "w") as file: - json.dump(self.tts_object(language), file, indent=2) + with open(os.path.join(DECKS_DIR_NAME, self.file_name), "w") as file: + file.write(self.get_json(language)) diff --git a/fftcgtool.py b/fftcgtool.py index b218452..6a91124 100755 --- a/fftcgtool.py +++ b/fftcgtool.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 import argparse +import io import logging import os +import sys +import zipfile import fftcg @@ -66,6 +69,12 @@ def main() -> None: help="language for imported objects", ) + parser.add_argument( + "-s", "--stdout", + action="store_true", + help="print the deck files in a zip archive to stdout, skip creating JSONs on disk", + ) + subparsers = parser.add_subparsers( description="Import either an Opus to extend the mod, or import a deck to play right away.", dest="subcommand", @@ -143,12 +152,29 @@ def main() -> None: os.chdir(OUT_DIR_NAME) # call function based on args - for deck in args.func(args): - deck.save(args.language) + decks = args.func(args) - # bye - print("Done. Put the generated JSON files in your 'Saved Objects' Folder.") - print("Thanks for using fftcgtool!") + # decide what to do with the decks + if args.stdout: + # create file-like buffer + with io.BytesIO() as buffer: + # create zip file in buffer + with zipfile.ZipFile(buffer, "w") as zip_file: + for deck in decks: + zip_file.writestr(deck.file_name, deck.get_json(args.language)) + + # write zip file to stdout + buffer.seek(0) + with open(sys.stdout.fileno(), "wb", closefd=False, buffering=0) as stdout: + stdout.write(buffer.read()) + + else: + for deck in decks: + deck.save(args.language) + + # bye + print("Done. Put the generated JSON files in your 'Saved Objects' Folder.") + print("Thanks for using fftcgtool!") if __name__ == "__main__":