[MerlinSkinThemes] - add aarch64 support by dreamer-flt
[enigma2-plugins.git] / netcaster / src / StreamInterface.py
1 from twisted.internet import reactor
2 from twisted.web.client import HTTPClientFactory, HTTPPageDownloader, _parse
3
4 valid_types = ("MP3","PLS") #list of playable mediatypes
5
6 def getPage(url, contextFactory=None, *args, **kwargs):
7         scheme, host, port, path = _parse(url)
8         factory = LimitedHTTPClientFactory(url, *args, **kwargs)
9         if scheme == 'https':
10                 from twisted.internet import ssl 
11                 if contextFactory is None:
12                         contextFactory = ssl.ClientContextFactory()
13                 reactor.connectSSL(host, port, factory, contextFactory)
14         else:
15                 reactor.connectTCP(host, port, factory)
16         return factory.deferred
17
18 class LimitedHTTPClientFactory(HTTPClientFactory):
19
20         LIMIT = 1024
21
22         protocol = HTTPPageDownloader
23
24         def __init__(self, *args, **kwargs):
25                 HTTPClientFactory.__init__(self, *args, **kwargs)
26                 self.curlength = 0
27                 self.buf = ""
28
29         def buildProtocol(self, addr):
30                 self.p = HTTPClientFactory.buildProtocol(self, addr)
31                 return self.p
32
33         def pageStart(self, p):
34                 pass
35
36         def pagePart(self, d):
37                 if self.status == '200':
38                         self.curlength += len(d)
39                         if self.curlength >= self.LIMIT:
40                                 print "[LimitedHTTPClientFactory] reached limit"
41                                 # XXX: timing out here is pretty hackish imo
42                                 self.p.timeout()
43                                 return
44                 self.buf += d
45
46         def pageEnd(self):
47                 if self.waiting:
48                         self.waiting = 0
49                         self.deferred.callback(self.buf)
50
51 class StreamInterface:
52     def __init__(self,session,cbListLoaded=None):
53         self.session = session
54         self.cbListLoaded = cbListLoaded
55
56         self.list= [] # contains the streams in this iface
57
58     def getList(self):
59         #loads a list auf Streams into self.list
60         pass
61
62     def getMenuItems(self,selectedStream,generic=False):
63         # this return a list of MenuEntries of actions of this iterface
64         # list=(("item1",func1),("item2",func2), ... )
65         #
66         # generic=True indicates, that items of the returned list are services
67         # in any context (like saving a stream to the favorites)
68         return []
69
70     def OnListLoaded(self):
71         # called from the interface, if list was loaded
72         if self.cbListLoaded is not None:
73             self.cbListLoaded(self.list)
74
75 ###############################################################################
76 class Stream:
77     isfavorite = False
78     def __init__(self,name,description,url,type="mp3"):
79         self.name = name
80         self.description = description
81         self.url = url
82         self.type=type
83     def getName(self):
84         return self.name
85     def getDescription(self):
86         return self.description
87     def setName(self,name):
88         self.name = name
89     def setDescription(self,description):
90         self.description = description
91     def setURL(self,url):
92         self.url = url
93     def getURL(self, callback):
94         self.callback = callback
95         if self.type.lower() == "pls":
96                 self.getPLSContent()
97         else:
98             self.callback(self.url)
99
100     def getPLSContent(self):
101         print "loading PLS of stream ",self.name,self.url
102         getPage(self.url).addCallback(self._gotPLSContent).addErrback(self._errorPLSContent)
103
104     def _gotPLSContent(self, lines):
105                 if lines.startswith("ICY "):
106                         print "PLS expected, but got ICY stream"
107                         self.type = "mp3"
108                         self.callback(self.url)
109
110                 else:
111                         for line in lines.split('\n'):
112                             if line.startswith("File"):
113                                 url = line.split("=")[1].rstrip().strip()
114                                 self.callback(url)
115                                 break
116                             print "Skipping:", line
117
118     def _errorPLSContent(self, data, callback):
119         callback(None)
120
121
122     def setFavorite(self,TrueFalse):
123         self.isfavorite = TrueFalse
124     def isFavorite(self):
125         return self.isfavorite
126     def setType(self,type):
127         self.type=type
128     def getType(self):
129         return self.type
130