Skip to content

Instantly share code, notes, and snippets.

@Gemba
Last active September 20, 2023 21:00
Show Gist options
  • Save Gemba/13f0accddcecd68a356721ebac020d76 to your computer and use it in GitHub Desktop.
Save Gemba/13f0accddcecd68a356721ebac020d76 to your computer and use it in GitHub Desktop.
Fix Mobygames score (0.0 ... 10.0 scale) to ES gamelist score (0.0 ... 1.0 scale) in Skyscraper's db.xml file. Skyscraper versions before 3.8.1 did divide by 5 and not by 10.
#! /usr/bin/env python3
# Fixes Mobygames score (0.0 ... 10.0 scale) to ES gamelist rating (0.0 ... 1.0
# scale) in db.xml. Skyscraper since v2.6.0 (Aug 2018) calculates ES
# gamelist.xml ratings from mobygames score by dividing by 5.0 which result in
# values between 0.0 and 2.0 and not between zero and one. Maybe the
# implementation was right in the beginneing and then the Mobygames API has been
# changed.
# NB: A fixed Skyscraper version can be found at
# https://github.com/Gemba/skyscraper
# (c) 2023 Gemba @ GitHub. License: MIT
import sys
import xmltodict
from pathlib import Path
def fix_score(row):
row["#text"] = f'{float(row["#text"]) / 2.0}'
return row
if __name__ == "__main__":
if len(sys.argv) != 2 or not Path(sys.argv[1]).exists():
print(
"[!] Provide a Skyscraper cache db.xml filename.\n"
f" Usage: python3 {sys.argv[0]} <path/to/db-xml-file>\n"
" Inputfile will not be altered, a new XML file will be written."
)
sys.exit(1)
db_xml = sys.argv[1]
db_out_xml = Path(db_xml).parent / "db_score_fixed.xml"
print(f"[*] Reading '{db_xml}'.")
with open(db_xml, "rb") as dbxml:
db_dict = xmltodict.parse(dbxml, force_list=("resource",))
if not db_dict["resources"]:
print(f"[-] Empty XML file.")
sys.exit()
rows = [
fix_score(r)
for r in db_dict["resources"]["resource"]
if r["@type"] == "rating"
and r["@source"] == "mobygames"
and float(r["#text"]) > 1.0
]
if len(rows):
print(f"[*] Fixed {len(rows)} ratings(s).")
with open(db_out_xml, "w") as dbxml:
xmltodict.unparse(db_dict, output=dbxml, pretty=True, indent=" ")
print(f"[+] Corrected file at '{db_out_xml}'.")
else:
print(f"[*] No rows affected. No file written.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment