mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
ttsdeck checking
This commit is contained in:
parent
68533b9a44
commit
e9e6d5b2b8
1 changed files with 36 additions and 0 deletions
|
@ -1,7 +1,9 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -14,24 +16,58 @@ from .utils import CARD_BACK_URL, DECKS_DIR_NAME
|
||||||
|
|
||||||
class TTSDeck(Cards):
|
class TTSDeck(Cards):
|
||||||
def __init__(self, codes: list[Code], name: str, description: str, face_down: bool):
|
def __init__(self, codes: list[Code], name: str, description: str, face_down: bool):
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.__description = description
|
self.__description = description
|
||||||
self.__face_down = face_down
|
self.__face_down = face_down
|
||||||
|
|
||||||
# get cards from carddb
|
# get cards from carddb
|
||||||
carddb = CardDB()
|
carddb = CardDB()
|
||||||
|
|
||||||
|
# non-imported cards
|
||||||
|
codes_invalid = frozenset([
|
||||||
|
code
|
||||||
|
for code in codes
|
||||||
|
if code not in carddb
|
||||||
|
])
|
||||||
|
|
||||||
|
# show errors and remove non-imported cards
|
||||||
|
for code in codes_invalid:
|
||||||
|
logger.error(f"Code '{code}' not in CardDB, ignoring!")
|
||||||
|
while code in codes:
|
||||||
|
codes.remove(code)
|
||||||
|
|
||||||
|
# put existing cards into deck
|
||||||
self.extend([
|
self.extend([
|
||||||
carddb[code]
|
carddb[code]
|
||||||
for code in codes
|
for code in codes
|
||||||
])
|
])
|
||||||
|
|
||||||
__FFDECKS_API_URL = "https://ffdecks.com/api/deck"
|
__FFDECKS_API_URL = "https://ffdecks.com/api/deck"
|
||||||
|
__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) -> TTSDeck:
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# check deck id
|
||||||
|
match = TTSDeck.__RE_FFDECKS_ID.match(deck_id)
|
||||||
|
|
||||||
|
if match is None:
|
||||||
|
logger.error("Malformed Deck ID for FFDecks API!")
|
||||||
|
return cls([], "", "", True)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# extract deck id from match
|
||||||
|
deck_id = match.groups()[3]
|
||||||
|
|
||||||
# api request
|
# api request
|
||||||
req = requests.get(TTSDeck.__FFDECKS_API_URL, params={"deck_id": deck_id})
|
req = requests.get(TTSDeck.__FFDECKS_API_URL, params={"deck_id": deck_id})
|
||||||
|
|
||||||
|
if req.status_code != 200:
|
||||||
|
logger.error("Invalid Deck ID for FFDecks API!")
|
||||||
|
return cls([], "", "", True)
|
||||||
|
|
||||||
# pre-extract the used data
|
# pre-extract the used data
|
||||||
deck_cards = [{
|
deck_cards = [{
|
||||||
"code": card["card"]["serial_number"],
|
"code": card["card"]["serial_number"],
|
||||||
|
|
Loading…
Reference in a new issue