def download(cap,ui): """ Downloads cap, it will try all the urls untill one works. """ if os.path.exists(cap.capname+".cap"): if integrity(cap): return True else: os.remove(cap.capname+".cap") chunkSize=1024*8 succeeded=False for url in cap.urls: try: opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'cap-python/'+version)] u=opener.open(url) totalSize = float(u.headers['content-length']) sizeString = "%.2f Bytes" % (totalSize * 1.00) if totalSize > 1024: sizeString = "%.2f Kb" % (totalSize/1024.00) if totalSize > (1048576): sizeString = "%.2f Mb" % (totalSize/1048576.00) if totalSize > (1073741824): sizeString = "%.2f Gb ahh!" % (totalSize/1073741824.00) if not ui.downloadProgress(0, 0, cap.capname+'.cap',sizeString): return False if totalSize > 0: f=open(cap.capname+".cap",'w') current=0 while current <= totalSize: f.write(u.read(chunkSize)) if not ui.downloadProgress(int(100.00*(current/totalSize)), 1, file,totalSize): return False current+=chunkSize if not ui.downloadProgress(0, 2, file,totalSize): return False f.close() u.close() succeeded=True break except urllib2.HTTPError, e: if not ui.downloadProgress(0, -1, cap.capname,0): return False if e.code == 404: errors.append("download error: '"+url+"' not found ( 404 ).") elif e.code == 403: errors.append("download error: Cannot access '"+url+"', permission denied ( 403 )") else: errors.append("urlib2 error: "+e.code) return succeeded