59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import json
|
|
import os
|
|
import re
|
|
|
|
COVERART_DIR = "/Users/matt/Desktop/VideoGameList/Coverart"
|
|
GAMES_JSON = "/Users/matt/Desktop/VideoGameList/games.json"
|
|
|
|
def title_to_filename(title):
|
|
"""Transform a game title to its expected Coverart filename."""
|
|
transformed = title.replace("'", "_").replace(":", "_")
|
|
return f"{transformed}-01.png"
|
|
|
|
def main():
|
|
# 1. Build a set of available Coverart filenames
|
|
coverart_files = set(os.listdir(COVERART_DIR))
|
|
print(f"Coverart files available: {len(coverart_files)}")
|
|
|
|
# 2. Load games.json
|
|
with open(GAMES_JSON, "r", encoding="utf-8") as f:
|
|
games = json.load(f)
|
|
print(f"Games in JSON: {len(games)}")
|
|
|
|
matched = []
|
|
unmatched = []
|
|
cleared = []
|
|
|
|
for game in games:
|
|
title = game.get("title", game.get("name", ""))
|
|
expected = title_to_filename(title)
|
|
current = game.get("cover_art_local", "")
|
|
|
|
if expected in coverart_files:
|
|
game["cover_art_local"] = f"coverart/{expected}"
|
|
matched.append(title)
|
|
else:
|
|
# Clear any stale metadata/ paths; leave truly empty ones alone
|
|
if current.startswith("metadata/"):
|
|
game["cover_art_local"] = ""
|
|
cleared.append(title)
|
|
unmatched.append(title)
|
|
|
|
# 3. Save updated games.json
|
|
with open(GAMES_JSON, "w", encoding="utf-8") as f:
|
|
json.dump(games, f, indent=2, ensure_ascii=False)
|
|
|
|
# 4. Report
|
|
print(f"\nMatched : {len(matched)}")
|
|
print(f"Unmatched : {len(unmatched)}")
|
|
print(f"Cleared (metadata/ paths removed): {len(cleared)}")
|
|
|
|
if unmatched:
|
|
print("\nUnmatched titles:")
|
|
for t in unmatched:
|
|
expected = title_to_filename(t)
|
|
print(f" Title : {t!r}")
|
|
print(f" Expected : {expected!r}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|