injectorHub/lib/downloader.py
2024-05-03 09:42:03 +08:00

72 lines
2.1 KiB
Python

from re import T
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, fname):
self.url = url
self.to = to
self.progress_callback = progress_callback
self.fname = fname
self.onfinish = onfinish
Thread.__init__(self, daemon=True)
def run(self):
finished = False
if (os.path.exists("software"+os.sep+self.fname)):
self.onfinish()
finished = True
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)
if (finished == False):
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, i["name"]))
threads[-1].start()