Handle Google web servers returning no file size info

This commit is contained in:
Jay Lee
2014-09-30 09:04:06 -04:00
parent f233f7505c
commit e46af32638

10
gam.py
View File

@ -695,7 +695,10 @@ def geturl(url, dst):
u = urllib2.urlopen(url) u = urllib2.urlopen(url)
f = open(dst, 'wb') f = open(dst, 'wb')
meta = u.info() meta = u.info()
file_size = int(meta.getheaders(u'Content-Length')[0]) try:
file_size = int(meta.getheaders(u'Content-Length')[0])
except IndexError:
file_size = -1
file_size_dl = 0 file_size_dl = 0
block_sz = 8192 block_sz = 8192
while True: while True:
@ -704,7 +707,10 @@ def geturl(url, dst):
break break
file_size_dl += len(buffer) file_size_dl += len(buffer)
f.write(buffer) f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) if file_size != -1:
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
else:
status = r"%10d [unknown size]" % (file_size_dl)
status = status + chr(8)*(len(status)+1) status = status + chr(8)*(len(status)+1)
print status, print status,
f.close() f.close()