From 9c7e80615535b17d2db1516474e3581236a18dc7 Mon Sep 17 00:00:00 2001 From: Akitake Date: Tue, 2 Jul 2024 22:44:15 +0200 Subject: [PATCH] Add script used to update datecodes in json files --- dateCodes.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 dateCodes.py diff --git a/dateCodes.py b/dateCodes.py new file mode 100644 index 0000000..03b9b4d --- /dev/null +++ b/dateCodes.py @@ -0,0 +1,58 @@ +import re +import json + +# Matches SUPPORTED.md lines with game information on them, splitting the needed data into groups +regex = re.compile(r"\| ([^|]*) \| ([^|]*) \| ([^|]*) \| \[.*?\]\((.*?)\) \|") + +# Updates every entry in a json file with the dateCode +def update_json_file(json_path, dateCode): + try: + with open(json_path, "r", encoding="utf-8") as json_file: + data = json.load(json_file) + + entries = 0 + if isinstance(data, list): + for entry in data: + if "gameCode" in entry: + new_entry = {} + for key, value in entry.items(): + new_entry[key] = value + if key == "gameCode": + new_entry["dateCode"] = dateCode + entry.clear() + entry.update(new_entry) + entries += 1 + + with open(json_path, "w", encoding="utf-8") as json_file: + json.dump(data, json_file, indent=4) + + print(f"'{json_path}' -> \"dateCode\": \"{dateCode}\"") + return entries + except Exception as e: + print(e) + +# Main function iterating through SUPPORTED.md, finding relevant lines, processing json files for each game version +def iterate(file_path): + try: + entries = 0 + count = 0 + with open(file_path, "r", encoding="utf-8") as file: + for line in file: + match = regex.search(line) + if match: + variant = match.group(1).strip() + dateCode = match.group(3).strip() + if "-" in variant: + dateCode_with_variant = f"{dateCode} ({variant.split("-")[1]})" + else: + dateCode_with_variant = dateCode + json_path = match.group(4).strip() + entries += update_json_file(json_path, dateCode_with_variant) + count += 1 + return [ entries, count ] + except Exception as e: + print(f"Error processing file '{file_path}': {e}") + + +entries, count = iterate("SUPPORTED.md") +print(f"\nUpdated {entries} entries over {count} files.") \ No newline at end of file