Stub RetroAchievements integration

This commit is contained in:
theo@manjaro 2023-12-11 16:55:33 -05:00
parent fe6ebce3a9
commit 2ab75b0238
2 changed files with 67 additions and 0 deletions

65
ra.py Normal file
View File

@ -0,0 +1,65 @@
import json
import requests
from racredentials import user, key
BASE_URL = "https://retroachievements.org/API/"
WEB_URL = "https://retroachievements.org"
class Award:
def __init__(self, awarddict):
self.name = awarddict["Title"]
self.type = awarddict["AwardType"]
self.date = awarddict["AwardedAt"]
self.iconurl = WEB_URL + awarddict["ImageIcon"]
self.gameurl = WEB_URL + "/game/" + str(awarddict["AwardData"])
if awarddict["ConsoleName"] == "Events":
self.name = "~Event~ " + self.name
self.url = self.genurl()
def genurl(self):
# lower the string
name = self.name.lower()
# Only keep letters, and replace spaces
stripped = ""
for c in name.strip():
if ord("a") <= ord(c) <= ord("z") or ord("0") <= ord(c) <= ord("9"):
stripped += c
elif c == " ":
stripped += "-"
# delete multiple following dashes
splitted = stripped.split("-")
while splitted.count("") > 0:
splitted.remove("")
stripped = "-".join(splitted)
# Concatenate date
date = self.date.split("T")[0]
return f"{date}-{stripped}.gmi"
def request(path):
return json.loads(requests.get(BASE_URL + path).text)
def getAwards(user, key):
response = request(f"API_GetUserAwards.php?z={user}&y={key}&u={user}")[
"VisibleUserAwards"
]
indexed = {}
# Returns only the mastered award
# in case there's both a completed and master award
for award in [Award(x) for x in response]:
if award.name in indexed.keys():
if indexed[award.name].type == "Game Beaten":
indexed[award.name] = award
else:
indexed[award.name] = award
sorted = list(indexed.values())
sorted.sort(key=lambda x: x.date)
return sorted
if __name__ == "__main__":
awards = getAwards(user, key)
for award in awards:
print(award.name, award.url)

2
racredentials.py Normal file
View File

@ -0,0 +1,2 @@
user = "REPLACE THIS"
key = "REPLACE THIS"