1
0
Fork 0
mirror of synced 2024-06-02 18:54:41 +12:00

Update steam ratings (Only update if need)

This commit is contained in:
Dummerle 2021-08-13 21:24:03 +02:00
parent 6fbc3eaebe
commit 5dcaa3f8af
9 changed files with 112 additions and 173 deletions

View file

@ -1,5 +1,26 @@
import os
__version__ = "1.4.1"
style_path = os.path.join(os.path.dirname(__file__), "styles/")
lang_path = os.path.join(os.path.dirname(__file__), "languages/")
# Cache Directory: Store images
if p := os.getenv("XDG_CACHE_HOME"):
cache_dir = os.path.join(p, "rare/cache")
elif os.name == "nt":
cache_dir = os.path.expandvars("%APPDATA%/rare/cache")
else:
cache_dir = os.path.expanduser("~/.cache/rare/")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
# Data Directory: Images
if p := os.getenv("XDG_DATA_HOME"):
data_dir = os.path.join(p, "rare")
if os.name == "nt":
data_dir = os.path.expandvars("%APPDATA%/rare/")
else:
data_dir = os.path.expanduser("~/.local/share/rare/")
if not os.path.exists(data_dir):
os.makedirs(data_dir)

View file

@ -1,16 +1,13 @@
import json
import os
import platform
from logging import getLogger
from PyQt5.QtCore import QThread, pyqtSignal, QSettings, Qt
from PyQt5.QtCore import QThread, pyqtSignal, Qt
from PyQt5.QtWidgets import QDialog
from requests.exceptions import ConnectionError
from custom_legendary.core import LegendaryCore
from rare.components.dialogs.login import LoginDialog
from rare.ui.components.dialogs.launch_dialog import Ui_LaunchDialog
from rare.utils import steam_grades
from rare.utils.utils import download_images
logger = getLogger("Login")
@ -28,45 +25,6 @@ class ImageThread(QThread):
self.download_progess.emit(100)
class SteamThread(QThread):
progress = pyqtSignal(int)
action = pyqtSignal(str)
def __init__(self, core: LegendaryCore, parent):
super(SteamThread, self).__init__(parent)
self.core = core
def run(self) -> None:
gamelist = self.core.get_game_list(True)
if not os.path.exists(os.path.expanduser("~/.cache/rare/game_list.json")):
self.action.emit(self.tr("Getting data from ProtonDB"))
steam_grades.upgrade_all([(i.app_title, i.app_name) for i in gamelist], self.progress)
self.progress.emit(99)
self.action.emit(self.tr("Checking Games for data"))
grades = json.load(open(os.path.expanduser("~/.cache/rare/game_list.json")))
ids = json.load(open(os.path.expanduser("~/.cache/rare/steam_ids.json")))
for game in gamelist:
if not grades.get(game.app_name):
steam_id = steam_grades.get_steam_id(game.app_title, ids)
grade = steam_grades.get_grade(steam_id)
grades[game.app_name] = {
"steam_id": steam_id,
"grade": grade
}
if not grades[game.app_name].get("steam_id"):
grades[game.app_name]["steam_id"] = steam_grades.get_steam_id(game.app_title, ids)
if not grades[game.app_name].get("grade"):
grades[game.app_name]["grade"] = steam_grades.get_grade(game.app_title)
with open(os.path.expanduser("~/.cache/rare/game_list.json"), "w") as f:
f.write(json.dumps(grades))
f.close()
self.action.emit("Ready")
self.progress.emit(100)
class LoginThread(QThread):
login = pyqtSignal()
start_app = pyqtSignal(bool) # offline
@ -100,10 +58,7 @@ class LaunchDialog(QDialog, Ui_LaunchDialog):
self.setupUi(self)
self.offline = offline
self.setAttribute(Qt.WA_DeleteOnClose, True)
if platform.system() == "Windows":
self.finished = True
self.steam_info.setVisible(False)
self.steam_prog_bar.setVisible(False)
self.core = core
if not offline:
self.login_thread = LoginThread(core)
@ -135,33 +90,15 @@ class LaunchDialog(QDialog, Ui_LaunchDialog):
self.img_thread.download_progess.connect(self.update_image_progbar)
self.img_thread.finished.connect(self.finish)
self.img_thread.start()
# not disabled and not windows
if (not QSettings().value("disable_protondb", False, bool)) and (not platform.system() == "Windows"):
self.steam_thread = SteamThread(self.core, self)
self.steam_thread.progress.connect(self.update_steam_prog_bar)
self.steam_thread.action.connect(lambda x: self.steam_info.setText(x))
self.steam_thread.finished.connect(self.finish)
self.steam_thread.start()
else:
self.finished = True
self.steam_info.setVisible(False)
self.steam_prog_bar.setVisible(False)
else:
self.finished = True
self.finish()
def update_steam_prog_bar(self, value):
self.steam_prog_bar.setValue(value)
def update_image_progbar(self, i: int):
self.image_prog_bar.setValue(i)
def finish(self):
if self.finished:
self.image_info.setText(self.tr("Starting..."))
self.image_prog_bar.setValue(100)
self.steam_prog_bar.setValue(100)
self.start_app.emit(self.offline)
else:
self.finished = True
self.image_info.setText(self.tr("Starting..."))
self.image_prog_bar.setValue(100)
self.start_app.emit(self.offline)

