Fixed Some Bugs and Add the Multi-Thread Download Module

This commit is contained in:
Bigsk 2020-10-30 18:39:03 +08:00
parent 88fe5ae9c6
commit 5b20b901c8

77
main.py
View File

@ -1,10 +1,29 @@
#------------------#
import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hashlib,random,subprocess,math,zipfile
import os,requests,json,time,sys,psutil,string,platform,glob,getpass,shutil,hashlib,random,subprocess,math,zipfile,threading
#------------------#
class MultiThreadDownload(threading.Thread):
'''class for Multi-Thread Download'''
def __init__(self,url,startpos,endpos,f):
super(MultiThreadDownload,self).__init__()
self.url = url
self.startpos = startpos
self.endpos = endpos
self.fd = f
def download(self):
headers = {"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):
def __int__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0",AuthWay="offline",Account="Steve",Password="",GamePath=".minecraft",Memory="1GB",Version="",Java="",DownloadFrom="",DownloadTo=""):
self.__LauncherName=LauncherName
self.__LauncherVersion=LauncherVersion
'''The Main Class of the Launcher Core'''
'''AuthWay="offline",Account="Steve",Password="",GamePath=".minecraft",Memory="1GB",Version="",Java=""'''
'''
self.GetAuthWay(AuthWay)
self.GetAccoutn(Account)
self.__Password=Password
@ -12,7 +31,10 @@ class GMCLCore(object):
self.GetMemory(Memory)
self.__Version=Version
self.__Java=Java
self.Download(DownloadFrom,DownloadTo)
'''
def __init__(self,LauncherName="GMCLCore",LauncherVersion="A0.2.0"):
self.__LauncherName=LauncherName
self.__LauncherVersion=LauncherVersion
self.__UserAgent={'User-Agent':LauncherName+'/'+LauncherVersion+' ((GMCL Core Alpha 0.2.0;Alpha))'}
def GetAuthWay(self,AuthWay):
if(AuthWay=="offline"):
@ -68,7 +90,7 @@ class GMCLCore(object):
self.__ID=Account
self.__UUID=self.GetUUID(Account)
self.__Token=""
def GetJavaPaht(self):
def GetJavaPath(self):
if(platform.system()=="Windows"):
path="C:/"
elif(platform.system()=="Linux"):
@ -82,10 +104,45 @@ class GMCLCore(object):
if("javaw.exe" in os.path.split(FullDir)[1]):
return FullDir
i+=1
if(i=1):
if(i==1):
break
def Download(self,DownloadFrom,DownloadTo):
if(DownloadFrom=="" or DownloadTo==""):
pass
def SetJavaPath(self,path):
if(path==None):
return "Error:Wrong local address for java."
else:
self.__JAVA=path
def Download(self,DownloadFrom,DownloadTo,ThreadNum=3):
if(DownloadFrom=="" or DownloadTo==""):
return "Error:Wrong online address or local address for download."
else:
url = DownloadFrom
filename = DownloadTo
filesize = int(requests.head(url).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)
t.start()
mtd_list.append(t)
for i in mtd_list:
i.join()
launcher=GMCLCore("AAA","V250")
launcher.Download("http://mirrors.163.com/centos/8.2.2004/isos/x86_64/CentOS-8.2.2004-x86_64-boot.iso","aa.iso")