Improving Download
This commit is contained in:
parent
ce21a4ce29
commit
b4319e5b8e
62
core.py
62
core.py
@ -2,24 +2,6 @@
|
|||||||
import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hashlib,random,subprocess,math,zipfile,threading
|
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):
|
class GMCLCore(object):
|
||||||
'''The Main Class of the Launcher Core'''
|
'''The Main Class of the Launcher Core'''
|
||||||
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0",LogOutput=False):
|
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0",LogOutput=False):
|
||||||
@ -76,18 +58,18 @@ class GMCLCore(object):
|
|||||||
self.Log("info","Successful.","MakeDir")
|
self.Log("info","Successful.","MakeDir")
|
||||||
else:
|
else:
|
||||||
return self.Log("warn","Exist.","MakeDir")
|
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'''
|
'''The function which was used to search files'''
|
||||||
''':Path The root path you want to search'''
|
''':Path The root path you want to search'''
|
||||||
''':Tag The name keyword of the file 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=[]
|
List=[]
|
||||||
i=0
|
i=0
|
||||||
for root,dirs,files in os.walk(Path,topdown=True):
|
for root,dirs,files in os.walk(Path,topdown=True):
|
||||||
if Tag in files:
|
if Tag in files:
|
||||||
i+=1
|
i+=1
|
||||||
List.append(root+'\\'+Tag)
|
List.append(root+'\\'+Tag)
|
||||||
if(i==Num):
|
if(i==Depth):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
@ -152,7 +134,7 @@ class GMCLCore(object):
|
|||||||
self.__AuthWay="online"
|
self.__AuthWay="online"
|
||||||
self.Log("info","Success.","SetAuthWay")
|
self.Log("info","Success.","SetAuthWay")
|
||||||
else:
|
else:
|
||||||
self.Log("warn","Wrong Auth Way.","SetAccount")
|
return self.Log("warn","Wrong Auth Way.","SetAccount")
|
||||||
def SetRecomMem(self):
|
def SetRecomMem(self):
|
||||||
'''The recommendation memory value set function'''
|
'''The recommendation memory value set function'''
|
||||||
self.__Memory = float(psutil.virtual_memory().free / 1024 ** 3) * 0.8
|
self.__Memory = float(psutil.virtual_memory().free / 1024 ** 3) * 0.8
|
||||||
@ -161,13 +143,13 @@ class GMCLCore(object):
|
|||||||
'''The java path set function'''
|
'''The java path set function'''
|
||||||
''':Path Your java path'''
|
''':Path Your java path'''
|
||||||
if(path==None):
|
if(path==None):
|
||||||
self.Log("warn","Wrong local address for java.","SetJavaPath")
|
return self.Log("warn","Wrong local address for java.","SetJavaPath")
|
||||||
else:
|
else:
|
||||||
if(os.path.exists(path)):
|
if(os.path.exists(path)):
|
||||||
self.__JAVA=path
|
self.__JAVA=path
|
||||||
self.Log("info","Success.","SetJavaPath")
|
self.Log("info","Success.","SetJavaPath")
|
||||||
else:
|
else:
|
||||||
self.Log("warn","Wrong local address for java.","SetJavaPath")
|
return self.Log("warn","Wrong local address for java.","SetJavaPath")
|
||||||
def SetGameMem(self,Memory):
|
def SetGameMem(self,Memory):
|
||||||
'''The game memory value set function'''
|
'''The game memory value set function'''
|
||||||
''':Memory The memory of the game,the unit is GiBytes'''
|
''':Memory The memory of the game,the unit is GiBytes'''
|
||||||
@ -175,38 +157,14 @@ class GMCLCore(object):
|
|||||||
self.__Memory=float(Memory)
|
self.__Memory=float(Memory)
|
||||||
self.Log("info","Success.","SetGameMem")
|
self.Log("info","Success.","SetGameMem")
|
||||||
except:
|
except:
|
||||||
self.Log("warn","Wrong parameter for game memory.","SetGameMem")
|
return self.Log("warn","Wrong parameter for game memory.","SetGameMem")
|
||||||
def Download(self,DownloadFrom,DownloadTo,ThreadNum=3):
|
def Download(self,DownloadFrom,DownloadTo,ThreadNum=20):
|
||||||
'''The multi-thread download function'''
|
'''The multi-thread download function'''
|
||||||
''':DownloadFrom The online url for the file'''
|
''':DownloadFrom The online url for the file'''
|
||||||
''':DownloadTo The local path 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==""):
|
if(DownloadFrom=="" or DownloadTo==""):
|
||||||
self.Log("warn","Wrong online address or local address for download.","Download")
|
self.Log("warn","Wrong online address or local address for download.","Download")
|
||||||
else:
|
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")
|
self.Log("info","Success.","Download")
|
||||||
|
return (str(time.time()-StartTime),filesize // (time.time()-StartTime))
|
||||||
|
Reference in New Issue
Block a user