diff options
author | Pedro Alvarez <pedro.alvarez@codethink.co.uk> | 2015-09-26 17:07:51 +0000 |
---|---|---|
committer | Pedro Alvarez <pedro.alvarez@codethink.co.uk> | 2015-09-26 17:07:51 +0000 |
commit | 644fd0a12e75941e1d8fcdeeced9b3d87628696e (patch) | |
tree | 6ead5bf7503f8794713d83d263cfd602a37405aa | |
parent | 12f1f62cda10e9464b0a7498048257b69696e01a (diff) | |
download | ciat-tester-644fd0a12e75941e1d8fcdeeced9b3d87628696e.tar.gz |
Show better progress when downloading images
-rwxr-xr-x | tester | 19 |
1 files changed, 15 insertions, 4 deletions
@@ -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 |