1
0
Fork 0
mirror of synced 2024-06-22 04:20:25 +12:00

SteamGrades: Use orjson instead of python's implementation

This commit is contained in:
loathingKernel 2022-08-29 17:51:46 +03:00
parent 86c683835b
commit 5be24a4d89
No known key found for this signature in database
GPG key ID: CE0C72D0B53821FD

View file

@ -1,5 +1,5 @@
import difflib import difflib
import json import orjson
import os import os
from datetime import date from datetime import date
@ -20,7 +20,7 @@ def get_rating(core: LegendaryCore, app_name: str):
global __grades_json global __grades_json
if __grades_json is None: if __grades_json is None:
if os.path.exists(p := os.path.join(data_dir(), "steam_ids.json")): if os.path.exists(p := os.path.join(data_dir(), "steam_ids.json")):
grades = json.loads(open(p).read()) grades = orjson.loads(open(p).read())
__grades_json = grades __grades_json = grades
else: else:
grades = {} grades = {}
@ -37,8 +37,7 @@ def get_rating(core: LegendaryCore, app_name: str):
return "fail" return "fail"
grades[app_name] = {"steam_id": steam_id, "grade": grade} grades[app_name] = {"steam_id": steam_id, "grade": grade}
with open(os.path.join(data_dir(), "steam_ids.json"), "w") as f: with open(os.path.join(data_dir(), "steam_ids.json"), "w") as f:
f.write(json.dumps(grades)) f.write(orjson.dumps(grades).decode("utf-8"))
f.close()
return grade return grade
else: else:
return grades[app_name].get("grade") return grades[app_name].get("grade")
@ -52,8 +51,8 @@ def get_grade(steam_code):
url = "https://www.protondb.com/api/v1/reports/summaries/" url = "https://www.protondb.com/api/v1/reports/summaries/"
res = requests.get(f"{url}{steam_code}.json") res = requests.get(f"{url}{steam_code}.json")
try: try:
lista = json.loads(res.text) lista = orjson.loads(res.text)
except json.decoder.JSONDecodeError: except orjson.JSONDecodeError:
return "fail" return "fail"
return lista["tier"] return lista["tier"]
@ -63,17 +62,16 @@ def load_json() -> dict:
file = os.path.join(cache_dir(), "game_list.json") file = os.path.join(cache_dir(), "game_list.json")
if not os.path.exists(file): if not os.path.exists(file):
response = requests.get(url) response = requests.get(url)
steam_ids = json.loads(response.text)["applist"]["apps"] steam_ids = orjson.loads(response.text)["applist"]["apps"]
ids = {} ids = {}
for game in steam_ids: for game in steam_ids:
ids[game["name"]] = game["appid"] ids[game["name"]] = game["appid"]
with open(file, "w") as f: with open(file, "w") as f:
f.write(json.dumps(ids)) f.write(orjson.dumps(ids).decode("utf-8"))
f.close()
return ids return ids
else: else:
return json.loads(open(file, "r").read()) return orjson.loads(open(file, "r").read())
def get_steam_id(title: str): def get_steam_id(title: str):
@ -85,16 +83,15 @@ def get_steam_id(title: str):
if not os.path.exists(file): if not os.path.exists(file):
response = requests.get(url) response = requests.get(url)
ids = {} ids = {}
steam_ids = json.loads(response.text)["applist"]["apps"] steam_ids = orjson.loads(response.text)["applist"]["apps"]
for game in steam_ids: for game in steam_ids:
ids[game["name"]] = game["appid"] ids[game["name"]] = game["appid"]
__steam_ids_json = ids __steam_ids_json = ids
with open(file, "w") as f: with open(file, "w") as f:
f.write(json.dumps(ids)) f.write(orjson.dumps(ids).decode("utf-8"))
f.close()
else: else:
ids = json.loads(open(file, "r").read()) ids = orjson.loads(open(file, "r").read())
__steam_ids_json = ids __steam_ids_json = ids
else: else:
ids = __steam_ids_json ids = __steam_ids_json
@ -112,7 +109,7 @@ def get_steam_id(title: str):
def check_time(): # this function check if it's time to update def check_time(): # this function check if it's time to update
file = os.path.join(cache_dir(), "game_list.json") file = os.path.join(cache_dir(), "game_list.json")
json_table = json.loads(open(file, "r").read()) json_table = orjson.loads(open(file, "r").read())
today = date.today() today = date.today()
day = 0 # it controls how many days it's necessary for an update day = 0 # it controls how many days it's necessary for an update