forked from Johnserf-Seed/TikTokDownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TikTokUpdata.py
143 lines (130 loc) · 5.09 KB
/
TikTokUpdata.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Description:TikTokUpdata.py
@Date :2022/11/30 15:58:51
@Author :JohnserfSeed
@version :1.0
@License :(C)Copyright 2019-2022, Liugroup-NLPR-CASIA
@Github :https://github.com/johnserf-seed
@Mail :[email protected]
-------------------------------------------------
Change Log :22/11/30 通过版本文件检查是否有更新
-------------------------------------------------
'''
import os
import sys
import shutil
import zipfile
import platform
import requests
# from retrying import retry
class Updata:
# 防止网络问题导致获取更新版本号失败
# 但是会重复下载
# @retry(stop_max_attempt_number=3)
def __init__(self) -> None:
# 根据操作系统生成文件分隔符
if (platform.system() == 'Windows'):
self.sprit = '\\'
elif (platform.system() == 'Linux'):
self.sprit = '/'
else:
self.sprit = '/'
# 本地版本
try:
with open('version', 'r') as file:
self.l_Version = int(file.read())
except:
print('[ 🌋 ]:获取本地版本号失败!')
# 获取失败则使用网络版本-1
self.l_Version = int(requests.get(
'https://cdn.jsdelivr.net/gh/Johnserf-Seed/TikTokDownload@main/version').text) - 1
# 获取失败强制升级
# self.get_Updata()
# 本地没有版本文件则生成当前版本的文件
with open('version', 'w') as file:
file.write(str(self.l_Version))
# 仓库版本
try:
print('[ 🗻 ]:获取最新版本号中!')
self.g_Version = int(requests.get(
'https://cdn.jsdelivr.net/gh/Johnserf-Seed/TikTokDownload@main/version').text)
except:
print('[ 🌋 ]:获取网络版本号失败!')
self.g_Version = self.l_Version
def get_Updata(self):
if self.l_Version == self.g_Version:
print('[ 🚩 ]:目前 %i 版本已是最新' % self.l_Version)
return
elif self.l_Version < self.g_Version:
isUpdata = input('[ 🌋 ]:当前不是最新版本,需要升级吗? (y/n) :')
if isUpdata == 'Y' or isUpdata == 'y':
print('[ 🚩 ]:正在为你下载 %i 版本中,升级前请确保关闭所有打开的项目文件' %
self.g_Version)
self.zip_Download()
if isUpdata == 'N' or isUpdata == 'n':
print('[ 🚩 ]:取消升级,旧版可能会出现没有修复的bug')
return
else:
self.get_Updata()
# @retry(stop_max_attempt_number=3)
def zip_Download(self):
# 删除加速下载
url = 'https://github.com/Johnserf-Seed/TikTokDownload/archive/master.zip'
try:
zip = requests.get(url, stream=True)
filesize = int(zip.headers['content-length'])
except:
input('[ 🚧 ]:网络不太通畅,请重新运行')
sys.exit(0)
with open('TikTokDownload-main.zip', 'wb') as f:
# 写入文件
offset = 0
for chunk in zip.iter_content(chunk_size=512):
if not chunk:
break
f.seek(offset)
f.write(chunk)
offset = offset + len(chunk)
proess = offset / int(filesize) * 100
print('\r' + '[下载进度]:%s%.2f%%' % (
'>' * int(offset * 50 / filesize), proess), end=' ')
print('\r')
# 解压缩升级包
self.zip_Extract()
def zip_Extract(self):
zip_file = zipfile.ZipFile('TikTokDownload-main.zip')
print('[ 提示 ]:开始解压缩升级包')
zip_file.extractall()
# 目标文件夹
target = os.getcwd()
# 需要移动的目录
last = os.getcwd() + self.sprit + 'TikTokDownload-main' + self.sprit
# 移动更新文件
self.move_File(last, target)
def move_File(self, oripath, tardir):
# 判断原始文件路劲是否存在
if not os.path.exists(oripath):
print('[ 🚩 ]:升级目录不存在,请重新运行' % oripath)
status = 0
else:
# 移动文件
for i in os.listdir(oripath):
try:
print('[ 删除 ]:' + tardir + self.sprit + i)
shutil.rmtree(tardir + self.sprit + i)
except:
pass
print('[ 移动 ]:' + oripath + i)
print('[ 移到 ]:' + tardir + self.sprit + i)
shutil.move(oripath + i, tardir + self.sprit + i)
print('[ 🚩 ]:删除更新临时目录')
# 重新读取本地版本
with open('version', 'r') as file:
self.l_Version = int(file.read())
shutil.rmtree(oripath)
status = 1
return status
if __name__ == '__main__':
Updata().get_Updata()