ba5bf0a63c965801c3d012e872e8bc97e8fef049
[openblackhole/openblackhole-enigma2.git] / lib / 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="STB 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                 parsed = urlparse(url)
41                 scheme = parsed.scheme
42                 host = parsed.hostname
43                 port = parsed.port or (443 if scheme == 'https' else 80)
44                 self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
45                 self.connection = reactor.connectTCP(host, port, self.factory)
46
47         def start(self):
48                 return self.factory.deferred
49
50         def stop(self):
51                 print "[stop]"
52                 self.connection.disconnect()
53
54         def addProgress(self, progress_callback):
55                 print "[addProgress]"
56                 self.factory.progress_callback = progress_callback