View file

@ -1,4 +1,3 @@
import json
import os
import platform
@ -14,6 +13,7 @@ from rare.components.tabs.games.game_info.game_settings import GameSettings
from rare.ui.components.tabs.games.game_info.game_info import Ui_GameInfo
from rare.utils.extra_widgets import SideTabBar
from rare.utils.legendary_utils import VerifyThread
from rare.utils.steam_grades import SteamWorker
from rare.utils.utils import IMAGE_DIR, get_size
@ -58,6 +58,7 @@ class InfoTabs(QTabWidget):
self.parent().layout.setCurrentIndex(0)
class GameInfo(QWidget, Ui_GameInfo):
igame: InstalledGame
game: Game
@ -71,21 +72,14 @@ class GameInfo(QWidget, Ui_GameInfo):
self.setupUi(self)
self.core = core
self.ratings = {"platinum": self.tr("Platinum"),
"gold": self.tr("Gold"),
"silver": self.tr("Silver"),
"bronze": self.tr("Bronze"),
"fail": self.tr("Could not get grade"),
"pending": self.tr("Not enough reports")}
if os.path.exists(p := os.path.expanduser("~/.cache/rare/game_list.json")):
self.grade_table = json.load(open(p))
else:
self.grade_table = {}
if platform.system() == "Windows":
self.lbl_grade.setVisible(False)
self.grade.setVisible(False)
if platform.system() != "Windows":
self.steam_worker = SteamWorker(self.core)
self.steam_worker.rating_signal.connect(self.grade.setText)
self.game_actions_stack.setCurrentIndex(0)
self.game_actions_stack.resize(self.game_actions_stack.minimumSize())
@ -159,12 +153,10 @@ class GameInfo(QWidget, Ui_GameInfo):
self.install_size.setText(get_size(self.igame.install_size))
self.install_path.setText(self.igame.install_path)
if platform.system() != "Windows" and self.grade_table:
try:
grade = self.grade_table[app_name]["grade"]
except KeyError:
grade = "fail"
self.grade.setText(self.ratings[grade])
if platform.system() != "Windows":
self.grade.setText(self.tr("Loading"))
self.steam_worker.set_app_name(app_name)
self.steam_worker.start()
if len(self.verify_threads.keys()) == 0 or not self.verify_threads.get(app_name):
self.verify_widget.setCurrentIndex(0)

View file

