Skip to content

Commit

Permalink
Added look for newer version and display in console and gui if availa…
Browse files Browse the repository at this point in the history
…ble (#54)

* Added look for newer version and display in console and gui if available

* Added requestes package to requirements because Windows doesnt have it by default
  • Loading branch information
howroyd authored Nov 2, 2023
1 parent b670c67 commit 7a97daa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "simonsays_drgreengiant"
version = "2.0.0"
version = "2.0.1"
authors = [{ name = "Simon Howroyd", email = "[email protected]" }]
description = "A Twitch Plays style programme to allow users in Twitch chats to control the broadcasters mouse and keyboard"
keywords = ["twitch", "chat", "twitchplays", "troll"]
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Pillow
pynput
requests
semantic-version
tomlkit
twitchirc_drgreengiant
16 changes: 12 additions & 4 deletions src/simonsays_drgreengiant/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from collections.abc import Iterable
from typing import Any, Callable, NoReturn, Optional

import semantic_version

from . import config, environment, hidactions

KEY_IGNORED_STR = 'ignored'
Expand Down Expand Up @@ -171,7 +173,7 @@ def make_window(cfg: config.Config, width_px: int, height_px: int) -> tk.Tk:
return window


def make_canvas(cfg: config.Config, image_path: str, *, window: tk.Tk | None = None) -> tk.Canvas:
def make_canvas(cfg: config.Config, image_path: str, updateavailable: semantic_version = None, *, window: tk.Tk | None = None) -> tk.Canvas:
"""Make the canvas with a background image"""
# img = tk.PhotoImage(file=image_path)
from PIL import Image
Expand All @@ -187,7 +189,13 @@ def make_canvas(cfg: config.Config, image_path: str, *, window: tk.Tk | None = N
canvas.image = img # Keep a reference to the image to prevent garbage collection
channels = "\n".join(cfg.channel)
canvas.create_text((5, 5), text=f"Connected to channels:\n{channels}", anchor=tk.N + tk.W)
canvas.create_text((img.width() - 5, 5), text=f"Version: {cfg.version}", anchor=tk.N + tk.E)

versiontext = f"Version: {cfg.version}"
if updateavailable:
versiontext += "\nNew version available!"
versiontext += f"\nLatest version: {updateavailable}"

canvas.create_text((img.width() - 5, 5), text=versiontext, anchor=tk.N + tk.E)

window.update()

Expand Down Expand Up @@ -490,9 +498,9 @@ def enabled_cb(cfg: config.Config, enabled_button: tk.Button, state_var: tk.Bool
print("Enabled")


def make_gui(cfg: config.Config) -> tuple[tk.Tk, mp.Event, Callable]:
def make_gui(cfg: config.Config, updateavailable: semantic_version.Version = None) -> tuple[tk.Tk, mp.Event, Callable]:
"""Make the GUI"""
canvas = make_canvas(cfg, environment.resource_path("assets", "Green_tato_640.png"))
canvas = make_canvas(cfg, environment.resource_path("assets", "Green_tato_640.png"), updateavailable)
window = canvas.winfo_toplevel()

triggerredraw = mp.Event()
Expand Down
17 changes: 16 additions & 1 deletion src/simonsays_drgreengiant/simonsays.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pprint
from typing import NoReturn

import requests
import semantic_version
from twitchirc_drgreengiant import offlineirc, twitchirc

from . import config, environment, errorcodes, gui, twitchactions
Expand Down Expand Up @@ -100,6 +102,15 @@ def get_action_from_message(myconfig: config.Config, msg: twitchirc.TwitchMessag
return functools.partial(myconfig.actions[tag].run, force=sudo), tag


def check_for_updates() -> semantic_version.Version | None:
latest = requests.get("https://api.github.com/repos/howroyd/simonsays/releases/latest")
latestversion = semantic_version.Version(latest.json()["tag_name"].lstrip("v"))
thisversion = semantic_version.Version.coerce(VERSION.lstrip("v"))
if latestversion > thisversion:
return latestversion
return None


def main() -> NoReturn:
myconfig = config.Config.load(VERSION)
myconfig.save(backup_old=True)
Expand All @@ -109,16 +120,20 @@ def main() -> NoReturn:
with contextlib.ExitStack() as stack:
executor = stack.enter_context(cf.ThreadPoolExecutor(max_workers=1))

updateavailable = None
irc = None
if config.OFFLINE:
print("OFFLINE MODE")
irc = stack.enter_context(offlineirc.OfflineIrc(myconfig.channel, username="drgreengiant"))
else:
if updateavailable := check_for_updates():
print(f"Update available! Current version: {VERSION}, Latest version: {updateavailable}")
print("https://github.com/howroyd/simonsays/releases/tag/2.0.0\n\n")
irc = stack.enter_context(twitchirc.TwitchIrc(myconfig.channel))

print(channel_connected(myconfig))

mygui, exit_event, redraw_gui = gui.make_gui(myconfig)
mygui, exit_event, redraw_gui = gui.make_gui(myconfig, updateavailable=updateavailable)

while True:
redraw_gui() # Required otherwise you can't click stuff
Expand Down

0 comments on commit 7a97daa

Please sign in to comment.