Skip to content

Commit

Permalink
Use progress bar for uncompressed json downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Feb 14, 2015
1 parent 3589a22 commit 7ea7c30
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import json
import math
import misc.progress as pbar
import time
import urllib.error


try:
import requests
except ImportError as e:
Expand Down Expand Up @@ -111,8 +111,8 @@ def download(
cmdenv.DEBUG0(str(f.info()))

# Figure out how much data we have
bytes = int(f.getheader('Content-Length'))
maxBytesLen = len("{:>n}".format(bytes))
contentLength = int(f.getheader('Content-Length'))
maxBytesLen = len("{:>n}".format(contentLength))
fetched = 0
started = time.time()

Expand All @@ -136,9 +136,9 @@ def download(
# download status including, especially, the 100% report.
while True:
duration = time.time() - started
if bytes and duration >= 1:
# estimated bytes per second
rate = math.ceil(bytes / duration)
if contentLength and duration >= 1:
# estimated contentLength per second
rate = math.ceil(contentLength / duration)
# but how much can we download in 1/10s
burstSize = rate / 10
chunkSize += math.ceil((burstSize - chunkSize) * 0.7)
Expand All @@ -148,13 +148,13 @@ def download(
"| {:>5.2f}% "
.format(
localFile,
fetched, bytes,
fetched, contentLength,
rateVal(fetched, duration),
(fetched * 100 / bytes),
(fetched * 100 / contentLength),
len=maxBytesLen
), end='\r')

if fetched >= bytes:
if fetched >= contentLength:
if not cmdenv.quiet:
print()
break
Expand Down Expand Up @@ -196,21 +196,16 @@ def retrieve_json_data(url):
jsData = req.content
else:
totalLength = int(totalLength)
lastDone = None
progBar = pbar.Progress(totalLength, 25)
jsData = bytes()
for data in req.iter_content():
jsData += data
bytesRead = len(jsData)
done = int(50 * bytesRead / total_length)
if done != lastDone:
sys.stdout.write("\r[{:<50s}] {}/{} ".format(
'=' * done,
makeUnit(bytesRead),
progBar.increment(len(data), postfix=lambda: \
" {}/{} ".format(
makeUnit(progBar.value),
makeUnit(totalLength),
))
sys.stdout.flush()
lastDone = done
if lastDone:
print("\n")
))
progBar.clear()

return json.loads(jsData.decode())

0 comments on commit 7ea7c30

Please sign in to comment.