1
0
Fork 0
mirror of synced 2024-06-02 02:34:40 +12:00

Download Images

This commit is contained in:
Dummerle 2020-11-01 19:09:53 +01:00
parent abc0d5d433
commit e2e713840e
5 changed files with 93 additions and 37 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/images/
/.idea/
/Rare/__pycache__/

19
Rare.py
View file

@ -1,16 +1,16 @@
#!/usr/bin/env python
import os
import math
import json
import requests
import configparser
import json
import math
import os
import subprocess
import requests
from PIL import Image
from time import sleep
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
print('Rare v1 starting up...')
@ -279,7 +279,7 @@ class LoginWindow(QMainWindow):
pass
if os.path.isfile(os.path.expanduser("~") + '/.config/legendary/user.json'):
self.close()
#Run list-games once to get game list + info for newly logged in account
# Run list-games once to get game list + info for newly logged in account
installedGames = subprocess.Popen('legendary list-games', shell=True,
stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
installedGames.wait()
@ -386,8 +386,8 @@ def main():
else:
notLoggedIn()
def startMainProcess(loggedIn=False):
def startMainProcess(loggedIn=False):
print('Creating directorys')
try:
os.mkdir('images')
@ -467,7 +467,7 @@ def startMainProcess(loggedIn=False):
uninstalledArt.save('images/' + game["app_name"] + '/UninstalledArt.png')
except:
pass
#If the user had to login first, the QApplication will already be running, so we don't have to start it again
# If the user had to login first, the QApplication will already be running, so we don't have to start it again
if not loggedIn:
# Start GUI stuff
app = QApplication([])
@ -478,7 +478,6 @@ def startMainProcess(loggedIn=False):
mainWindow.show()
def notLoggedIn():
app = QApplication([])
window = LoginWindow()

View file

@ -7,7 +7,6 @@ from Rare.Login import ImportWidget
class LoginDialog(QDialog):
signal = pyqtSignal(bool)
def __init__(self):
super(LoginDialog, self).__init__()
@ -16,7 +15,7 @@ class LoginDialog(QDialog):
self.widget = QWidget()
self.import_widget = ImportWidget()
self.login_widget = QWidget()
self.import_widget.signal.connect(self.loggedin)
self.import_widget.signal.connect(self.exit_login)
self.initWidget()
self.layout.insertWidget(0, self.widget)
@ -25,6 +24,7 @@ class LoginDialog(QDialog):
self.setLayout(self.layout)
self.layout.setCurrentIndex(0)
self.show()
def initWidget(self):
self.widget_layout = QVBoxLayout()
@ -41,8 +41,9 @@ class LoginDialog(QDialog):
self.widget_layout.addWidget(self.close_button)
self.widget.setLayout(self.widget_layout)
def loggedin(self, int):
self.signal.emit(True)
def get_login(self):
self.exec_()
def login(self):
self.layout.setCurrentIndex(1)
@ -51,8 +52,7 @@ class LoginDialog(QDialog):
self.layout.setCurrentIndex(2)
def exit_login(self):
self.signal.emit(False)
self.close()
class InstallDialog(QDialog):
def __init__(self, game):

View file

@ -1,11 +1,21 @@
import logging
import os
import sys
import requests
from PIL import Image
from PyQt5.QtWidgets import QTabWidget, QMainWindow, QWidget, QApplication
from Rare.Dialogs import LoginDialog
from Rare.TabWidgets import Settings, GameListInstalled, BrowserTab, GameListUninstalled, UpdateList
from Rare.config import IMAGE_DIR, LOGLEVEL
from Rare.utils import legendaryUtils
logging.basicConfig(
format='[%(name)s] %(levelname)s: %(message)s',
level=LOGLEVEL
)
logger = logging.getLogger("Rare")
class MainWindow(QMainWindow):
@ -38,32 +48,73 @@ class TabWidget(QTabWidget):
self.addTab(self.settings, "Settings")
def main(start: bool = True):
# Check if Legendary is installed
if not bool:
exit(0)
def main():
app = QApplication(sys.argv)
if os.path.isfile(os.path.expanduser("~") + '/.config/legendary/user.json'):
print("Launching Rare")
startMainProcess()
logger.info("Launching Rare")
download_images()
window = MainWindow()
else:
notLoggedIn()
dia = LoginDialog()
if not dia.login():
main()
def startMainProcess():
# TODO Download Images
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
def notLoggedIn():
app = QApplication(sys.argv)
dia = LoginDialog()
dia.signal.connect(main)
dia.show()
app.exec_()
def download_images():
if not os.path.isdir(IMAGE_DIR):
os.mkdir(IMAGE_DIR)
# Download Images
for game in legendaryUtils.get_games():
for image in game.metadata["keyImages"]:
if image["type"] == "DieselGameBoxTall" or image["type"] == "DieselGameBoxLogo":
if not os.path.isfile(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png"):
if not os.path.isdir(f"{IMAGE_DIR}/" + game.app_name):
os.mkdir(f"{IMAGE_DIR}/" + game.app_name)
logger.info(f"Download Image for Game: {game.app_title}")
url = image["url"]
with open(f"{IMAGE_DIR}/{game.app_name}/{image['type']}.png", "wb") as f:
f.write(requests.get(url).content)
f.close()
if not os.path.isfile(f"{IMAGE_DIR}/{game.app_name}/FinalArt.png"):
logger.info("Scaling cover for " + game.app_name)
if os.path.isfile(f"{IMAGE_DIR}/{game.app_name}/DieselGameBoxLogo"):
bg: Image.Image = Image.open()
bg = bg.resize((int(bg.size[1] * 3 / 4), bg.size[1]))
logo = Image.open('images/' + game["app_name"] + '/DieselGameBoxLogo.png').convert('RGBA')
wpercent = ((bg.size[0] * (3 / 4)) / float(logo.size[0]))
hsize = int((float(logo.size[1]) * float(wpercent)))
logo = logo.resize((int(bg.size[0] * (3 / 4)), hsize), Image.ANTIALIAS)
# Calculate where the image has to be placed
pasteX = int((bg.size[0] - logo.size[0]) / 2)
pasteY = int((bg.size[1] - logo.size[1]) / 2)
# And finally copy the background and paste in the image
finalArt = bg.copy()
finalArt.paste(logo, (pasteX, pasteY), logo)
# Write out the file
finalArt.save(f'{IMAGE_DIR}/' + game.app_name + '/FinalArt.png')
logoCopy = logo.copy()
logoCopy.putalpha(int(256 * 3 / 4))
logo.paste(logoCopy, logo)
uninstalledArt = bg.copy()
uninstalledArt.paste(logo, (pasteX, pasteY), logo)
uninstalledArt = uninstalledArt.convert('L')
uninstalledArt.save(f'{IMAGE_DIR}/' + game.app_name + '/UninstalledArt.png')
else:
# We just open up the background and save that as the final image
if os.path.isfile(f'{IMAGE_DIR}/' + game.app_name + '/DieselGameBoxTall.png'):
finalArt = Image.open(f'{IMAGE_DIR}/' + game.app_name + '/DieselGameBoxTall.png')
finalArt.save(f'{IMAGE_DIR}/{game.app_name}/FinalArt.png')
# And same with the grayscale one
uninstalledArt = finalArt.convert('L')
uninstalledArt.save(f'{IMAGE_DIR}/{game.app_name}/UninstalledArt.png')
else:
logger.error(f"File {IMAGE_DIR}/{game.app_name}/DieselGameBoxTall.png dowsn't exist")
if __name__ == '__main__':
main()

4
Rare/config.py Normal file
View file

@ -0,0 +1,4 @@
import logging
IMAGE_DIR = "../images"
LOGLEVEL = logging.ERROR