Skip to content

Commit

Permalink
Check for latest version
Browse files Browse the repository at this point in the history
- Adds check to latest GitHub release
- If new version found, can open up to that webpage
  • Loading branch information
bbtufty committed Oct 20, 2024
1 parent fce6cfa commit 0dc8515
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.2 (Unreleased)
================

- Check version is most up-to-date [#8]
- Add configurable region/language preferences [#7]
- Ensure package name is valid for JDownloader [#6]

Expand Down
5 changes: 5 additions & 0 deletions nxbrew_dl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from importlib.metadata import version

from .gui import MainWindow

# Get the version
__version__ = version(__name__)

__all__ = [
"MainWindow",
]
58 changes: 53 additions & 5 deletions nxbrew_dl/gui/gui_nxbrew_dl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from functools import partial

from PySide6.QtCore import (
Expand All @@ -11,9 +12,11 @@
)
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import (
QMessageBox,
QMainWindow,
QFileDialog,
)
from packaging.version import Version

import nxbrew_dl
from .gui_about import AboutWindow
Expand All @@ -27,6 +30,7 @@
from .layout_nxbrew_dl import Ui_nxbrew_dl
from ..nxbrew_dl import NXBrew
from ..util import (
check_github_version,
get_game_dict,
load_yml,
save_yml,
Expand Down Expand Up @@ -68,6 +72,26 @@ def __init__(self):
icon.addFile(icon_path, QSize(), QIcon.Mode.Normal, QIcon.State.Off)
self.setWindowIcon(icon)

# Set up the logger
self.logger = get_gui_logger(log_level="INFO")
self.logger.warning("Do not close this window!")

# Check for version updates
self.logger.info("Checking for new versions online")
github_version, github_url = check_github_version()
local_version = nxbrew_dl.__version__

new_version_available = False
if Version(local_version) < Version(github_version):
self.logger.info("New version of NXBrew-dl available!")
new_version_available = True
else:
self.logger.info("You have the latest version of NXBrew-dl")

self.update_notification = self.setup_update_notification(new_version_available,
url=github_url,
)

# Load in various config files
self.mod_dir = os.path.dirname(nxbrew_dl.__file__)

Expand Down Expand Up @@ -102,9 +126,6 @@ def __init__(self):
# Do an initial load of the config
self.load_config()

self.logger = get_gui_logger(log_level="INFO")
self.logger.warning("Do not close this window!")

# Set up the worker threads for later
self.nxbrew_thread = None
self.nxbrew_worker = None
Expand Down Expand Up @@ -151,6 +172,29 @@ def __init__(self):

self.load_table()

def setup_update_notification(self,
new_version_available,
url,
):
"""Create a message box to open up to the latest GitHub release"""

if not new_version_available:
return None

# Open up a dialogue box to go to the webpage
update_box = QMessageBox()
reply = update_box.question(self,
"Version update!",
"Open latest GitHub release?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)

if reply == QMessageBox.StandardButton.Yes:
self.logger.info("Opening GitHub, and closing down")
open_url(url)
sys.exit()

return update_box

def get_game_dict(self):
"""Get game dictionary from NXBrew A-Z page"""

Expand Down Expand Up @@ -397,8 +441,12 @@ def run_nxbrew_dl(self):
def closeEvent(self, event):
"""Close the application"""

self.logger.info("Closing down. Will save config")
self.save_config()
# Check if we've fully loaded, else just close it down
loaded = hasattr(self, "user_config")

if loaded:
self.logger.info("Closing down. Will save config")
self.save_config()

event.accept()

Expand Down
2 changes: 2 additions & 0 deletions nxbrew_dl/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .discord_tools import discord_push
from .download_tools import get_dl_dict, bypass_ouo
from .github_tools import check_github_version
from .html_tools import get_html_page, get_game_dict, get_languages, get_thumb_url
from .io_tools import load_yml, save_yml, load_json, save_json
from .regex_tools import check_has_filetype, get_game_name
Expand All @@ -8,6 +9,7 @@
"discord_push",
"get_dl_dict",
"bypass_ouo",
"check_github_version",
"get_html_page",
"get_game_dict",
"check_has_filetype",
Expand Down
15 changes: 15 additions & 0 deletions nxbrew_dl/util/github_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import requests

def check_github_version():
"""Check NXBrew-dl version on GitHub. Returns version and associated URL"""

url = "https://api.github.com/repos/bbtufty/nxbrew-dl/releases/latest"
r = requests.get(url)

json = r.json()

# Pull out version and URL
version = json["name"]
github_url = json["html_url"]

return version, github_url

0 comments on commit 0dc8515

Please sign in to comment.