2024-05-03 09:42:03 +08:00
|
|
|
from re import T
|
2024-05-03 09:08:46 +08:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import requests
|
|
|
|
import py7zr
|
|
|
|
from threading import Thread
|
|
|
|
from logger.logger import *
|
|
|
|
|
|
|
|
CHUNKSIZE = 1024
|
|
|
|
|
|
|
|
|
|
|
|
class Download_Theard(Thread):
|
2024-05-03 09:42:03 +08:00
|
|
|
def __init__(self, url, to, progress_callback, onfinish, fname):
|
2024-05-03 09:08:46 +08:00
|
|
|
self.url = url
|
|
|
|
self.to = to
|
|
|
|
self.progress_callback = progress_callback
|
2024-05-03 09:42:03 +08:00
|
|
|
self.fname = fname
|
2024-05-03 09:08:46 +08:00
|
|
|
self.onfinish = onfinish
|
|
|
|
Thread.__init__(self, daemon=True)
|
|
|
|
|
|
|
|
def run(self):
|
2024-05-03 09:42:03 +08:00
|
|
|
finished = False
|
|
|
|
if (os.path.exists("software"+os.sep+self.fname)):
|
|
|
|
self.onfinish()
|
|
|
|
finished = True
|
2024-05-03 09:08:46 +08:00
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
info("[downloader]download "+self.url)
|
|
|
|
self.progress_callback(0)
|
|
|
|
r = requests.get(self.url, stream=True)
|
|
|
|
if (r.status_code >= 300):
|
|
|
|
raise Exception
|
|
|
|
total = int(r.headers.get('content-length', 0))
|
|
|
|
now_file = 0
|
|
|
|
with open(self.to, 'wb') as fp:
|
|
|
|
for item in r.iter_content(CHUNKSIZE):
|
|
|
|
fp.write(item)
|
|
|
|
now_file += len(item)
|
|
|
|
self.progress_callback(now_file/total*0.9)
|
|
|
|
self.progress_callback(0.9)
|
|
|
|
info("[downloader]unzip "+self.url)
|
|
|
|
with py7zr.SevenZipFile(self.to, mode='r') as z:
|
|
|
|
z.extractall(path="software")
|
|
|
|
self.progress_callback(1)
|
2024-05-03 09:42:03 +08:00
|
|
|
if (finished == False):
|
|
|
|
self.onfinish()
|
2024-05-03 09:08:46 +08:00
|
|
|
return
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def download_files(files, onfisish, anim_list):
|
|
|
|
threads = []
|
|
|
|
finishnum = 0
|
|
|
|
finishmax = len(files)
|
|
|
|
|
|
|
|
def anim_check(*args):
|
|
|
|
onfisish() # run in main thread
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def progfinish():
|
|
|
|
nonlocal finishnum, finishmax
|
|
|
|
finishnum += 1
|
|
|
|
if (finishnum == finishmax):
|
|
|
|
anim_list.anim.append(anim_check)
|
|
|
|
|
|
|
|
for i in files:
|
|
|
|
threads.append(Download_Theard(
|
2024-05-03 09:42:03 +08:00
|
|
|
i["from"], i["to"], i["progbar"], progfinish, i["name"]))
|
2024-05-03 09:08:46 +08:00
|
|
|
threads[-1].start()
|