From bcf82a011f4fe5774f74fa386b64d5c1d24776c6 Mon Sep 17 00:00:00 2001 From: NinjaCheetah <58050615+NinjaCheetah@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:04:39 -0400 Subject: [PATCH] NUSGet now checks for updates on startup --- NUSGet.py | 31 +++++++++++++++++++++++++++---- modules/core.py | 22 ++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 modules/core.py diff --git a/NUSGet.py b/NUSGet.py index 11a7c24..5688942 100644 --- a/NUSGet.py +++ b/NUSGet.py @@ -6,6 +6,7 @@ import os import json import pathlib import platform +import webbrowser from importlib.metadata import version from PySide6.QtGui import QIcon @@ -15,6 +16,7 @@ from PySide6.QtCore import QRunnable, Slot, QThreadPool, Signal, QObject, QLibra from qt.py.ui_MainMenu import Ui_MainWindow +from modules.core import * from modules.download_wii import run_nus_download_wii from modules.download_dsi import run_nus_download_dsi @@ -26,7 +28,7 @@ regions = {"World": ["41"], "USA/NTSC": ["45"], "Europe/PAL": ["50"], "Japan": [ # Signals needed for the worker used for threading the downloads. class WorkerSignals(QObject): - result = Signal(int) + result = Signal(object) progress = Signal(str) @@ -59,7 +61,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) - self.log_text = "" self.threadpool = QThreadPool() self.ui.download_btn.clicked.connect(self.download_btn_pressed) self.ui.pack_archive_chkbox.clicked.connect(self.pack_wad_chkbox_toggled) @@ -73,7 +74,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): # Basic intro text set to automatically show when the app loads. This may be changed in the future. libwiipy_version = "v" + version("libWiiPy") libtwlpy_version = "v" + version("libTWLPy") - log_message = (app.translate("MainWindow", "NUSGet v{nusget_version}\nDeveloped by NinjaCheetah\nPowered by libWiiPy " + self.log_text = (app.translate("MainWindow", "NUSGet v{nusget_version}\nDeveloped by NinjaCheetah\nPowered by libWiiPy " "{libwiipy_version}\nDSi support provided by libTWLPy {libtwlpy_version}\n\n" "Select a title from the list on the left, or enter a Title ID to begin.\n\n" "Titles marked with a checkmark are free and have a ticket available, and can" @@ -82,7 +83,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): "downloaded to a folder named \"NUSGet\" inside your downloads folder.") .format(nusget_version=nusget_version, libwiipy_version=libwiipy_version, libtwlpy_version=libtwlpy_version)) - self.ui.log_text_browser.setText(log_message) + self.ui.log_text_browser.setText(self.log_text) # Add console entries to dropdown and attach on change signal. self.ui.console_select_dropdown.addItem("Wii") self.ui.console_select_dropdown.addItem("vWii") @@ -123,6 +124,11 @@ class MainWindow(QMainWindow, Ui_MainWindow): tree[0].insertTopLevelItems(0, self.tree_categories) # Connect the double click signal for handling when titles are selected. tree[0].itemDoubleClicked.connect(self.onItemClicked) + # Do a quick check to see if there's a newer release available, and inform the user if there is. + worker = Worker(check_nusget_updates, nusget_version) + worker.signals.result.connect(self.prompt_for_update) + worker.signals.progress.connect(self.update_log_text) + self.threadpool.start(worker) @Slot(QTreeWidgetItem, int) def onItemClicked(self, item, col): @@ -159,6 +165,23 @@ class MainWindow(QMainWindow, Ui_MainWindow): scroll_bar = self.ui.log_text_browser.verticalScrollBar() scroll_bar.setValue(scroll_bar.maximum()) + def prompt_for_update(self, new_version): + # This method is designed to display a message box informing the user that a new NUSGet version is available. + if new_version is not None: + msg_box = QMessageBox() + msg_box.setIcon(QMessageBox.Icon.Information) + msg_box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) + msg_box.setDefaultButton(QMessageBox.StandardButton.Yes) + msg_box.setWindowTitle(app.translate("MainWindow", "NUSGet Update Available")) + msg_box.setText(app.translate("MainWindow", "There's a newer version of NUSGet available!")) + msg_box.setInformativeText(app.translate("MainWindow", "You're currently running v{nusget_version}, " + "but v{new_version} is available on GitHub. Would you like to view" + " the latest version?" + .format(nusget_version=nusget_version, new_version=nusget_version))) + ret = msg_box.exec() + if ret == QMessageBox.StandardButton.Yes: + webbrowser.open("https://github.com/NinjaCheetah/NUSGet/releases/latest") + def load_title_data(self, selected_title, selected_version, selected_region=None): # Use the information passed from the double click callback to prepare a title for downloading. selected_version = selected_version[1:] diff --git a/modules/core.py b/modules/core.py new file mode 100644 index 0000000..f9be4d0 --- /dev/null +++ b/modules/core.py @@ -0,0 +1,22 @@ +# "modules/core.py", licensed under the MIT license +# Copyright 2024 NinjaCheetah + +import requests + + +def check_nusget_updates(current_version: str, progress_callback=None) -> str | None: + # Simple function to make a request to the GitHub API and then check if the latest available version is newer. + gh_api_request = requests.get(url="https://api.github.com/repos/NinjaCheetah/NUSGet/releases/latest", stream=True) + if gh_api_request.status_code != 200: + progress_callback.emit("\n\nCould not check for updates.") + else: + api_response = gh_api_request.json() + new_version: str = api_response["tag_name"].replace('v', '') + new_version_split = new_version.split('.') + current_version_split = current_version.split('.') + for place in range(len(new_version_split)): + if new_version_split[place] > current_version_split[place]: + progress_callback.emit("\n\nThere's a newer version of NUSGet available!") + return new_version + progress_callback.emit("\n\nYou're running the latest release of NUSGet.") + return None diff --git a/requirements.txt b/requirements.txt index 65aafac..04f7d7a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ nuitka libWiiPy libTWLPy zstandard +requests imageio \ No newline at end of file