@ -12,6 +12,7 @@ from custom_legendary.models.game import Game
from rare.ui.components.tabs.games.game_info.game_info import Ui_GameInfo
from rare.utils.extra_widgets import SideTabBar
from rare.utils.json_formatter import QJsonModel
from rare.utils.steam_grades import SteamWorker
from rare.utils.utils import IMAGE_DIR
@ -60,16 +61,9 @@ class UninstalledInfo(QWidget, Ui_GameInfo):
self.setupUi(self)
self.core = core
self.ratings = {"platinum": self.tr("Platinum"),
"gold": self.tr("Gold"),
"silver": self.tr("Silver"),
"bronze": self.tr("Bronze"),
"fail": self.tr("Could not get grade"),
"pending": self.tr("Not enough reports")}
if os.path.exists(p := os.path.expanduser("~/.cache/rare/game_list.json")):
self.grade_table = json.load(open(p))
else:
self.grade_table = {}
if platform.system() != "Windows":
self.steam_worker = SteamWorker(self.core)
self.steam_worker.rating_signal.connect(self.grade.setText)
if platform.system() == "Windows":
self.lbl_grade.setVisible(False)
@ -110,9 +104,7 @@ class UninstalledInfo(QWidget, Ui_GameInfo):
self.install_size.setText("N/A")
self.install_path.setText("N/A")
if platform.system() != "Windows" and self.grade_table:
try:
grade = self.grade_table[app_name]["grade"]
except KeyError:
grade = "fail"
self.grade.setText(self.ratings[grade])
if platform.system() != "Windows":
self.grade.setText(self.tr("Loading"))
self.steam_worker.set_app_name(app_name)
self.steam_worker.start()

View file

@ -7,6 +7,7 @@ from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QScrollArea, QWidget, QLabel, QVBoxLayout, QStackedWidget
from custom_legendary.core import LegendaryCore
from rare import data_dir
from rare.components.tabs.games.game_widgets.base_installed_widget import BaseInstalledWidget
from rare.components.tabs.games.game_widgets.installed_icon_widget import GameWidgetInstalled
from rare.components.tabs.games.game_widgets.installed_list_widget import InstalledListWidget
@ -73,7 +74,7 @@ class GameList(QStackedWidget):
self.list_layout = QVBoxLayout()
self.list_layout.addWidget(QLabel(self.info_text))
self.IMAGE_DIR = self.settings.value("img_dir", os.path.expanduser("~/.cache/rare/images"), str)
self.IMAGE_DIR = os.path.join(data_dir, "images")
self.updates = []
self.widgets = {}
if not self.offline:

View file

@ -8,7 +8,7 @@
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtWidgets
class Ui_LaunchDialog(object):
@ -27,13 +27,6 @@ class Ui_LaunchDialog(object):
self.image_info = QtWidgets.QLabel(LaunchDialog)
self.image_info.setObjectName("image_info")
self.verticalLayout.addWidget(self.image_info)
self.steam_prog_bar = QtWidgets.QProgressBar(LaunchDialog)
self.steam_prog_bar.setProperty("value", 0)
self.steam_prog_bar.setObjectName("steam_prog_bar")
self.verticalLayout.addWidget(self.steam_prog_bar)
self.steam_info = QtWidgets.QLabel(LaunchDialog)
self.steam_info.setObjectName("steam_info")
self.verticalLayout.addWidget(self.steam_info)
self.retranslateUi(LaunchDialog)
QtCore.QMetaObject.connectSlotsByName(LaunchDialog)
@ -43,7 +36,6 @@ class Ui_LaunchDialog(object):
LaunchDialog.setWindowTitle(_translate("LaunchDialog", "Launching Rare"))
self.title_label.setText(_translate("LaunchDialog", "<h2>Launching Rare</h2>"))
self.image_info.setText(_translate("LaunchDialog", "Downloading images"))
self.steam_info.setText(_translate("LaunchDialog", "Getting Steam grades"))
if __name__ == "__main__":

View file

@ -35,20 +35,6 @@
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="steam_prog_bar">
<property name="value">
<number>0</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="steam_info">
<property name="text">
<string>Getting Steam grades</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View file

