2021-06-15 08:30:57 +12:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from PyQt5 import QtGui
|
2021-08-08 09:42:40 +12:00
|
|
|
from PyQt5.QtCore import pyqtSignal
|
|
|
|
from PyQt5.QtNetwork import QNetworkAccessManager
|
2021-06-15 08:30:57 +12:00
|
|
|
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel
|
|
|
|
|
2021-08-08 09:42:40 +12:00
|
|
|
from rare.components.tabs.shop.constants import search_query
|
2021-06-15 08:30:57 +12:00
|
|
|
from rare.utils.extra_widgets import ImageLabel
|
2021-08-08 09:42:40 +12:00
|
|
|
from rare.utils.utils import get_lang, QtRequestManager
|
2021-06-15 08:30:57 +12:00
|
|
|
|
2021-06-17 05:03:18 +12:00
|
|
|
logger = logging.getLogger("GameWidgets")
|
|
|
|
|
2021-06-15 08:30:57 +12:00
|
|
|
|
|
|
|
class GameWidget(QWidget):
|
|
|
|
show_info = pyqtSignal(dict)
|
|
|
|
|
|
|
|
def __init__(self, path, json_info=None, width=300):
|
|
|
|
super(GameWidget, self).__init__()
|
|
|
|
self.manager = QNetworkAccessManager()
|
|
|
|
self.width = width
|
|
|
|
self.path = path
|
2021-08-08 09:42:40 +12:00
|
|
|
if json_info:
|
|
|
|
self.init_ui(json_info)
|
2021-06-15 08:30:57 +12:00
|
|
|
|
2021-08-08 09:42:40 +12:00
|
|
|
def init_ui(self, json_info):
|
2021-06-15 08:30:57 +12:00
|
|
|
self.layout = QVBoxLayout()
|
|
|
|
self.image = ImageLabel()
|
2021-06-17 10:34:23 +12:00
|
|
|
self.layout.addWidget(self.image)
|
|
|
|
|
|
|
|
self.title_label = QLabel(json_info["title"])
|
|
|
|
self.title_label.setWordWrap(True)
|
|
|
|
self.layout.addWidget(self.title_label)
|
|
|
|
|
|
|
|
for c in r'<>?":|\/*':
|
|
|
|
json_info["title"] = json_info["title"].replace(c, "")
|
|
|
|
|
2021-06-15 08:30:57 +12:00
|
|
|
self.json_info = json_info
|
|
|
|
self.slug = json_info["productSlug"]
|
|
|
|
|
|
|
|
self.title = json_info["title"]
|
|
|
|
for img in json_info["keyImages"]:
|
|
|
|
if img["type"] in ["DieselStoreFrontWide", "OfferImageWide", "VaultClosed"]:
|
|
|
|
if img["type"] == "VaultClosed" and self.title != "Mystery Game":
|
|
|
|
continue
|
|
|
|
self.image.update_image(img["url"], json_info["title"], (self.width, int(self.width * 9 / 16)))
|
|
|
|
break
|
|
|
|
else:
|
2021-06-17 05:03:18 +12:00
|
|
|
logger.info(", ".join([img["type"] for img in json_info["keyImages"]]))
|
|
|
|
# print(json_info["keyImages"])
|
2021-06-15 08:30:57 +12:00
|
|
|
|
|
|
|
self.setLayout(self.layout)
|
|
|
|
|
|
|
|
def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
|
|
|
|
self.show_info.emit(self.json_info)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_request(cls, name, path):
|
|
|
|
c = cls(path)
|
2021-08-08 09:42:40 +12:00
|
|
|
c.manager = QtRequestManager("json")
|
|
|
|
c.manager.data_ready.connect(c.handle_response)
|
2021-06-15 08:30:57 +12:00
|
|
|
locale = get_lang()
|
2021-08-08 09:42:40 +12:00
|
|
|
payload = {
|
|
|
|
"query": search_query,
|
2021-06-15 08:30:57 +12:00
|
|
|
"variables": {"category": "games/edition/base|bundles/games|editors|software/edition/base", "count": 1,
|
|
|
|
"country": "DE", "keywords": name, "locale": locale, "sortDir": "DESC",
|
|
|
|
"allowCountries": locale.upper(),
|
|
|
|
"start": 0, "tag": "", "withMapping": False, "withPrice": True}
|
2021-08-08 09:42:40 +12:00
|
|
|
}
|
|
|
|
c.manager.post("https://www.epicgames.com/graphql", payload)
|
|
|
|
c.search_request.da.connect(lambda: c.handle_response(path))
|
2021-06-15 08:30:57 +12:00
|
|
|
return c
|
|
|
|
|
2021-08-08 09:42:40 +12:00
|
|
|
def handle_response(self, data):
|
|
|
|
|
|
|
|
data = data["data"]["Catalog"]["searchStore"]["elements"][0]
|
|
|
|
self.init_ui(data)
|