Skip to content

Commit

Permalink
self updating mechanism, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tusky committed Aug 12, 2018
1 parent 55a4aea commit 1719fa8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ venv.bak/
.mypy_cache/

config.json
.idea/
.idea/
update/
23 changes: 16 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import subprocess
import time
from typing import TYPE_CHECKING

from api.discord_presence import DiscordPresence
from api.kodi import Kodi
from updater import Updater
from util.config import Configurations
from util.system_tray import SysTray
from util.web_interface import WebInterface
Expand Down Expand Up @@ -80,10 +82,17 @@ def run(self):


if __name__ == '__main__':
config = Configurations()
kodiscord = App(config)
sys_tray = SysTray(kodiscord, config)
sys_tray.start()
web = WebInterface(config)
web.start()
kodiscord.run()
updater = Updater()
if updater.is_there_an_update():
try:
subprocess.Popen(['KoDiscord-updater.exe'], creationflags=subprocess.CREATE_NEW_CONSOLE)
except FileNotFoundError:
pass
else:
config = Configurations()
kodiscord = App(config)
sys_tray = SysTray(kodiscord, config)
sys_tray.start()
web = WebInterface(config)
web.start()
kodiscord.run()
16 changes: 12 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
from pathlib import Path

from cx_Freeze import setup, Executable

Expand All @@ -8,21 +9,28 @@

base = 'Win32GUI'

executables = [Executable('app.py', base=base, icon='kodi-icon.ico', targetName="KoDiscord.exe")]
kodiscord = Executable('app.py', base=base, icon='kodi-icon.ico', targetName="KoDiscord.exe")
updater = Executable('updater.py', base=None, icon='kodi-icon.ico', targetName='KoDiscord-updater.exe')

packages = ['asyncio', 'idna', 'pypresence', 'requests', 'threading', 'typing', 'pystray', 'PIL']
executables = [kodiscord, updater]

packages = ['asyncio', 'idna', 'pypresence', 'requests', 'threading', 'typing', 'pystray', 'PIL', 'pkg_resources']
options = {
'build_exe': {
'packages': packages,
'include_files': ['kodi-icon.ico', 'LICENSE', 'config.json', 'web'],
'include_files': ['kodi-icon.ico', 'LICENSE', 'config.json', 'web', 'version', 'updater.bat'],
'include_msvcr': True
},
}
version = Path('version').read_text().strip()

setup(
name="KoDiscord",
options=options,
version="0.5",
version=version,
author='Richard Hajdu',
author_email='tuskone16@gmail.com',
url='https://github.com/Tusky/KoDiscord',
description='Sends your currently watched Movie or TV Show from Kodi to discord.',
executables=executables
)
3 changes: 3 additions & 0 deletions updater.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
XCOPY /S /Y update\KoDiscord\* .
RMDIR /S /Q update\
start "" KoDiscord.exe
57 changes: 57 additions & 0 deletions updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import subprocess
import zipfile
from pathlib import Path

import requests
from pkg_resources import parse_version


class Updater:
download_url = None
remote_version = None
local_version = None
file = None

def __init__(self):
self.get_local_version()
self.get_remote_version()
self.get_filename()

def get_local_version(self):
self.local_version = parse_version(Path('version').read_text().strip())

def get_remote_version(self):
data = requests.get('https://api.github.com/repos/Tusky/KoDiscord/releases/latest').json()
self.download_url = data['assets'][0]['browser_download_url']
self.remote_version = parse_version(data['tag_name'])

def is_there_an_update(self):
return self.local_version < self.remote_version

def get_filename(self):
folder = Path('update')
if not folder.exists():
folder.mkdir()
self.file = folder / self.download_url.split('/')[-1]

def download_package(self):
if not self.file.exists():
r = requests.get(self.download_url)
self.file.write_bytes(r.content)

def extract_update(self):
with zipfile.ZipFile(str(self.file), "r") as file:
file.extractall("update")
self.file.unlink()

@staticmethod
def update_kodiscord():
subprocess.Popen(['updater.bat'], creationflags=subprocess.CREATE_NEW_CONSOLE)


if __name__ == '__main__':
updater = Updater()
if updater.is_there_an_update():
updater.download_package()
updater.extract_update()
updater.update_kodiscord()
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.6.0

0 comments on commit 1719fa8

Please sign in to comment.