From 644fd0a12e75941e1d8fcdeeced9b3d87628696e Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Sat, 26 Sep 2015 17:07:51 +0000 Subject: Show better progress when downloading images --- tester | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tester b/tester index 777ca96..da50541 100755 --- a/tester +++ b/tester @@ -262,15 +262,26 @@ def download_image(url): path = str(uuid.uuid4()) + '-' + url.rpartition('/')[2] print('[Local] Fetching: ' + url) - response = urllib2.urlopen(url) + file_to_fetch = urllib2.urlopen(url) + total_file_size = int(file_to_fetch.info()['Content-Length']) + + copied_size = 0 + last_percentaje_shown = -1 CHUNK = 16 * 1024 with open(path, 'wb') as f: - print('[Local] Progress:'), + print('[Local] Progress:') while True: - chunk = response.read(CHUNK) + chunk = file_to_fetch.read(CHUNK) if not chunk: break f.write(chunk) - print('-'), + copied_size += len(chunk) + percentaje = (copied_size * 100) / total_file_size + if last_percentaje_shown < percentaje: + last_percentaje_shown = percentaje + print (" Copied %s %% - %s of %s bytes" % (percentaje, + copied_size, + total_file_size)) + print('\n[Local] Saved as: ' + path) return path -- cgit v1.2.1