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

multi deck import in ttsdeck

This commit is contained in:
Jörn-Michael Miehe 2021-09-08 15:25:20 +02:00
parent 2f49e75143
commit 0a4fd686ef
2 changed files with 20 additions and 14 deletions

View file

@ -168,12 +168,7 @@ def ffdecks(deck_ids: list[str]) -> list[fftcgtool.TTSDeck]:
DECK_ID: each of the Decks to import DECK_ID: each of the Decks to import
""" """
decks: list[fftcgtool.TTSDeck] = [] return fftcgtool.TTSDeck.from_ffdecks_decks(deck_ids)
for deck_id in deck_ids:
# import a deck
decks.append(fftcgtool.TTSDeck.from_ffdecks_deck(deck_id))
return decks
@main.result_callback() @main.result_callback()

View file

@ -4,6 +4,7 @@ import json
import logging import logging
import os import os
import re import re
from typing import Optional, Iterable
import requests 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) __RE_FFDECKS_ID = re.compile(r"((https?://)?ffdecks\.com(/+api)?/+deck/+)?([0-9]+).*", flags=re.UNICODE)
@classmethod @classmethod
def from_ffdecks_deck(cls, deck_id: str) -> TTSDeck: def from_ffdecks_deck(cls, deck_id: str) -> Optional[TTSDeck]:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# check deck id # check deck id
@ -59,7 +60,7 @@ class TTSDeck(Cards):
if match is None: if match is None:
logger.error("Malformed Deck ID for FFDecks API!") logger.error("Malformed Deck ID for FFDecks API!")
return cls([], "", "", True) return None
else: else:
# extract deck id from match # extract deck id from match
@ -70,9 +71,13 @@ class TTSDeck(Cards):
if not res.ok: if not res.ok:
logger.error("Invalid Deck ID for FFDecks API!") 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 # pre-extract the used data
deck_cards = [{ deck_cards = [{
@ -124,13 +129,19 @@ class TTSDeck(Cards):
for _ in range(card["count"]) for _ in range(card["count"])
] ]
# general metadata
name = f"{res.json()['name']}"
description = deck_id
# create deck object # create deck object
return cls(codes, name, description, True) 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]: def get_tts_object(self, language: Language) -> dict[str, any]:
carddb = CardDB() carddb = CardDB()