1 from twisted.web import client
2 from twisted.internet import reactor, defer
3 from twisted.python import failure
5 class HTTPProgressDownloader(client.HTTPDownloader):
6 def __init__(self, url, outfile, headers=None):
7 client.HTTPDownloader.__init__(self, url, outfile, headers=headers, agent="Dreambox HTTP Downloader")
9 self.progress_callback = None
10 self.deferred = defer.Deferred()
12 def noPage(self, reason):
13 if self.status == "304":
14 print reason.getErrorMessage()
15 client.HTTPDownloader.page(self, "")
17 client.HTTPDownloader.noPage(self, reason)
19 def gotHeaders(self, headers):
20 if self.status == "200":
21 if headers.has_key("content-length"):
22 self.totalbytes = int(headers["content-length"][0])
25 self.currentbytes = 0.0
26 return client.HTTPDownloader.gotHeaders(self, headers)
28 def pagePart(self, packet):
29 if self.status == "200":
30 self.currentbytes += len(packet)
31 if self.totalbytes and self.progress_callback:
32 self.progress_callback(self.currentbytes, self.totalbytes)
33 return client.HTTPDownloader.pagePart(self, packet)
36 return client.HTTPDownloader.pageEnd(self)
38 class downloadWithProgress:
39 def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
40 scheme, host, port, path = client._parse(url)
41 self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
42 self.connection = reactor.connectTCP(host, port, self.factory)
45 return self.factory.deferred
49 self.connection.disconnect()
51 def addProgress(self, progress_callback):
53 self.factory.progress_callback = progress_callback