|
@@ -0,0 +1,68 @@
|
|
1
|
+from boxbranding import getMachineBrand, getMachineName
|
|
2
|
+
|
|
3
|
+from twisted.web import client
|
|
4
|
+from twisted.internet import reactor, defer, ssl
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+class HTTPProgressDownloader(client.HTTPDownloader):
|
|
8
|
+ def __init__(self, url, outfile, headers=None):
|
|
9
|
+ client.HTTPDownloader.__init__(self, url, outfile, headers=headers, agent="Enigma2 HbbTV/1.1.1 (+PVR+RTSP+DL;OpenATV;;;)")
|
|
10
|
+ self.status = None
|
|
11
|
+ self.progress_callback = None
|
|
12
|
+ self.deferred = defer.Deferred()
|
|
13
|
+
|
|
14
|
+ def noPage(self, reason):
|
|
15
|
+ if self.status == "304":
|
|
16
|
+ print reason.getErrorMessage()
|
|
17
|
+ client.HTTPDownloader.page(self, "")
|
|
18
|
+ else:
|
|
19
|
+ client.HTTPDownloader.noPage(self, reason)
|
|
20
|
+
|
|
21
|
+ def gotHeaders(self, headers):
|
|
22
|
+ if self.status == "200":
|
|
23
|
+ if headers.has_key("content-length"):
|
|
24
|
+ self.totalbytes = int(headers["content-length"][0])
|
|
25
|
+ else:
|
|
26
|
+ self.totalbytes = 0
|
|
27
|
+ self.currentbytes = 0.0
|
|
28
|
+ return client.HTTPDownloader.gotHeaders(self, headers)
|
|
29
|
+
|
|
30
|
+ def pagePart(self, packet):
|
|
31
|
+ if self.status == "200":
|
|
32
|
+ self.currentbytes += len(packet)
|
|
33
|
+ if self.totalbytes and self.progress_callback:
|
|
34
|
+ self.progress_callback(self.currentbytes, self.totalbytes)
|
|
35
|
+ return client.HTTPDownloader.pagePart(self, packet)
|
|
36
|
+
|
|
37
|
+ def pageEnd(self):
|
|
38
|
+ return client.HTTPDownloader.pageEnd(self)
|
|
39
|
+
|
|
40
|
+class downloadWithProgress:
|
|
41
|
+ def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
|
|
42
|
+ if hasattr(client, '_parse'):
|
|
43
|
+ scheme, host, port, path = client._parse(url)
|
|
44
|
+ else:
|
|
45
|
+ from twisted.web.client import _URI
|
|
46
|
+ uri = _URI.fromBytes(url)
|
|
47
|
+ scheme = uri.scheme
|
|
48
|
+ host = uri.host
|
|
49
|
+ port = uri.port
|
|
50
|
+ path = uri.path
|
|
51
|
+
|
|
52
|
+ self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
|
|
53
|
+ if scheme == "https":
|
|
54
|
+ self.connection = reactor.connectSSL(host, port, self.factory, ssl.ClientContextFactory())
|
|
55
|
+ else:
|
|
56
|
+ self.connection = reactor.connectTCP(host, port, self.factory)
|
|
57
|
+
|
|
58
|
+ def start(self):
|
|
59
|
+ return self.factory.deferred
|
|
60
|
+
|
|
61
|
+ def stop(self):
|
|
62
|
+ if self.connection:
|
|
63
|
+ print "[stop]"
|
|
64
|
+ self.connection.disconnect()
|
|
65
|
+
|
|
66
|
+ def addProgress(self, progress_callback):
|
|
67
|
+ print "[addProgress]"
|
|
68
|
+ self.factory.progress_callback = progress_callback
|