71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
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"):
|
|
stripped += c
|
|
elif ord("0") <= ord(c) <= ord("9"):
|
|
stripped += c
|
|
else:
|
|
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}")
|
|
if "VisibleUserAwards" in response.keys():
|
|
response = response["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
|
|
else:
|
|
print(response)
|
|
return []
|
|
|
|
|
|
if __name__ == "__main__":
|
|
awards = getAwards(user, key)
|
|
for award in awards:
|
|
print(award.name, award.url)
|