replace urllib.urlopen with twisted.web.client.getPage
[enigma2-plugins.git] / netcaster / src / bin / StreamInterface.py
1 from twisted.web.client import getPage
2
3 valid_types = ("MP3","PLS") #list of playable mediatypes
4
5 class StreamInterface:
6     def __init__(self,session,cbListLoaded=None):
7         self.session = session
8         self.cbListLoaded = cbListLoaded
9         
10         self.list= [] # contains the streams in this iface
11         
12     def getList(self):
13         #loads a list auf Streams into self.list
14         pass
15     
16     def getMenuItems(self,selectedStream,generic=False):
17         # this return a list of MenuEntries of actions of this iterface
18         # list=(("item1",func1),("item2",func2), ... )
19         #
20         # generic=True indicates, that items of the returned list are services 
21         # in any context (like saving a stream to the favorites)
22         return []
23     
24     def OnListLoaded(self):
25         # called from the interface, if list was loaded
26         if self.cbListLoaded is not None:
27             self.cbListLoaded(self.list)
28     
29 ############################################################################### 
30 class Stream:
31     isfavorite = False
32     def __init__(self,name,description,url,type="mp3"):
33         self.name = name
34         self.description = description
35         self.url = url
36         self.type=type
37     def getName(self):
38         return self.name
39     def getDescription(self):
40         return self.description
41     def setName(self,name):
42         self.name = name
43     def setDescription(self,description):
44         self.description = description
45     def setURL(self,url):
46         self.url = url
47     def getURL(self, callback):
48         self.callback = callback
49         if self.type.lower() == "pls":
50             return self.getPLSContent(callback)
51         else:
52             callback(self.url)
53
54     def getPLSContent(self):
55         print "loading PLS of stream ",self.name,self.url
56         getPage(self.url).addCallback(self._gotPLSContent).addErrback(self._errorPLSContent)
57     
58     def _gotPLSContent(self, lines):
59                 if lines.startswith("ICY "):
60                         print "PLS expected, but got ICY stream"                        
61                         self.type = "mp3"
62                         self.callback(self.url)
63                         
64                 else:
65                         for line in lines.split('\n'):
66                             if line.startswith("File"):
67                                 url = line.split("=")[1].rstrip().strip()                               
68                                 self.callback(url)
69                                 break
70                             print "Skipping:", line
71                             
72     def _errorPLSContent(self, data, callback):
73         callback(None)
74                 
75         
76     def setFavorite(self,TrueFalse):
77         self.isfavorite = TrueFalse
78     def isFavorite(self):
79         return self.isfavorite
80     def setType(self,type):
81         self.type=type
82     def getType(self):
83         return self.type
84