def download_file(path, url, progress_bar=None): remote_sz = utils.remote_file_size(url) sz = utils.local_file_size(path) req = urllib2.Request(url) req.headers['Range'] = 'bytes=%d-' % sz with open(path, "ab" if sz else "wb") as fd: with closing(urllib2.urlopen(req)) as remote_fd: remote_fd = urllib2.urlopen(req) written = sz while written < remote_sz: block = remote_fd.read(BLOCK_SIZE) fd.write(block) written += len(block) if progress_bar: progress_bar.show(written, remote_sz)
) sys.stdout.write(progress) sys.stdout.flush() def download_audio((i, audio), total, path, lnks_path, m_th=True): filepath = utils.norm_path(path, audio.name) if os.path.exists(filepath): logging.debug("[%d/%d] \"%s\" has been downloaded.", i + 1, total, filepath) else: tmp_name = utils.norm_path(path, audio.id_name) sz = utils.local_file_size(tmp_name) try: remote_sz = utils.remote_file_size(audio.url) except urllib2.URLError as e: logging.critical("Skipping file: %s.\n" + "Can't get access to %s: %s", audio.name, audio.url, e) return logging.debug("[#%d] %s - %d (%.02fM)", i + 1, audio.url, remote_sz, remote_sz / utils.MB) if sz != remote_sz: if logging.getLogger().isEnabledFor(logging.INFO): progress_bar = \ AudioDownloadProgressBar(i, total, m_th, audio.short_name)