diff --git a/magic_spoiler/__main__.py b/magic_spoiler/__main__.py index 7abaa36d..dea820b3 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 @@ -196,10 +208,10 @@ def open_header(card_xml_file: IO[Any]) -> None: + " THIS FILE IS AUTOMATICALLY GENERATED & ALL EDITS WILL BE OVERRIDDEN.\n" + " -->\n" + "\n" - + "Cockatrice/Magic-Spoiler\n" - + "" + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " (UTC)" + "\n" - + "https://github.com/Cockatrice/Magic-Spoiler/blob/files/spoiler.xml\n" - + "" + datetime.datetime.utcnow().strftime("%Y%m%d") + "\n" + + " Cockatrice/Magic-Spoiler\n" + + " " + datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " (UTC)" + "\n" + + " https://github.com/Cockatrice/Magic-Spoiler/blob/files/spoiler.xml\n" + + " " + datetime.datetime.utcnow().strftime("%Y%m%d") + "\n" + "\n" + "\n" ) @@ -215,7 +227,7 @@ def fill_header_sets(card_xml_file: IO[Any], set_obj: Dict[str, str]) -> None: "\n" "" + set_obj["code"] + "\n" "" + set_obj["name"] + "\n" - "" + set_obj["set_type"].replace("_"," ").title() + "\n" + "" + set_obj["set_type"].replace("_", " ").title() + "\n" "" + set_obj["released_at"] + "\n" "\n" ) @@ -488,7 +500,11 @@ def write_set_xml(trice_dict: List[Dict[str, Any]], set_obj: Dict[str, str]) -> return # Move new version to old location - print("Changes detected, replacing {}.xml with updated version".format(set_obj["code"])) + print( + "Changes detected, replacing {}.xml with updated version".format( + set_obj["code"] + ) + ) shutil.move(card_xml_file.name, old_xml_location) @@ -510,13 +526,15 @@ def write_set_json(trice_dict: List[Dict[str, Any]], set_obj: Dict[str, str]) -> # If content didn't change, discard newest creation old_xml_location = str(OUTPUT_DIR.joinpath("{}.json".format(set_obj["code"]))) if compare_json_content(str(output_file_path), old_xml_location): - print( - "No new data in {}.json, skipping replacement".format(set_obj["code"]) - ) + print("No new data in {}.json, skipping replacement".format(set_obj["code"])) return # Move new version to old location - print("Changes detected, replacing {}.json with updated version".format(set_obj["code"])) + print( + "Changes detected, replacing {}.json with updated version".format( + set_obj["code"] + ) + ) shutil.move(str(output_file_path), old_xml_location)