Skip to content

Commit e1a8d1c

Browse files
committed
plugin updateCheck added
added an update check upon plugin boot. If an update is found the user is prompted to download & install automatically or open website and download and install manually
1 parent 98f9e6d commit e1a8d1c

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

src/Kofi.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
import TouchPortalAPI as TP
1616
from TouchPortalAPI.logger import Logger
1717

18-
from TPPEntry import TP_PLUGIN_SETTINGS, PLUGIN_ID, TP_PLUGIN_INFO, __version__
18+
from TPPEntry import TP_PLUGIN_SETTINGS, PLUGIN_ID, TP_PLUGIN_INFO, PLUGIN_NAME, PLUGIN_RELEASE_INFO, __version__
1919
from createDefaultConfig import create_default_yaml_file
2020
from updateConfig import update_config_file
21+
from update_check import plugin_update_check, download_update
2122

2223

2324
## • figure out how to set a static location for pyngrok to download the ngrok.exe file rather than it doing it every time?
@@ -250,11 +251,31 @@ def handleSettings(settings, on_connect=False):
250251
TP_PLUGIN_SETTINGS['Kofi Verification Token']['value'] = value
251252

252253

254+
255+
256+
253257
@TPClient.on(TP.TYPES.onNotificationOptionClicked)
254258
def onNoticationClicked(data):
255259
if data['optionId'] == f"{PLUGIN_ID}.settings.error.options":
256260
url = "https://dashboard.ngrok.com/signup"
257261
webbrowser.open(url, new=0, autoraise=True)
262+
263+
elif data['optionId'] == f"{PLUGIN_ID}.update.download":
264+
if PLUGIN_RELEASE_INFO['downloadURL']:
265+
download_URL = PLUGIN_RELEASE_INFO['downloadURL']
266+
g_log.info("Downloading the update...")
267+
tpp_file = download_update(download_URL)
268+
if tpp_file:
269+
os.startfile(tpp_file)
270+
else:
271+
g_log.error("Error downloading the update, download URL not found.", download_URL)
272+
273+
elif data['optionId'] == f"{PLUGIN_ID}.update.manual":
274+
if PLUGIN_RELEASE_INFO['htmlURL']:
275+
webbrowser.open(PLUGIN_RELEASE_INFO['htmlURL'], new=0, autoraise=True)
276+
else:
277+
g_log.error("Error opening the download page, URL not found.", PLUGIN_RELEASE_INFO['htmlURL'])
278+
258279

259280

260281
#--- On Startup ---#
@@ -264,6 +285,31 @@ def onConnect(data):
264285
g_log.debug(f"Connection: {data}")
265286
if settings := data.get('settings'):
266287
handleSettings(settings, True)
288+
289+
try:
290+
global PLUGIN_RELEASE_INFO
291+
PLUGIN_RELEASE_INFO = plugin_update_check(str(data['pluginVersion']))
292+
293+
if PLUGIN_RELEASE_INFO['patchnotes']:
294+
patchNotes = f"A new version of {PLUGIN_NAME} is available and ready to Download.\nThis may include Bug Fixes and or New Features\n\nPatch Notes\n{PLUGIN_RELEASE_INFO['patchnotes']}"
295+
elif PLUGIN_RELEASE_INFO['patchnotes'] == "":
296+
patchNotes = f"A new version of {PLUGIN_NAME} is available and ready to Download.\nThis may include Bug Fixes and or New Features"
297+
if PLUGIN_RELEASE_INFO['version']:
298+
TPClient.showNotification(
299+
notificationId= f"{PLUGIN_ID}.TP.Plugins.Update_Check",
300+
title=f"{PLUGIN_NAME} {PLUGIN_RELEASE_INFO['version']} is available",
301+
msg=patchNotes,
302+
options= [
303+
{
304+
"id":f"{PLUGIN_ID}.update.download",
305+
"title":"(Auto) Download & Update!"
306+
},
307+
{
308+
"id":f"{PLUGIN_ID}.update.manual",
309+
"title":"(Manual) Open Plugin Download Page"
310+
}])
311+
except Exception as e:
312+
print("Error Checking for Updates", e)
267313

