1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00
fftcgtool/fftcgtool.py

156 lines
3.5 KiB
Python
Raw Normal View History

2021-08-03 00:22:00 +00:00
#!/usr/bin/env python3
2021-08-04 16:36:23 +00:00
import argparse
import logging
import os
2021-08-03 00:22:00 +00:00
2021-08-09 05:01:25 +00:00
import fftcg
2021-08-23 14:00:17 +00:00
# constants
OUT_DIR_NAME = "out" # name of output directory
2021-08-23 12:57:05 +00:00
def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
2021-09-01 18:14:56 +00:00
# load the current carddb
carddb = fftcg.CardDB()
carddb.load()
2021-08-23 12:57:05 +00:00
2021-09-01 18:14:56 +00:00
decks: list[fftcg.TTSDeck] = []
2021-08-23 16:25:25 +00:00
for opus_id in args.opus_ids:
# import an opus
opus = fftcg.Opus(opus_id, args.language)
book = fftcg.Book(opus, args.language, args.num_requests)
book.save()
carddb.update(opus)
decks.extend(opus.elemental_decks)
2021-08-23 12:57:05 +00:00
2021-09-02 01:06:12 +00:00
carddb.upload_prompt()
2021-08-23 12:57:05 +00:00
# create elemental decks for opus
2021-08-23 16:25:25 +00:00
return decks
2021-08-23 12:57:05 +00:00
2021-08-24 15:32:51 +00:00
def ffdecks_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
2021-08-23 12:57:05 +00:00
# load the current carddb
2021-09-01 18:14:56 +00:00
carddb = fftcg.CardDB()
2021-08-23 12:57:05 +00:00
carddb.load()
2021-08-24 15:32:51 +00:00
decks: list[fftcg.TTSDeck] = []
for deck_id in args.deck_ids:
decks.append(fftcg.TTSDeck.from_ffdecks_deck(deck_id))
2021-08-23 12:57:05 +00:00
# import a deck
2021-08-24 15:32:51 +00:00
return decks
2021-08-23 12:57:05 +00:00
2021-08-09 02:03:24 +00:00
def main() -> None:
# set up CLI
2021-08-23 12:57:05 +00:00
# main parser
2021-08-04 16:36:23 +00:00
parser = argparse.ArgumentParser(
2021-08-23 12:57:05 +00:00
prog="fftcgtool",
2021-08-09 04:18:56 +00:00
description="Imports FFTCG cards for TT-Sim.",
)
parser.add_argument(
"-v", "--verbose",
help="increase output verbosity",
action="count",
)
2021-08-23 16:16:56 +00:00
parser.add_argument(
"-l", "--language",
type=fftcg.Language,
default="en",
2021-09-03 00:45:32 +00:00
metavar="LANG",
help="language for imported objects",
2021-08-23 16:16:56 +00:00
)
2021-08-23 12:57:05 +00:00
subparsers = parser.add_subparsers(
description="Import either an Opus to extend the mod, or import a deck to play right away.",
dest="subcommand",
help="valid subcommands",
required=True,
)
# "opus" subcommand
2021-09-03 00:45:32 +00:00
opuses_parser = subparsers.add_parser(
"opuses",
description="Imports Opuses from the square API and creates its elemental decks as JSON files.",
2021-08-23 12:57:05 +00:00
)
2021-09-03 00:45:32 +00:00
opuses_parser.set_defaults(
2021-08-23 12:57:05 +00:00
func=opus_decks
)
2021-09-03 00:45:32 +00:00
opuses_parser.add_argument(
2021-08-23 16:25:25 +00:00
"opus_ids",
2021-08-23 12:57:05 +00:00
type=str,
2021-08-23 16:25:25 +00:00
nargs="+",
2021-08-04 22:54:15 +00:00
metavar="Opus_ID",
2021-08-23 16:25:25 +00:00
help="the Opuses to import",
2021-08-09 04:18:56 +00:00
)
2021-08-04 16:36:23 +00:00
2021-09-03 00:45:32 +00:00
opuses_parser.add_argument(
2021-08-23 12:57:05 +00:00
"-n", "--num_requests",
2021-08-04 16:36:23 +00:00
type=int,
default=20,
metavar="COUNT",
2021-08-09 04:18:56 +00:00
help="maximum number of concurrent requests",
)
2021-08-04 16:36:23 +00:00
2021-09-03 00:45:32 +00:00
# "ffdecks" subcommand
ffdecks_parser = subparsers.add_parser(
"ffdecks",
description="Imports Decks from the ffdecks.com API and creates it as a JSON file.",
2021-08-23 12:57:05 +00:00
)
2021-09-03 00:45:32 +00:00
ffdecks_parser.set_defaults(
2021-08-24 15:32:51 +00:00
func=ffdecks_decks
2021-08-23 12:57:05 +00:00
)
2021-09-03 00:45:32 +00:00
ffdecks_parser.add_argument(
2021-08-24 15:32:51 +00:00
"deck_ids",
2021-08-23 12:57:05 +00:00
type=str,
2021-08-24 15:32:51 +00:00
nargs="+",
2021-08-23 12:57:05 +00:00
metavar="Deck_ID",
2021-08-24 15:32:51 +00:00
help="the Decks to import",
2021-08-23 12:57:05 +00:00
)
2021-08-04 16:36:23 +00:00
# parse arguments
args = parser.parse_args()
# set up logging
if args.verbose is None:
args.verbose = logging.WARN
elif args.verbose == 1:
args.verbose = logging.INFO
else:
args.verbose = logging.DEBUG
2021-08-23 12:57:05 +00:00
logging.basicConfig(
level=args.verbose,
2021-08-23 12:57:05 +00:00
format="%(levelname)s: %(processName)s %(message)s",
)
2021-08-04 16:36:23 +00:00
logger = logging.getLogger(__name__)
logger.debug(f"{args = }")
2021-08-04 16:36:23 +00:00
# output directory
2021-08-23 14:00:17 +00:00
if not os.path.exists(OUT_DIR_NAME):
os.mkdir(OUT_DIR_NAME)
os.chdir(OUT_DIR_NAME)
2021-08-04 16:36:23 +00:00
2021-08-23 12:57:05 +00:00
# call function based on args
for deck in args.func(args):
deck.save(args.language)
# bye
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.")
print("Thanks for using fftcgtool!")
2021-08-04 01:39:19 +00:00
2021-08-09 04:18:56 +00:00
if __name__ == "__main__":
2021-08-03 21:41:25 +00:00
main()