Patch of Mike Looijmans
[enigma2-plugins.git] / netcaster / src / bin / StreamInterface.py
1 from urllib import urlopen
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):
48         if self.type.lower() == "pls":
49             return self.getPLSContent()
50         else:
51             return self.url
52
53     def getPLSContent(self):
54         print "loading PLS of stream ",self.name,self.url
55         url = None
56         try:
57             fp = urlopen(self.url)
58             lines = fp.read(1024)
59             fp.close()
60             if lines.startswith("ICY "):
61                 print "PLS expected, but got ICY stream"
62                 url = self.url
63                 self.type = "mp3"
64             else:
65                 for line in lines.split('\n'):
66                     if line.startswith("File"):
67                         url = line.split("=")[1].rstrip().strip()
68                         break
69                     print "Skipping:", line
70         except Exception, e:
71             print "Error while loading PLS of stream ",self.getName(),"! ",e
72         return url
73         
74     def setFavorite(self,TrueFalse):
75         self.isfavorite = TrueFalse
76     def isFavorite(self):
77         return self.isfavorite
78     def setType(self,type):
79         self.type=type
80     def getType(self):
81         return self.type
82