@ -4,14 +4,60 @@ import os
from datetime import date
import requests
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import QThread, pyqtSignal
from custom_legendary.core import LegendaryCore
from rare import cache_dir, data_dir
replace_chars = ",;.:-_ "
file = os.path.expanduser("~/.cache/rare/game_list.json")
file = os.path.join(cache_dir, "game_list.json")
url = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"
class SteamWorker(QThread):
app_name = ""
rating_signal = pyqtSignal(str)
def __init__(self, core: LegendaryCore):
super(SteamWorker, self).__init__()
self.core = core
self.ratings = {"platinum": self.tr("Platinum"),
"gold": self.tr("Gold"),
"silver": self.tr("Silver"),
"bronze": self.tr("Bronze"),
"fail": self.tr("Could not get grade"),
"pending": self.tr("Could not get grade")
}
def set_app_name(self, app_name: str):
self.app_name = app_name
def run(self) -> None:
self.rating_signal.emit(self.ratings[get_rating(self.app_name, self.core)])
def get_rating(app_name: str, core: LegendaryCore):
if os.path.exists(p := os.path.join(data_dir, "steam_ids.json")):
grades = json.load(open(p))
else:
grades = {}
if not grades.get(app_name):
steam_id = get_steam_id(core.get_game(app_name).app_title)
grade = get_grade(steam_id)
grades[app_name] = {
"steam_id": steam_id,
"grade": grade
}
with open(os.path.join(data_dir, "steam_ids.json"), "w") as f:
f.write(json.dumps(grades))
f.close()
return grade
else:
return grades[app_name].get("grade")
# you should iniciate the module with the game's steam code
def get_grade(steam_code):
if steam_code == 0:
@ -28,56 +74,36 @@ def get_grade(steam_code):
def load_json() -> dict:
if not os.path.exists(p := os.path.expanduser("~/.cache/rare/steam_ids.json")):
if not os.path.exists(file):
response = requests.get(url)
steam_ids = json.loads(response.text)["applist"]["apps"]
ids = {}
for game in steam_ids:
ids[game["name"]] = game["appid"]
with open(os.path.expanduser(p), "w") as f:
with open(file, "w") as f:
f.write(json.dumps(ids))
f.close()
return ids
else:
return json.loads(open(os.path.expanduser("~/.cache/rare/steam_ids.json"), "r").read())
return json.loads(open(file, "r").read())
def upgrade_all(games, progress: pyqtSignal = None):
ids = load_json()
data = {}
for i, (title, app_name) in enumerate(games):
data[app_name] = {}
steam_id = get_steam_id(title, ids)
data[app_name] = {
"steam_id": steam_id,
"grade": get_grade(steam_id)}
if progress:
progress.emit(int(i / len(games) * 100))
with open(os.path.expanduser("~/.cache/rare/game_list.json"), "w") as f:
f.write(json.dumps(data))
f.close()
def get_steam_id(title: str, json_data: dict = None):
def get_steam_id(title: str):
# workarounds for satisfactory
title = title.replace("Early Access", "").replace("Experimental", "").strip()
if not json_data:
if not os.path.exists(p := os.path.expanduser("~/.cache/rare/steam_ids.json")):
response = requests.get(url)
ids = {}
steam_ids = json.loads(response.text)["applist"]["apps"]
for game in steam_ids:
ids[game["name"]] = game["appid"]
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"]
with open(os.path.expanduser(p), "w") as f:
f.write(json.dumps(steam_ids))
f.close()
else:
ids = json.loads(open(os.path.expanduser("~/.cache/rare/steam_ids.json"), "r").read())
with open(file, "w") as f:
f.write(json.dumps(ids))
f.close()
else:
ids = json_data
ids = json.loads(open(file, "r").read())
if title in ids.keys():
steam_name = [title]
else:
@ -86,15 +112,7 @@ def get_steam_id(title: str, json_data: dict = None):
return ids[steam_name[0]]
else:
return 0
# print(x)
# for game in steam_ids:
# num = difflib.SequenceMatcher(None, game["name"], title).ratio()
# if num > most_similar[2] and num > 0.5:
# most_similar = (game["appid"], game["name"], num)
# print(time.time()-t)
# name = difflib.get_close_matches(steam_ids.keys(), title)
# return most_similar
def check_time(): # this function check if it's time to update

View file

@ -15,14 +15,14 @@ from PyQt5.QtGui import QPalette, QColor
if platform.system() == "Windows":
from win32com.client import Dispatch
from rare import lang_path, style_path
from rare import lang_path, style_path, data_dir
# Mac not supported
from custom_legendary.core import LegendaryCore
logger = getLogger("Utils")
s = QSettings("Rare", "Rare")
IMAGE_DIR = s.value("img_dir", os.path.expanduser("~/.cache/rare/images"), type=str)
IMAGE_DIR = os.path.join(data_dir, "images")
def download_images(signal: pyqtSignal, core: LegendaryCore):