From d2f40113440644cac4e609445769538b001cf922 Mon Sep 17 00:00:00 2001 From: Stephen Chen Date: Wed, 17 Jun 2020 02:09:39 +0800 Subject: [PATCH] added progress bar --- download_manager_v2.py | 22 +++++++++++++--------- requirements.txt | 1 + 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/download_manager_v2.py b/download_manager_v2.py index 934b676..faee7b0 100755 --- a/download_manager_v2.py +++ b/download_manager_v2.py @@ -1,11 +1,12 @@ import requests from os import path import boto3 +import sys +import progressbar if __name__ == '__main__': with open('archive_links.txt') as links_f: for url in links_f: url = url.strip() - url = 'https://file-examples.com/wp-content/uploads/2019/09/file-sample_300kB.rtf' if not url: continue f_name = url.rsplit('/', 1)[-1] @@ -15,17 +16,20 @@ # below is downloading content with open(f_path, 'wb') as f: - response = requests.get(url, allow_redirects=True) + response = requests.get(url, allow_redirects=True, stream=True) total_length = response.headers.get('content-length') if total_length is None: # no content length header f.write(response.content) else: - dl = 0 total_length = int(total_length) - for data in response.iter_content(chunk_size=4096): - dl += len(data) - f.write(data) - done = int(50 * dl / total_length) - sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50 - done))) - sys.stdout.flush() + with progressbar.ProgressBar(max_value=total_length) as bar: + dl = 0 + for data in response.iter_content(chunk_size=4096): + dl += len(data) + f.write(data) + # noinspection PyBroadException + try: + bar.update(dl) + except: + pass diff --git a/requirements.txt b/requirements.txt index 749f046..396758f 100755 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ requests googletrans googlemaps boto3 +progressbar2