2017-01-11 14:51:31 +01:00
|
|
|
from urllib import request
|
2017-01-18 17:43:46 +01:00
|
|
|
import math, sys
|
2017-01-11 14:51:31 +01:00
|
|
|
|
|
|
|
def print_progress_bar(percent_progress=0, char_size=50, clear_line=True):
|
|
|
|
|
|
|
|
bar_progress = math.floor(char_size*(percent_progress/100))
|
|
|
|
bar = "=" * bar_progress + " " * (char_size - bar_progress)
|
|
|
|
|
|
|
|
if clear_line:
|
|
|
|
sys.stdout.write(u"\u001b[1000D")
|
|
|
|
sys.stdout.write("[{:s}] {:d}%".format(bar, int(percent_progress)))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def download_file(file_url, dest_path, bulk_size = 8192):
|
|
|
|
|
|
|
|
req = request.urlopen(file_url)
|
|
|
|
meta = req.info()
|
|
|
|
file_size = int(meta.get('Content-Length'))
|
|
|
|
|
|
|
|
with open(dest_path, 'wb') as dest_file:
|
|
|
|
print('Downloading "{:s}". Size: {:d}b'.format(file_url, file_size))
|
|
|
|
downloaded_size = 0
|
|
|
|
while True:
|
|
|
|
buff = req.read(bulk_size)
|
|
|
|
if not buff:
|
|
|
|
break
|
|
|
|
downloaded_size += len(buff)
|
|
|
|
dest_file.write(buff)
|
|
|
|
progress = downloaded_size/file_size*100
|
|
|
|
print_progress_bar(progress)
|
|
|
|
dest_file.close()
|
|
|
|
# Add linebreak
|
|
|
|
print("")
|