import urllib.request
import urllib
import shutil
import os
import os.path
import sys
import time
import threading
class downloadFile:
def __init__(self, downloadLink, downloadPath, onDiskName):
self.downloadSize = urllib.request.urlopen(downloadLink).length
self.downloadLink = downloadLink
self.downloadPath = downloadPath
self.onDiskName = onDiskName
self.hardPath = os.path.join(self.downloadPath, self.onDiskName)
def returnMode(self, returnMode = 'stats'):
if returnMode == 'stats':
return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize]
elif returnMode == 'printedStats':
print('self.downloadLink = ' + self.downloadLink)
print('self.downloadPath = ' + self.downloadPath)
print('self.onDiskName = ' + self.onDiskName)
print('self.downloadSize = ' + self.downloadSize)
print('self.hardPath = ' + self.hardPath)
return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize, self.hardPath]
def executeDownload(self):
self.downloadStart = time.strftime("[%H:%M:%S] ")
with urllib.request.urlopen(self.downloadLink) as response, open(self.hardPath, 'wb') as currentFile:
shutil.copyfileobj(response, currentFile)
currentFile.close()
self.downloadEnd = time.strftime("[%H:%M:%S] ")
def downloadStats(self):
currentFileSize = os.path.getsize(self.hardPath)
percentManifested = int(currentFileSize/(self.downloadSize/100))
return [currentFileSize, percentManifested]
def liveDownloadStats(self):
if os.path.isfile(self.hardPath) == False:
time.sleep(1)
statList = self.downloadStats()
while statList[0] < self.downloadSize:
sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
sys.stdout.flush()
sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
sys.stdout.write("\n")
server = downloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.8/minecraft_server.1.8.jar", "C:/Users/my-username/Desktop/", "minecraftServer.jar")
t1 = threading.Thread(target=server.executeDownload())
t2 = threading.Thread(target=server.liveDownloadStats())
t2.start()
t1.start()
time.sleep(100)
This is supposed to start printing the percentage downloaded of the file being downloaded once the file appears. What I am seeing is that the file appears and then moments later I get the output saying that it is 100% downloaded. I can't see exactly what I'm doing wrong here.