From b4319e5b8ea18b53ac6b5da578cd5e75260e1f06 Mon Sep 17 00:00:00 2001 From: Bigsk Date: Sat, 21 Nov 2020 15:29:48 +0800 Subject: [PATCH] Improving Download --- core.py | 62 ++++++++++----------------------------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/core.py b/core.py index 7ee0594..0bf5af5 100644 --- a/core.py +++ b/core.py @@ -2,24 +2,6 @@ import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hashlib,random,subprocess,math,zipfile,threading #------------------# -class MultiThreadDownload(threading.Thread): - '''The Class for Multi-Thread Download''' - '''Get from Internet and improved by Ghink Network Studio''' - def __init__(self,url,startpos,endpos,f,UA): - super(MultiThreadDownload,self).__init__() - self.url=url - self.startpos=startpos - self.endpos=endpos - self.fd=f - self.UA=UA - def download(self): - headers=self.UA.update({"Range":"bytes=%s-%s"%(self.startpos,self.endpos)}) - res=requests.get(self.url,headers=headers) - self.fd.seek(self.startpos) - self.fd.write(res.content) - def run(self): - self.download() - class GMCLCore(object): '''The Main Class of the Launcher Core''' def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0",LogOutput=False): @@ -76,18 +58,18 @@ class GMCLCore(object): self.Log("info","Successful.","MakeDir") else: return self.Log("warn","Exist.","MakeDir") - def SearchFile(self,Path,Tag,Num=1): + def SearchFile(self,Path,Tag,Depth=1): '''The function which was used to search files''' ''':Path The root path you want to search''' ''':Tag The name keyword of the file you want to search''' - ''':Num How much result you want to get,you should set as "all" if you want to return all result,the default value is 1''' + ''':Depth The depth of dir you want to search,you should set as "all" if you want to search all dirs,the default value is 1''' List=[] i=0 for root,dirs,files in os.walk(Path,topdown=True): if Tag in files: i+=1 List.append(root+'\\'+Tag) - if(i==Num): + if(i==Depth): break else: pass @@ -152,7 +134,7 @@ class GMCLCore(object): self.__AuthWay="online" self.Log("info","Success.","SetAuthWay") else: - self.Log("warn","Wrong Auth Way.","SetAccount") + return self.Log("warn","Wrong Auth Way.","SetAccount") def SetRecomMem(self): '''The recommendation memory value set function''' self.__Memory = float(psutil.virtual_memory().free / 1024 ** 3) * 0.8 @@ -161,13 +143,13 @@ class GMCLCore(object): '''The java path set function''' ''':Path Your java path''' if(path==None): - self.Log("warn","Wrong local address for java.","SetJavaPath") + return self.Log("warn","Wrong local address for java.","SetJavaPath") else: if(os.path.exists(path)): self.__JAVA=path self.Log("info","Success.","SetJavaPath") else: - self.Log("warn","Wrong local address for java.","SetJavaPath") + return self.Log("warn","Wrong local address for java.","SetJavaPath") def SetGameMem(self,Memory): '''The game memory value set function''' ''':Memory The memory of the game,the unit is GiBytes''' @@ -175,38 +157,14 @@ class GMCLCore(object): self.__Memory=float(Memory) self.Log("info","Success.","SetGameMem") except: - self.Log("warn","Wrong parameter for game memory.","SetGameMem") - def Download(self,DownloadFrom,DownloadTo,ThreadNum=3): + return self.Log("warn","Wrong parameter for game memory.","SetGameMem") + def Download(self,DownloadFrom,DownloadTo,ThreadNum=20): '''The multi-thread download function''' ''':DownloadFrom The online url for the file''' ''':DownloadTo The local path for the file''' - ''':ThreadNum The number of the thread,the default value is 3''' + ''':ThreadNum The number of the thread,the default value is 20''' if(DownloadFrom=="" or DownloadTo==""): self.Log("warn","Wrong online address or local address for download.","Download") else: - url=DownloadFrom - filename=DownloadTo - filesize=int(requests.head(url,headers=self.__UserAgent).headers['Content-Length']) - threadnum=ThreadNum - threading.BoundedSemaphore(threadnum) - step=filesize // threadnum - mtd_list=[] - start=0 - end=-1 - tempf=open(filename,'w') - tempf.close() - with open(filename,'rb+') as f: - fileno=f.fileno() - while end < filesize -1: - start=end +1 - end=start + step -1 - if end > filesize: - end=filesize - dup=os.dup(fileno) - fd=os.fdopen(dup,'rb+',-1) - t=MultiThreadDownload(url,start,end,fd,self.__UserAgent) - t.start() - mtd_list.append(t) - for i in mtd_list: - i.join() self.Log("info","Success.","Download") + return (str(time.time()-StartTime),filesize // (time.time()-StartTime))