Tamarin/lib/web.py

34 lines
1.0 KiB
Python
Raw Normal View History

from urllib import request
import math, sys
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("")