From 49e7b8987019fb514cb16e45adc26f5f6a914e98 Mon Sep 17 00:00:00 2001 From: Bigsk Date: Sat, 21 Nov 2020 18:50:03 +0800 Subject: [PATCH] Use Aria2C to deal Download tasks,Use EnableMultiThreadDownload() To enable it --- core.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/core.py b/core.py index 0bf5af5..516472b 100644 --- a/core.py +++ b/core.py @@ -15,9 +15,14 @@ class GMCLCore(object): self.__UserAgent={'User-Agent':LauncherName+'/'+LauncherVersion+' ((GMCL Core Alpha 0.2.0;Alpha))'} self.__Log=[] self.__AuthWay="" + self.__MultiThreadDownload=False self.Log("info","Successful.","__init__") def __del__(self): self.Log("info","Successful.","__del__") + def EnableMultiThreadDownload(self): + '''The function which was used to enable the function of multi-thread download''' + self.__MultiThreadDownload=True + self.Log("info","Successful.","EnableMultiThreadDownload") def Log(self,Type,Text,Function="Anonymous Function",ErrorType="TypeError"): '''The function which was used to manager the log output system''' ''':Type Type of the log info''' @@ -166,5 +171,28 @@ class GMCLCore(object): if(DownloadFrom=="" or DownloadTo==""): self.Log("warn","Wrong online address or local address for download.","Download") else: + StartTime=time.time() + FileSize=int(requests.head(DownloadFrom,headers=self.__UserAgent).headers['Content-Length']) + if(self.__MultiThreadDownload==True): + if(platform.system()=="Windows"): + if(os.path.isfile("aria2c.exe")): + os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"") + else: + if(os.path.isdir("C:\\Program Files (x86)")): + with open("aria2c.exe","wb") as FileObject: + FileObject.write(requests.get("https://resource.ghink.net/aria2c/win64/aria2c.exe",headers=self.__UserAgent).content) + else: + with open("aria2c.exe","wb") as FileObject: + FileObject.write(requests.get("https://resource.ghink.net/aria2c/win32/aria2c.exe",headers=self.__UserAgent).content) + os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"") + else: + os.system("aria2c.exe -o "+DownloadTo+" "+DownloadFrom+" -U \""+self.__UserAgent['User-Agent']+"\"") + else: + with open(DownloadTo,"wb") as FileObject: + FileObject.write(requests.get(DownloadFrom).content) self.Log("info","Success.","Download") - return (str(time.time()-StartTime),filesize // (time.time()-StartTime)) + return (str(time.time()-StartTime),FileSize // (time.time()-StartTime)) + +launcher=GMCLCore() +launcher.EnableMultiThreadDownload() +launcher.Download("http://mirrors.163.com/centos/8-stream/isos/x86_64/CentOS-Stream-8-x86_64-20201030-boot.iso","test.iso") \ No newline at end of file