From 3dd54f59d8958d7990970caa565194017a6088d6 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Sat, 25 Mar 2023 14:34:46 +0100 Subject: [PATCH] remove writing of json files (#295) Co-authored-by: tooomm --- .github/CONTRIBUTING.md | 2 +- .github/workflows/deploy.yml | 2 +- README.md | 6 +-- magic_spoiler/__main__.py | 83 +----------------------------------- setup.py | 4 +- 5 files changed, 8 insertions(+), 89 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 17259cdf..b82e61e2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -12,7 +12,7 @@ It only takes a few days - be patient. - If the card is a legitimate spoiler and it isn't showing up yet, you can request it by [contacting the Scryfall support](https://scryfall.com/contact) and let them know. Make sure to link the official spoiler source in your report. - If the card shouldn't exist at all, let the Scryfall team know as well, please. -What you should **NOT** do however, is to submit PR's to our files branch and fix the xml/json files there directly.
+What you should **NOT** do however, is to submit PR's to our files branch and fix the xml files there directly.
You have to provide updates to Scryfall as all other changes would get overridden again. diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e9dbf5d2..442647b6 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -71,7 +71,7 @@ jobs: run: | git config user.name github-actions git config user.email github-actions@github.com - git add "*.xml" "*.json" SpoilerSeasonEnabled + git add "*.xml" SpoilerSeasonEnabled git commit -m "Deploy: $GITHUB_SHA" git push deploy_commit=`git rev-parse HEAD` diff --git a/README.md b/README.md index 4179c5d7..7c038fa4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Magic-Spoiler is a Python script to query the [Scryfall](https://scryfall.com >**Enable "Download Spoilers Automatically" in `Cockatrice → Settings → Card Sources → Spoilers` to get updates automatically pushed to your client!**
You can also [add the desired .xml file(s) to your customsets folder manually](https://github.com/Cockatrice/Cockatrice/wiki/Custom-Cards-&-Sets#to-add-custom-sets-follow-these-steps) to make Cockatrice use it. -Just looking for XML or JSON files? [They are in our `files` branch!](https://github.com/Cockatrice/Magic-Spoiler/tree/files) +Just looking for XML files? [They are in our `files` branch!](https://github.com/Cockatrice/Magic-Spoiler/tree/files) When run by our CI, the script automatically updates the files and uploads new versions to this branch. ([History of changes](https://github.com/Cockatrice/Magic-Spoiler/commits/files))
GitHub Actions are scheduled to autoamtically run on a daily basis. @@ -43,5 +43,5 @@ All XML and JSON spoiler files are written to the `out/` directory: | File Name | Content | |:--|:--| -| `spoiler.xml`, `spoiler.json` | files contain **all** currently available spoilers from different **sets** | -| `{SET_CODE}.xml`, `{SET_CODE}.json` | files contain just the spoiler available for this **single set** | +| `spoiler.xml` | file contains **all** currently available spoilers from different **sets** | +| `{SET_CODE}.xml` | files contain just the spoiler available for this **single set** | diff --git a/magic_spoiler/__main__.py b/magic_spoiler/__main__.py index 0c2671df..da6969d8 100644 --- a/magic_spoiler/__main__.py +++ b/magic_spoiler/__main__.py @@ -121,7 +121,7 @@ def build_types(sf_card: Dict[str, Any]) -> Tuple[List[str], str, List[str]]: def scryfall2mtgjson(scryfall_cards: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """ - Convert SF cards to MTGJSON format for dispatching + Convert SF cards to MTGJSON v4 format for dispatching :param scryfall_cards: List of Scryfall cards :return: MTGJSON card list """ @@ -426,53 +426,6 @@ def write_spoilers_xml(trice_dicts: Dict[str, List[Dict[str, Any]]]) -> bool: return True -def write_spoilers_json(trice_dicts: Dict[str, List[Dict[str, Any]]]) -> bool: - """ - Dump the JSON into a spoiler file - :param trice_dicts: All spoiled cards - :return: Written successfully - """ - if not trice_dicts: - return False - - output_file_path = OUTPUT_TMP_DIR.joinpath("spoiler.json") - - OUTPUT_TMP_DIR.mkdir(parents=True, exist_ok=True) - with output_file_path.open("w", encoding="utf-8") as f: - json.dump(trice_dicts, f, sort_keys=True, indent=4) - - # If content didn't change, discard newest creation - old_xml_location = str(OUTPUT_DIR.joinpath("spoiler.json")) - if compare_json_content(str(output_file_path), old_xml_location): - print("No new data in spoiler.json, skipping replacement") - return False - - # Move new version to old location - print("Changes detected, replacing spoiler.json with updated version") - shutil.move(str(output_file_path), old_xml_location) - return True - - -def compare_json_content(f1: str, f2: str) -> bool: - """ - Compare the contents of two JSON files and report - if the contents are the same, minus comments - :param f1: File 1 - :param f2: File 2 - :return: Is file content, minus comments, the same? - """ - file1 = pathlib.Path(f1) - file2 = pathlib.Path(f2) - - if file1.is_file() and file2.is_file(): - f1_hash = hashlib.sha512(file1.open("rb").read()).hexdigest() - f2_hash = hashlib.sha512(file2.open("rb").read()).hexdigest() - - return f1_hash == f2_hash - - return False - - def compare_xml_content(a: str, b: str) -> bool: """ Compare the contents of two XML files and report @@ -534,38 +487,6 @@ def write_set_xml(trice_dict: List[Dict[str, Any]], set_obj: Dict[str, str]) -> return True -def write_set_json(trice_dict: List[Dict[str, Any]], set_obj: Dict[str, str]) -> bool: - """ - Dump the JSON into a spoiler file - :param trice_dict: Cards - :param set_obj: Set Information - :return: Written successfully - """ - if not trice_dict: - return False - - output_file_path = OUTPUT_TMP_DIR.joinpath("{}.json".format(set_obj["code"])) - - OUTPUT_TMP_DIR.mkdir(parents=True, exist_ok=True) - with output_file_path.open("w", encoding="utf-8") as f: - json.dump(trice_dict, f, sort_keys=True, indent=4) - - # 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"])) - return False - - # Move new version to old location - print( - "Changes detected, replacing {}.json with updated version".format( - set_obj["code"] - ) - ) - shutil.move(str(output_file_path), old_xml_location) - return True - - def get_spoiler_sets() -> List[Dict[str, str]]: """ Download Sf sets and mark spoiler sets @@ -639,7 +560,6 @@ def main() -> None: # Write SET.xml changed |= write_set_xml(trice_dict, set_info) - changed |= write_set_json(trice_dict, set_info) # Save for spoiler.xml spoiler_xml[set_info["code"]] = trice_dict @@ -647,7 +567,6 @@ def main() -> None: if spoiler_xml: # Write out the spoiler.xml file changed |= write_spoilers_xml(spoiler_xml) - changed |= write_spoilers_json(spoiler_xml) # Cleanup outdated stuff that's not necessary changed |= delete_old_files() diff --git a/setup.py b/setup.py index 33b32f72..23fab290 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setuptools.setup( author="Zach Halpern", author_email="zach@cockatrice.us", url="https://github.com/Cockatrice/Magic-Spoiler/", - description="Build JSON and XML files for distribution of MTG spoiler cards", + description="Build XML files for distribution of MTG spoiler cards", long_description=open("README.md", "r").read(), long_description_content_type="text/markdown", license="GPL-3.0", @@ -19,6 +19,6 @@ setuptools.setup( "Programming Language :: Python :: 3.7", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", ], - keywords="Magic: The Gathering, MTG, JSON, Card Games, Collectible, Trading Cards", + keywords="Magic: The Gathering, MTG, XML, Card Games, Collectible, Trading Cards", packages=setuptools.find_packages(), )