def download_requests_stream(request_stream, destination, message=None, total_read=0): """This is a facility to download a request with nice progress bars.""" # Doing len(request_stream.content) may defeat the purpose of a # progress bar total_length = 0 if not request_stream.headers.get("Content-Encoding", ""): total_length = int(request_stream.headers.get("Content-Length", "0")) # Content-Length in the case of resuming will be # Content-Length - total_read so we add back up to have the feel of # resuming if os.path.exists(destination): total_length += total_read progress_bar = _init_progress_bar(total_length, destination, message) progress_bar.start() if os.path.exists(destination): mode = "ab" else: mode = "wb" with open(destination, mode) as destination_file: for buf in request_stream.iter_content(1024): destination_file.write(buf) if not is_dumb_terminal(): total_read += len(buf) progress_bar.update(total_read) progress_bar.finish()
def _init_progress_bar(total_length, destination, message=None): if not message: message = "Downloading {!r}".format(os.path.basename(destination)) valid_length = total_length and total_length > 0 if valid_length and is_dumb_terminal(): widgets = [message, " ", Percentage()] maxval = total_length elif valid_length and not is_dumb_terminal(): widgets = [ message, Bar(marker="=", left="[", right="]"), " ", Percentage() ] maxval = total_length elif not valid_length and is_dumb_terminal(): widgets = [message] maxval = UnknownLength else: widgets = [message, AnimatedMarker()] maxval = UnknownLength return ProgressBar(widgets=widgets, maxval=maxval)