diff --git a/magic_spoiler/__main__.py b/magic_spoiler/__main__.py index 5fb1ba21..b3b5d3cf 100644 --- a/magic_spoiler/__main__.py +++ b/magic_spoiler/__main__.py @@ -1,13 +1,12 @@ """ Handle Scryfall Spoilers """ -import hashlib -import json -import shutil - import contextvars import datetime +import hashlib +import json import pathlib +import shutil import time from typing import IO, Any, Dict, List, Tuple, Union @@ -98,15 +97,15 @@ def build_types(sf_card: Dict[str, Any]) -> Tuple[List[str], str, List[str]]: type_line = sf_card["type_line"] - if u"—" in type_line: - card_subs = type_line.split(u"—")[1].strip() + if "—" in type_line: + card_subs = type_line.split("—")[1].strip() sub_types = card_subs.split(" ") if " " in card_subs else [card_subs] for card_type in all_super_types: if card_type in type_line: super_types.append(card_type) - types: str = type_line.split(u"—")[0] + types: str = type_line.split("—")[0] for card_type in all_super_types: types = types.replace(card_type, "") @@ -156,30 +155,43 @@ def scryfall2mtgjson(scryfall_cards: List[Dict[str, Any]]) -> List[Dict[str, Any for sf_card in composed_sf_cards: super_types, types, sub_types = build_types(sf_card) - trice_card = { - "cmc": sf_card["cmc"], - "names": sf_card.get("names", None), - "mana_cost": sf_card.get("mana_cost", ""), - "name": sf_card["name"], - "number": sf_card["collector_number"], - "rarity": sf_card["rarity"].replace("mythic", "mythic rare").title(), - "text": sf_card.get("oracle_text", ""), - "url": sf_card["image_uris"].get("normal", "").rsplit("?", 1)[0], - "type": sf_card.get("type_line", "Unknown").replace(u"—", "-"), - "colorIdentity": sf_card.get("color_identity", None), - "colors": sf_card["colors"], - "power": sf_card.get("power", None), - "toughness": sf_card.get("toughness", None), - "layout": sf_card["layout"].replace("normal", ""), - "loyalty": sf_card.get("loyalty", None), - "artist": sf_card.get("artist", ""), - "flavor": sf_card.get("flavor_text", None), - "multiverseId": sf_card.get("multiverse_id", None), - "superTypes": super_types, - "types": types, - "subTypes": sub_types, - } - trice_cards.append(trice_card) + if "//" in sf_card.get("name", ""): + image = ( + sf_card["card_faces"][0] + .get("image_uris", {}) + .get("normal", "") + .rsplit("?", 1)[0], + ) + else: + image = sf_card.get("image_uris", {}).get("normal", "").rsplit("?", 1)[0] + + try: + trice_card = { + "cmc": sf_card["cmc"], + "names": sf_card.get("names", None), + "mana_cost": sf_card.get("mana_cost", ""), + "name": sf_card["name"], + "number": sf_card["collector_number"], + "rarity": sf_card["rarity"].replace("mythic", "mythic rare").title(), + "text": sf_card.get("oracle_text", ""), + "url": image, + "type": sf_card.get("type_line", "Unknown").replace("—", "-"), + "colorIdentity": sf_card.get("color_identity", None), + "colors": sf_card.get("colors", []), + "power": sf_card.get("power", None), + "toughness": sf_card.get("toughness", None), + "layout": sf_card["layout"].replace("normal", ""), + "loyalty": sf_card.get("loyalty", None), + "artist": sf_card.get("artist", ""), + "flavor": sf_card.get("flavor_text", None), + "multiverseId": sf_card.get("multiverse_id", None), + "superTypes": super_types, + "types": types, + "subTypes": sub_types, + } + trice_cards.append(trice_card) + except Exception: + print(f"Unable to parse {sf_card.get('name')}") return trice_cards @@ -192,7 +204,9 @@ def open_header(card_xml_file: IO[Any]) -> None: card_xml_file.write( "\n" + "