import base64
import os
import re
import threading
import tkinter
import tkinter.messagebox
import time
from tkinter import ttk
from Tools.tools import open_url
import requests
class Update:
def __init__(self, url, banben):
url = 'https://www.lanzous.com/tp/{}'.format(str(url).split('/')[-1])
print(url)
self.url = url
self.banben = banben
self.main()
def Lanzou_geturl(self, url):
while 1:
try:
self.lab['text'] = '正在加载'
headers = {
'host' : 'www.lanzous.com',
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
}
res = requests.get(url, headers=headers)
edow = re.findall("domianload = '(.*?)'", res.text)
nmousedow = re.findall("downloads = '(.*?)';", res.text)
url2 = edow[0] + nmousedow[0]
print(url2)
self.lab['text'] = url2
headers2 = {
'host' : 'vip.d2.baidupan.com',
'User-Agent' : 'Mozilla/5.0 (Linux; U; Android 8.0.0; zh-cn; MI 6 Build/OPR1.170623.027) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.128 Mobile Safari/537.36 XiaoMi/MiuiBrowser/10.5.1',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
}
res4 = requests.head(url2, headers=headers2)
print(res4.headers['Location'])
return res4.headers['Location']
except:
time.sleep(2)
def down_file(self, url, filesize):
try:
buffer = 10240
rsp = requests.get(url, stream=True)
offset = 0
for chunk in rsp.iter_content(chunk_size=buffer):
if not chunk: break
self.the_fileobj.seek(offset)
self.the_fileobj.write(chunk)
offset = offset + len(chunk)
proess = offset / int(filesize) * 100
self.lab['text'] = '进度---->{:.2f}%'.format(proess)
self.the_fileobj.close()
tkinter.messagebox.showinfo('下载完毕!', '最新版软件已经下载完成!')
os.startfile(os.getcwd())
self.root.destroy()
except Exception as e:
print('sss', e)
def start(self):
down_url = self.Lanzou_geturl(self.url)
the_filesize = requests.get(down_url, stream=True).headers['Content-Length']
the_filepath = "{}/小白V{}.exe".format(os.getcwd(), self.banben)
self.the_fileobj = open(the_filepath, 'wb')
t = threading.Thread(target=self.down_file, args=(down_url, the_filesize))
t.daemon = True
t.start()
def start_threading(self):
t = threading.Thread(target=self.start)
t.daemon = True
t.start()
def main(self):
self.root = tkinter.Tk()
self.root.title("更新助手")
self.root.minsize(350, 80)
self.root.geometry('300x70')
self.root.resizable(width=False, height=False)
self.lab = tkinter.Label(self.root, text='进度', fg='darkgreen', font=('微软雅黑', 12))
self.lab.place(x=5, y=10, width=350, height=20)
but = ttk.Button(self.root, text='下载文件', command=self.start_threading)
but.place(x=10, y=40, width=100, height=30)
but = ttk.Button(self.root, text='网页下载', command=lambda: open_url('兰凑云文件地址'))
but.place(x=180, y=40, width=100, height=30)
self.root.mainloop()