This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_update.py
151 lines (124 loc) · 4.53 KB
/
github_update.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import multiprocessing
from sys import platform
from tkinter import messagebox, filedialog
import requests
from diskcache import Cache
import mul_process_package
mul_process_package.ok()
github_release_url = 'https://api.github.com/repos/gzlock/mrplayer_mainland_live_server/releases/latest'
latest_version_key = 'latest_version'
file_key = 'latest_version_file'
file_downloaded_key = 'latest_version_file_downloaded'
def get_github_version(cache):
try:
print('检测github release')
res = requests.get(github_release_url, timeout=2)
except Exception as e:
print('错误', e)
_cache: Cache = None
_latest_version = 0
class UpdateSoftware:
__state__ = ''
def __init__(self, root, cache, current_version: float):
super().__init__()
self.root = root
global _cache
_cache = cache
self.current_version = current_version
self.asked = False
self.cache_version = _cache.get(latest_version_key, default=0)
if self.cache_version < current_version:
self.cache_version = current_version
# root.after_idle(self.get_github_version)
self.get_github_version()
self.loop_check()
def loop_check(self):
try:
# 还没有保存
if not self.asked and _cache.get(file_downloaded_key, default=False):
self.download_success()
except:
pass
finally:
if not self.asked:
self.root.after(2000, self.loop_check)
def get_github_version(self):
try:
print('检测github release')
res = requests.get(github_release_url, timeout=5)
if res.status_code == 200:
res = res.json()
self.compare_version(res)
except:
pass
def compare_version(self, res: dict = None):
"""
对比版本号
:param res:
:return:
"""
latest_version = float(res['tag_name'])
downloaded = _cache.get(file_downloaded_key, default=False)
print(
{'current version': self.current_version,
'cache version': self.cache_version,
'github version': latest_version,
'downloaded': downloaded})
# 比缓存里的版本号要新
if latest_version > self.cache_version:
print('有新版本,下载')
# 更新 缓存 版本号
_cache.set(latest_version_key, latest_version)
# 清空缓存
_cache.set(file_downloaded_key, False, 0)
_cache.set(file_key, 1, 0)
_cache.expire()
# 需要下载
self.download_file(res)
elif self.cache_version > self.current_version:
print('缓存版本比运行的新')
if downloaded:
print('已经完成,提示更新')
self.download_success()
else:
print('下载新版本')
self.download_file(res)
else:
print('没有新版本,清空文件缓存')
_cache.set(file_downloaded_key, False, 0)
_cache.set(file_key, 1, 0)
_cache.expire()
def download_file(self, res: dict):
if platform == 'win32':
find = 'win.zip'
else:
find = 'macOS.zip'
url: str = None
for item in res['assets']:
if item['name'] == find:
url = item['browser_download_url']
break
print('platform', platform, find, url)
multiprocessing.Process(target=UpdateSoftware.download_thread, args=(url, _cache)).start()
@staticmethod
def download_thread(url, _cache):
print('下载', url)
if url is None:
return
# return
res = requests.get(url)
if res.status_code == 200:
_cache.set(file_key, res.content)
_cache.set(file_downloaded_key, True)
print('下载完成')
def download_success(self, *args):
self.asked = True
yes = messagebox.askyesno('新版本下载完成', '是否保存到硬盘?')
if not yes:
return
latest_version = _cache.get(latest_version_key)
file = filedialog.asksaveasfilename(title='保存到', filetypes=[("Zip", ".zip")],
defaultextension='zip', initialfile='玩很大 v' + str(latest_version))
if len(file) > 0:
with open(file, 'wb') as file:
file.write(_cache.get(file))