Files
olympic-discipline-quiz/get_data/clean-results.py
Lennart J. Kurzweg (Nx2) 9d75757fec init
2024-08-16 19:28:51 +02:00

45 lines
2.3 KiB
Python

import subprocess
import json
with open("./results.json", "r") as f:
sports = json.load(f)
ret = {}
for sport in sports:
ret[sport] = {}
for event in sports[sport]:
ret[sport][event] = {}
ret[sport][event]["type"] = "team" if sports[sport][event][0]["participant"]["__typename"] == "GameTeam" else "single"
ret[sport][event]["participants"] = []
print(f"{sport} >> {event} ".ljust(50), end="")
for i in range(len(sports[sport][event])):
ret[sport][event]["participants"].append({})
try:
ret[sport][event]["participants"][i]["country"] = sports[sport][event][i]["noc"]["name"]
except:
try:
ret[sport][event]["participants"][i]["country"] = sports[sport][event][i]["participant"]["countryObject"]["name"]
except:
ret[sport][event]["participants"][i]["country"] = "None"
if ret[sport][event]["type"] == "single":
ret[sport][event]["participants"][i]["athlete"] = {}
ret[sport][event]["participants"][i]["athlete"]["meta_url"] = sports[sport][event][i]["participant"]["meta"]["url"]
ret[sport][event]["participants"][i]["athlete"]["name"] = f'{sports[sport][event][i]["participant"]["name"]} {sports[sport][event][i]["participant"]["surname"]}'
try:
template = sports[sport][event][i]["participant"]["thumbnail"]["urlTemplate"]
img = f'https://img.olympics.com/images/image/private/t_1-1_300/f_auto{template[template.index("}")+1:]}'
except:
img = f'https://gstatic.olympics.com/s1/f_auto/static/light/flag/paris-2024/olympic/3x2/{sports[sport][event][i]["participant"]["countryObject"]["triLetterCode"]}.png'
ret[sport][event]["participants"][i]["athlete"]["image"] = img
else:
try:
ret[sport][event]["participants"][i]["img"] = f'https://gstatic.olympics.com/s1/f_auto/static/light/flag/paris-2024/olympic/3x2/{sports[sport][event][i]["noc"]["code"]}.png'
except:
ret[sport][event]["participants"][i]["img"] = None
print("o", end="")
print()
with open("./data.json", "w") as f:
json.dump(ret, f, ensure_ascii=False, indent=4, sort_keys=True)