#!/usr/bin/env python """Retrieve files from the list, give new names if present in list.""" from __future__ import division import re import urllib import threading import time import sys class Download: def __init__(self, url, name): self.url = url self.name = name self.last_check = 0.0 class DownloadBag: def __init__(self, downloads): self._downloads = [] for url, name in downloads: self._downloads.append(Download(url, name)) def start(self): threads = [] for d in self._downloads: p = self._createProgress(d) thargs = (d.url, d.name, p) th = threading.Thread(target=urllib.urlretrieve, args=thargs) threads.append(th) th.start() return threads def _progress(self, download, blocks, blk_size, total): t = time.time() if t - download.last_check > 5: if total != -1: print "%s -> %s: %2.1f%%" % (download.url, download.name, blocks*blk_size/total*100) else: print "%s -> %s: %d bytes" % (download.url, download.name, blocks*blk_size) download.last_check = t def _createProgress(self, download): def progf(blocks, blk_size, total): self._progress(download, blocks, blk_size, total) return progf if __name__ == "__main__": spcre = re.compile(r"(.*?)\s+(.*)$") slashre = re.compile(r"(?:.*)/(.*?)$") f = file(sys.argv[1]) #"filelist.txt") filelist = [] for line in f: line = line.strip() m = spcre.match(line) if m: url, name = m.groups() else: m = slashre.match(line) if m: url, name = line, m.group(1) else: print "Incorrect line format:\n%s" % line continue filelist.append((url, name)) f.close() db = DownloadBag(filelist) threads = db.start() for t in threads: t.join()