I'm trying to make a simple code that downloads 4365 files (.mp4, .wmv, .jpeg & .pdf) from a .json file, the download part its done, but I wanted to get a reporthook telling me the %, Mb, speed an duration.
Even though sometimes it runs, I'm getting errors:
file "dw.py", line 21, in reporthook
speed = int(progress_size / (1024 * duration))
ZeroDivisionError: float division by zero
This is my code:
import urllib.request
import json
import sys
import time
with open('finalsinbin.json') as json_data: # importar toda la lista de videos
items = json.load(json_data)
def reporthook(count, block_size, total_size):
global start_time
if count == 0:
start_time = time.time()
return
duration = time.time() - start_time
progress_size = int(count * block_size)
speed = int(progress_size / (1024 * duration))
percent = min(int(count*blockSize*100/totalSize),100)
sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" % (percent, progress_size / (1024 * 1024), speed, duration))
sys.stdout.flush()
def batch(startAt, stopAt):
a=0
for i in items:
a+=1
if a < startAt or a > stopAt:
continue
num = i['id']
url = i['url']
typ = i['type']
urlfinal = (url + "/" + str(num) + typ)
filename = (str(num) + typ)
print( '[%d] Descargando el archivo %s...' % (a, str(num) + typ))
urllib.request.urlretrieve(urlfinal, filename, reporthook)
batch(1, 100) #download file from n to N
Try to use time.perf_counter()
instead of time.time()
. At least in my environment, time.perf_counter()
never went to 0 as below.
import time
def time_perf_counter():
start_time = time.perf_counter()
return time.perf_counter() - start_time
def time_time():
start_time = time.time()
return time.time() - start_time
def test_timer(timer, n):
count = 0
for i in range(n):
if timer() == 0:
count += 1
return count
n_test = 100000
print('{:>6s} / {:>6s}'.format(
'# fail',
'# test'))
print('{:6d} / {:6d}'.format(
test_timer(time_perf_counter, n_test),
n_test))
print('{:6d} / {:6d}'.format(
test_timer(time_time, n_test),
n_test))
The results are, for example:
# fail / # test
0 / 100000
89109 / 100000
My environments: MBP High Sierra, Python 3.6.3