Progress bar for download

This commit is contained in:
Phyks 2014-05-14 23:07:06 +02:00
parent 7f1fb2eb54
commit 9eaf790983

View File

@ -5,6 +5,7 @@ import isbntools
import re import re
import requesocks as requests # Requesocks is requests with SOCKS support import requesocks as requests # Requesocks is requests with SOCKS support
import subprocess import subprocess
import sys
import arxiv2bib as arxiv_metadata import arxiv2bib as arxiv_metadata
import tools import tools
import params import params
@ -26,6 +27,17 @@ def download(url):
} }
try: try:
r = requests.get(url, proxies=r_proxy) r = requests.get(url, proxies=r_proxy)
size = int(r.headers['Content-Length'].strip())
dl = ""
dl_size = 0
for buf in r.iter_content(1024):
if buf:
dl += buf
dl_size += len(buf)
done = int(50 * dl_size / size)
sys.stdout.write("\r[%s%s]"%('='*done,' '*(50-done)))
sys.stdout.write(" "+str(int(float(done)/52*100))+"%")
sys.stdout.flush()
contenttype = False contenttype = False
if 'pdf' in r.headers['content-type']: if 'pdf' in r.headers['content-type']:
contenttype = 'pdf' contenttype = 'pdf'
@ -35,7 +47,7 @@ def download(url):
if r.status_code != 200 or contenttype is False: if r.status_code != 200 or contenttype is False:
continue continue
return r.content, contenttype return dl, contenttype
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
tools.warning("Unable to get "+url+" using proxy "+proxy+". It " + tools.warning("Unable to get "+url+" using proxy "+proxy+". It " +
"may not be available.") "may not be available.")