Python's urllib.request.urlretrieve() accepts a function that will be called when each chunk is fetched from the network.
The function is called with three arguments: a progressive identifier of the chunk, its size and the total size of the download.
Given those three information can I compute the number of bytes already fetched? This will be used to compute the progress of the download.
I'm tempted to do chunk_number * chunk_size / download_size
, but I'm not sure the chunk size is constant for all the chunks.
You can keep a running total of all the sizes you've seen so far.
Try this:
import urllib.request
def my_downloader(url, filename = None):
running_total = 0
def my_reporthook(count, size, total):
nonlocal running_total
running_total += size
print ("{}%".format(100*running_total//total))
return urllib.request.urlretrieve(url, filename, my_reporthook)
print (my_downloader('https://www.gutenberg.org/files/55146/55146-h.zip'))