def download(link, file_dst, label):
"""Create HTTP request to download a file"""
# delimit labels to be at same size and a better visualization
# when printing the progres bar
fill_out = 20 - len(label)
extra = " " * fill_out
pbar_label = label + extra
LOG.debug("Starting download %s" % link)
try:
req = urllib2.urlopen(link)
remote_file_size = int(req.headers.get('content-length'))
CHUNK = 16 * 1024
with open(file_dst, 'wb') as fp:
with progressbar(length=remote_file_size,
fill_char=style('#', fg='green'),
empty_char=' ',
label=pbar_label,
show_percent=True) as bar:
while True:
chunk = req.read(CHUNK)
if not chunk: break
fp.write(chunk)
bar.update(len(chunk))
LOG.debug("Download complete, file %s saved locally" % file_dst)
except (HTTPError, RequestException, Exception) as ex:
raise ex
评论列表
文章目录