1
0
Fork 0
mirror of synced 2024-09-14 16:38:00 +12:00
Rare/rare/utils/steam_grades.py

90 lines
2.6 KiB
Python
Raw Normal View History

import difflib
import os
from datetime import datetime
from typing import Tuple, Optional
import orjson
import requests
from rare.lgndr.core import LegendaryCore
from rare.utils.paths import cache_dir
replace_chars = ",;.:-_ "
url = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"
2022-04-05 07:02:01 +12:00
__steam_ids_json = None
__grades_json = None
__active_download = False
2022-04-05 07:02:01 +12:00
def get_rating(core: LegendaryCore, app_name: str) -> Tuple[int, str]:
game = core.get_game(app_name)
try:
steam_id = get_steam_id(game.app_title)
if not steam_id:
raise Exception
grade = get_grade(steam_id)
except Exception as e:
return 0, "fail"
else:
return steam_id, grade
# you should iniciate the module with the game's steam code
def get_grade(steam_code):
if steam_code == 0:
return "fail"
steam_code = str(steam_code)
2021-12-24 22:09:50 +13:00
url = "https://www.protondb.com/api/v1/reports/summaries/"
res = requests.get(f"{url}{steam_code}.json")
try:
2023-09-24 06:21:58 +13:00
lista = orjson.loads(res.text) # pylint: disable=maybe-no-member
except orjson.JSONDecodeError: # pylint: disable=maybe-no-member
return "fail"
2021-12-24 22:09:50 +13:00
return lista["tier"]
def load_json() -> dict:
file = os.path.join(cache_dir(), "game_list.json")
mod_time = datetime.fromtimestamp(os.path.getmtime(file))
elapsed_time = abs(datetime.now() - mod_time)
global __active_download
if __active_download:
return {}
if not os.path.exists(file) or elapsed_time.days > 7:
__active_download = True
response = requests.get(url)
__active_download = False
2023-09-24 06:21:58 +13:00
steam_ids = orjson.loads(response.text)["applist"]["apps"] # pylint: disable=maybe-no-member
ids = {}
for game in steam_ids:
ids[game["name"]] = game["appid"]
with open(file, "w") as f:
2023-09-24 06:21:58 +13:00
f.write(orjson.dumps(ids).decode("utf-8")) # pylint: disable=maybe-no-member
return ids
else:
2023-09-24 06:21:58 +13:00
return orjson.loads(open(file, "r").read()) # pylint: disable=maybe-no-member
def get_steam_id(title: str) -> int:
# workarounds for satisfactory
# FIXME: This has to be made smarter.
title = title.replace("Early Access", "").replace("Experimental", "").strip()
# title = title.split(":")[0]
# title = title.split("-")[0]
2022-04-05 07:02:01 +12:00
global __steam_ids_json
if __steam_ids_json is None:
__steam_ids_json = load_json()
ids = __steam_ids_json
2022-04-05 07:02:01 +12:00
if title in ids.keys():
steam_name = [title]
else:
steam_name = difflib.get_close_matches(title, ids.keys(), n=1, cutoff=0.5)
if steam_name:
return ids[steam_name[0]]
else:
return 0