enigma2 (20101216 rel31 -> 20110928 rel32)
[enigma2.git] / usr / lib / enigma2 / python / Tools / Downloader.py
1 from twisted.web import client
2 from twisted.internet import reactor, defer
3 from twisted.python import failure
4
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")
8                 self.status = None
9                 self.progress_callback = None
10                 self.deferred = defer.Deferred()
11
12         def noPage(self, reason):
13                 if self.status == "304":
14                         print reason.getErrorMessage()
15                         client.HTTPDownloader.page(self, "")
16                 else:
17                         client.HTTPDownloader.noPage(self, reason)
18
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])
23                         else:
24                                 self.totalbytes = 0
25                         self.currentbytes = 0.0
26                 return client.HTTPDownloader.gotHeaders(self, headers)
27
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)
34
35         def pageEnd(self):
36                 return client.HTTPDownloader.pageEnd(self)
37
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)
43
44         def start(self):
45                 return self.factory.deferred
46
47         def stop(self):
48                 print "[stop]"
49                 self.connection.disconnect()
50
51         def addProgress(self, progress_callback):
52                 print "[addProgress]"
53                 self.factory.progress_callback = progress_callback