ef download_requests_stream(request_stream, destination, message=None):
"""This is a facility to download a request with nice progress bars."""
if not message:
message = 'Downloading {!r}'.format(os.path.basename(destination))
total_length = int(request_stream.headers.get('Content-Length', '0'))
if total_length:
progress_bar = ProgressBar(
widgets=[message,
Bar(marker='=', left='[', right=']'),
' ', Percentage()],
maxval=total_length)
else:
progress_bar = ProgressBar(
widgets=[message, AnimatedMarker()],
maxval=UnknownLength)
total_read = 0
progress_bar.start()
with open(destination, 'wb') as destination_file:
for buf in request_stream.iter_content(1024):
destination_file.write(buf)
total_read += len(buf)
progress_bar.update(total_read)
progress_bar.finish()