Skip to content

Instantly share code, notes, and snippets.

@jcarlosroldan
Last active October 2, 2018 19:24
Show Gist options
  • Save jcarlosroldan/66355df20d706b9ad6ec457a11654e96 to your computer and use it in GitHub Desktop.
Save jcarlosroldan/66355df20d706b9ad6ec457a11654e96 to your computer and use it in GitHub Desktop.
Download file to local with a nice progress bar and time estimate.
from datetime import timedelta
from requests import get
from time import time
from sys import stdout
def bytes_to_human(size, decimal_places=2):
for unit in ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]:
if size < 1024: break
size /= 1024
return "%s%sB" % (round(size, decimal_places), unit)
def seconds_to_human(seconds):
return str(timedelta(seconds=int(seconds))).zfill(8)
def download_file(url, path=None, chunk_size=10**5):
""" Downloads a file keeping track of the progress. """
if path == None: path = url.split("/")[-1]
r = get(url, stream=True)
total_bytes = int(r.headers.get('content-length'))
bytes_downloaded = 0
start = time()
print("Downloading %s (%s)" % (url, bytes_to_human(total_bytes)))
with open(path, "wb") as fp:
for chunk in r.iter_content(chunk_size=chunk_size):
if not chunk: continue
fp.write(chunk)
bytes_downloaded += len(chunk)
percent = bytes_downloaded / total_bytes
bar = ("█" * int(percent * 32)).ljust(32)
time_delta = time() - start
eta = seconds_to_human((total_bytes - bytes_downloaded) * time_delta / bytes_downloaded)
avg_speed = bytes_to_human(bytes_downloaded / time_delta).rjust(9)
stdout.flush()
stdout.write("\r %6.02f%% |%s| %s/s eta %s" % (100 * percent, bar, avg_speed, eta))
print()
if __name__ == "__main__":
download_file("https://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment