65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
|
import time
|
||
|
import os
|
||
|
import shutil
|
||
|
import requests
|
||
|
import py7zr
|
||
|
from threading import Thread
|
||
|
from logger.logger import *
|
||
|
|
||
|
CHUNKSIZE = 1024
|
||
|
|
||
|
|
||
|
class Download_Theard(Thread):
|
||
|
def __init__(self, url, to, progress_callback, onfinish):
|
||
|
self.url = url
|
||
|
self.to = to
|
||
|
self.progress_callback = progress_callback
|
||
|
self.onfinish = onfinish
|
||
|
Thread.__init__(self, daemon=True)
|
||
|
|
||
|
def run(self):
|
||
|
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)
|
||
|
self.onfinish()
|
||
|
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(
|
||
|
i["from"], i["to"], i["progbar"], progfinish))
|
||
|
threads[-1].start()
|