268314
create_default_yaml_file('ngrok.yaml')
269315
time.sleep(1)

src/TPPEntry.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
PLUGIN_ID = "gitago.kofi"
1+
PLUGIN_ID = "gitago.kofi"
22
PLUGIN_NAME = "Kofi"
33
PLUGIN_ICON = "Kofi_Logo_26px.png"
44
PLUGIN_FOLDER = "Kofi"
55
__version__ = 101
66

7-
7+
GITHUB_USER_NAME = "GitagoGaming"
8+
GITHUB_PLUGIN_NAME = "Kofi-TouchPortal-Plugin"
9+
PLUGIN_RELEASE_INFO = {} # This will be updated by the update_check.py script
810

911
TP_PLUGIN_INFO = {
1012
"sdk": 6,

src/update_check.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#update check
2+
import os
3+
import base64
4+
import requests
5+
from tempfile import gettempdir
6+
7+
from TPPEntry import GITHUB_USER_NAME, GITHUB_PLUGIN_NAME
8+
9+
10+
def get_release_info(owner, repo):
11+
url = f'https://api.github.com/repos/{owner}/{repo}/releases'
12+
if (response := requests.get(url)) and response.ok:
13+
if (releases := response.json()):
14+
for release in releases:
15+
if not release.get('prerelease'):
16+
assets = release.get('assets', [])
17+
if assets:
18+
download_url = assets[0].get('browser_download_url', "")
19+
release_version = release.get('tag_name', "")
20+
html_url = release.get('html_url', "")
21+
return release_version, download_url, html_url
22+
raise ValueError(f'No suitable non-prerelease found in repository: {url}')
23+
else:
24+
raise ValueError(f'Failed to decode JSON response from: {url}')
25+
else:
26+
raise ValueError(f'Invalid repository URL or response: {url}')
27+
28+
29+
def plugin_update_check(plugin_version:str):
30+
""" Checks Github for the latest version of the plugin
31+
- Returns patchnotes on notification if there is a new version
32+
"""
33+
34+
try:
35+
github_version, downloadURL, htmlURL = get_release_info(GITHUB_USER_NAME, GITHUB_PLUGIN_NAME)
36+
github_version = github_version.replace('v','').replace(".","")
37+
38+
if github_version.replace('v','').replace(".","") > plugin_version:
39+
### Pulling Patch Notes for Notification
40+
41+
try:
42+
r = requests.get(f"https://api.github.com/repos/{GITHUB_USER_NAME}/{GITHUB_PLUGIN_NAME}/contents/recent_patchnotes.txt")
43+
if r.status_code == 404:
44+
print("No Patch Notes Found")
45+
message = ""
46+
else:
47+
base64_bytes = r.json()['content'].encode('ascii')
48+
message_bytes = base64.b64decode(base64_bytes)
49+
message = message_bytes.decode('ascii')
50+
except Exception as e:
51+
message = None
52+
print("Error Plugin Update Check: ", e)
53+
return {"version":github_version, "patchnotes":message, "downloadURL": downloadURL, "htmlURL": htmlURL}
54+
else:
55+
return False, False
56+
57+
except Exception as e:
58+
print("Something went wrong checking update", e)
59+
60+
61+
62+
def download_update(download_url):
63+
# Extract the file name from the URL
64+
file_name = download_url.split('/')[-1]
65+
# Construct the full path in the temp directory
66+
temp_file_path = os.path.join(gettempdir(), file_name)
67+
68+
response = requests.get(download_url)
69+
if response.status_code == 200:
70+
with open(temp_file_path, 'wb') as file:
71+
file.write(response.content)
72+
return temp_file_path
73+
return None

0 commit comments

Comments
 (0)