Skip to content

Commit e30671f

Browse files
committed
add downloading bar
1 parent 3de6ced commit e30671f

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

package.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import logging
3030
import os
3131
import sys
32-
3332
import requests
34-
3533
import archive
34+
from tqdm import tqdm
3635

3736
"""Template for creating a new file"""
3837

@@ -207,20 +206,17 @@ def download(self, ver, path, url_from_srv):
207206
print('Start to download package : %s ' % filename.encode("utf-8"))
208207

209208
while True:
210-
# print("retry_count : %d"%retry_count)
211209
try:
212210
r = requests.get(url_from_srv, stream=True, headers=headers)
213-
211+
total_size = int(r.headers.get('content-length', 0))
214212
flush_count = 0
215213

216214
with open(path, 'wb') as f:
217-
for chunk in r.iter_content(chunk_size=1024):
215+
for chunk in tqdm(r.iter_content(chunk_size=1024), total=total_size//1024, unit='KB'):
218216
if chunk:
219217
f.write(chunk)
220218
f.flush()
221219
flush_count += 1
222-
sys.stdout.write("\rDownloding %d KB" % flush_count)
223-
sys.stdout.flush()
224220

225221
retry_count = retry_count + 1
226222

@@ -233,8 +229,7 @@ def download(self, ver, path, url_from_srv):
233229
if os.path.isfile(path):
234230
os.remove(path)
235231
if retry_count > 5:
236-
print(
237-
"error: Have tried downloading 5 times.\nstop Downloading file :%s" % path)
232+
print("error: Have tried downloading 5 times.\nstop Downloading file :%s" % path)
238233
if os.path.isfile(path):
239234
os.remove(path)
240235
ret = False
@@ -249,5 +244,3 @@ def download(self, ver, path, url_from_srv):
249244
os.remove(path)
250245
return False
251246
return ret
252-
253-

0 commit comments

Comments
 (0)