-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdater.py
61 lines (48 loc) · 1.74 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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):
try:
data = requests.get('https://api.github.com/repos/Tusky/KoDiscord/releases/latest').json()
except requests.exceptions.ConnectionError:
self.remote_version = self.local_version
else:
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()