2021-05-21 23:09:26 +12:00
|
|
|
import difflib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
import requests
|
2021-08-14 07:24:03 +12:00
|
|
|
|
2022-09-08 10:27:37 +12:00
|
|
|
from rare.lgndr.core import LegendaryCore
|
2022-02-17 12:04:53 +13:00
|
|
|
from rare.utils.paths import data_dir, cache_dir
|
2021-08-08 10:49:27 +12:00
|
|
|
|
2021-05-21 23:09:26 +12:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-02-14 21:20:59 +13:00
|
|
|
def get_rating(core: LegendaryCore, app_name: str):
|
2022-04-05 07:02:01 +12:00
|
|
|
global __grades_json
|
|
|
|
if __grades_json is None:
|
2022-09-08 10:27:37 +12:00
|
|
|
if os.path.exists(p := os.path.join(data_dir(), "steam_ids.json")):
|
2022-04-05 07:02:01 +12:00
|
|
|
grades = json.loads(open(p).read())
|
|
|
|
__grades_json = grades
|
|
|
|
else:
|
|
|
|
grades = {}
|
|
|
|
__grades_json = grades
|
2021-08-14 07:24:03 +12:00
|
|
|
else:
|
2022-04-05 07:02:01 +12:00
|
|
|
grades = __grades_json
|
2021-08-14 07:24:03 +12:00
|
|
|
|
|
|
|
if not grades.get(app_name):
|
2022-02-26 06:43:27 +13:00
|
|
|
game = core.get_game(app_name)
|
2023-02-14 21:20:59 +13:00
|
|
|
try:
|
|
|
|
steam_id = get_steam_id(game.app_title)
|
|
|
|
grade = get_grade(steam_id)
|
|
|
|
except:
|
|
|
|
return "fail"
|
2021-12-24 22:09:50 +13:00
|
|
|
grades[app_name] = {"steam_id": steam_id, "grade": grade}
|
2022-09-08 10:27:37 +12:00
|
|
|
with open(os.path.join(data_dir(), "steam_ids.json"), "w") as f:
|
2021-08-14 07:24:03 +12:00
|
|
|
f.write(json.dumps(grades))
|
|
|
|
f.close()
|
|
|
|
return grade
|
|
|
|
else:
|
|
|
|
return grades[app_name].get("grade")
|
|
|
|
|
|
|
|
|
2021-05-21 23:09:26 +12:00
|
|
|
# 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/"
|
2022-02-02 10:29:34 +13:00
|
|
|
res = requests.get(f"{url}{steam_code}.json")
|
2021-05-21 23:09:26 +12:00
|
|
|
try:
|
|
|
|
lista = json.loads(res.text)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
return "fail"
|
|
|
|
|
2021-12-24 22:09:50 +13:00
|
|
|
return lista["tier"]
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
|
|
|
|
def load_json() -> dict:
|
2022-09-08 10:27:37 +12:00
|
|
|
file = os.path.join(cache_dir(), "game_list.json")
|
2021-08-14 07:24:03 +12:00
|
|
|
if not os.path.exists(file):
|
2021-05-21 23:09:26 +12:00
|
|
|
response = requests.get(url)
|
|
|
|
steam_ids = json.loads(response.text)["applist"]["apps"]
|
|
|
|
ids = {}
|
|
|
|
for game in steam_ids:
|
|
|
|
ids[game["name"]] = game["appid"]
|
|
|
|
|
2021-08-14 07:24:03 +12:00
|
|
|
with open(file, "w") as f:
|
2021-05-21 23:09:26 +12:00
|
|
|
f.write(json.dumps(ids))
|
|
|
|
f.close()
|
|
|
|
return ids
|
|
|
|
else:
|
2021-08-14 07:24:03 +12:00
|
|
|
return json.loads(open(file, "r").read())
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
|
2021-08-14 07:24:03 +12:00
|
|
|
def get_steam_id(title: str):
|
2022-09-08 10:27:37 +12:00
|
|
|
file = os.path.join(cache_dir(), "game_list.json")
|
2021-08-14 07:24:03 +12:00
|
|
|
# workarounds for satisfactory
|
2021-05-21 23:09:26 +12:00
|
|
|
title = title.replace("Early Access", "").replace("Experimental", "").strip()
|
2022-04-05 07:02:01 +12:00
|
|
|
global __steam_ids_json
|
|
|
|
if __steam_ids_json is None:
|
|
|
|
if not os.path.exists(file):
|
|
|
|
response = requests.get(url)
|
|
|
|
ids = {}
|
|
|
|
steam_ids = json.loads(response.text)["applist"]["apps"]
|
|
|
|
for game in steam_ids:
|
|
|
|
ids[game["name"]] = game["appid"]
|
|
|
|
__steam_ids_json = ids
|
|
|
|
|
|
|
|
with open(file, "w") as f:
|
|
|
|
f.write(json.dumps(ids))
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
ids = json.loads(open(file, "r").read())
|
|
|
|
__steam_ids_json = ids
|
2021-05-21 23:09:26 +12:00
|
|
|
else:
|
2022-04-05 07:02:01 +12:00
|
|
|
ids = __steam_ids_json
|
|
|
|
|
2021-06-16 04:26:58 +12:00
|
|
|
if title in ids.keys():
|
|
|
|
steam_name = [title]
|
2021-05-21 23:09:26 +12:00
|
|
|
|
2021-06-16 04:26:58 +12:00
|
|
|
else:
|
|
|
|
steam_name = difflib.get_close_matches(title, ids.keys(), n=1)
|
2021-05-21 23:09:26 +12:00
|
|
|
if steam_name:
|
|
|
|
return ids[steam_name[0]]
|
|
|
|
else:
|
|
|
|
return 0
|
2021-08-14 07:24:03 +12:00
|
|
|
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
def check_time(): # this function check if it's time to update
|
2022-09-08 10:27:37 +12:00
|
|
|
file = os.path.join(cache_dir(), "game_list.json")
|
2022-04-05 07:02:01 +12:00
|
|
|
json_table = json.loads(open(file, "r").read())
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
today = date.today()
|
|
|
|
day = 0 # it controls how many days it's necessary for an update
|
2021-12-24 22:09:50 +13:00
|
|
|
for i in "ymd":
|
|
|
|
if i == "d":
|
2021-05-21 23:09:26 +12:00
|
|
|
day = 7
|
|
|
|
else:
|
|
|
|
day = 0
|
2021-12-24 22:09:50 +13:00
|
|
|
if int(today.strftime("%" + i)) > int(json_table["data"][i]) + day:
|
2021-05-21 23:09:26 +12:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|