added Stream2Dream support
[enigma2-plugins.git] / orfat / src / plugin.py
1 ##\r
2 ## ORF.at IPTV\r
3 ## by AliAbdul\r
4 ##\r
5 from Components.ActionMap import ActionMap
6 from Components.Pixmap import MovingPixmap, Pixmap
7 from enigma import ePoint, eServiceReference, eSize, eTimer
8 from os import listdir
9 from Plugins.Plugin import PluginDescriptor
10 from Screens.ChoiceBox import ChoiceBox
11 from Screens.InfoBar import MoviePlayer\r
12 from Screens.MessageBox import MessageBox\r
13 from Screens.Screen import Screen
14 from time import sleep
15 from Tools.BoundFunction import boundFunction
16 from Tools.Directories import fileExists, resolveFilename, SCOPE_PLUGINS\r
17 from Tools.LoadPixmap import LoadPixmap\r
18 from twisted.web.client import downloadPage, getPage
19
20 ##########################################################
21
22 PNG_PATH = resolveFilename(SCOPE_PLUGINS)+"/Extensions/ORFat/"
23
24 try:
25         from LT.LTStreamPlayer import streamplayer
26 except ImportError:
27         try:
28                 from Plugins.Extensions.LTMediaCenter.LTStreamPlayer import streamplayer
29         except ImportError:
30                 streamplayer = None
31
32 try:
33         from Plugins.Extensions.VlcPlayer.VlcServerConfig import vlcServerConfig
34 except ImportError:
35         vlcServerConfig = None
36
37 ##########################################################
38
39 class ChangedMoviePlayer(MoviePlayer):
40         def __init__(self, session, service):
41                 MoviePlayer.__init__(self, session, service)
42                 self.skinName = "MoviePlayer"
43
44         def leavePlayer(self):
45                 self.session.openWithCallback(self.leavePlayerConfirmed, MessageBox, "Abspielen beenden?")
46
47         def leavePlayerConfirmed(self, answer):
48                 if answer:
49                         self.close()
50
51         def doEofInternal(self, playing):
52                 pass
53
54         def getPluginList(self):
55                 list = []
56                 for p in plugins.getPlugins(where=PluginDescriptor.WHERE_EXTENSIONSMENU):
57                         if p.name != "ORF.at IPTV":
58                                 list.append(((boundFunction(self.getPluginName, p.name), boundFunction(self.runPlugin, p), lambda: True), None))
59                 return list
60
61         def showMovies(self):
62                 pass
63
64 ##########################################################
65
66 class ORFatCache(Screen):
67         skin = """
68                 <screen position="center,center" size="76,76" flags="wfNoBorder" backgroundColor="#ffffff" >
69                         <eLabel position="2,2" zPosition="1" size="72,72" font="Regular;18" backgroundColor="#252525" />
70                         <widget name="spinner" position="14,14" zPosition="2" size="48,48" alphatest="on" />
71                 </screen>"""
72
73         def __init__(self, session):
74                 self.session = session
75                 Screen.__init__(self, session)
76                 
77                 self["spinner"] = Pixmap()
78                 self.curr = 0
79                 self.Shown = False
80                 
81                 self.timer = eTimer()
82                 self.timer.callback.append(self.showNextSpinner)
83
84         def start(self):
85                 self.show()
86                 self.Shown = True
87                 self.timer.start(200, False)
88
89         def stop(self):
90                 self.hide()
91                 self.Shown = False
92                 self.timer.stop()
93
94         def showNextSpinner(self):
95                 self.curr += 1
96                 if self.curr > 10:
97                         self.curr = 0
98                 png = LoadPixmap(cached=True, path=PNG_PATH + str(self.curr) + ".png")
99                 self["spinner"].instance.setPixmap(png)
100
101 ##########################################################\r
102 \r
103 class ORFMain(Screen):
104         skin = """
105         <screen position="center,center" size="550,450" title="ORF.at IPTV - Server:" backgroundColor="#6699cc" >
106                 <ePixmap pixmap="skin_default/arrowup.png" position="256,10" size="37,70" alphatest="blend" />\r
107                 <widget name="pic" position="0,0" size="0,0" />
108                 <ePixmap pixmap="skin_default/arrowdown.png" position="256,370" size="37,70" alphatest="blend" />\r
109         </screen>"""
110         \r
111         def __init__(self, session):\r
112                 Screen.__init__(self, session)
113                 \r
114                 self.session = session
115                 self.movies = []
116                 self.pics = []
117                 self.selectedEntry = 0
118                 self.pic = "/tmp/orf.jpg"
119                 self.working = False
120                 self.cacheDialog = self.session.instantiateDialog(ORFatCache)
121                 self.cacheTimer = eTimer()
122                 self.cacheTimer.callback.append(self.chechCachedFile)
123                 self.transcodeServer = None
124                 
125                 self["pic"] = MovingPixmap()\r
126                 self["actions"] = ActionMap(["WizardActions", "MenuActions"],
127                         {
128                                 "ok": self.okClicked,
129                                 "back": self.exit,
130                                 "up": self.up,
131                                 "down": self.down,
132                                 "menu": self.selectServer
133                         }, -1)
134                 
135                 self.onLayoutFinish.append(self.downloadList)\r
136 \r
137         def okClicked(self):
138                 if self.working == False:
139                         if len(self.movies) > 0:
140                                 if self.cacheDialog.Shown:
141                                         self.playCachedFile()
142                                 else:
143                                         url = self.movies[self.selectedEntry]
144                                         if self.transcodeServer is not None:
145                                                 if self.transcodeServer == "LT Stream2Dream":
146                                                         r = streamplayer.play(url)
147                                                         if r == "ok":
148                                                                 sleep(6)
149                                                                 self.cacheDialog.start()
150                                                                 self.cacheTimer.start(1000, False)
151                                                 else:
152                                                         self.transcodeServer.play(self.session, url, "ORF.at IPTV")
153                                         else:
154                                                 self.session.open(MessageBox, "Es wurde kein Server ausgewaehlt!", MessageBox.TYPE_ERROR)
155
156         def exit(self):
157                 if not self.working:
158                         if self.cacheDialog.Shown:
159                                 if streamplayer:
160                                         if streamplayer.connected:
161                                                 streamplayer.stop()
162                                                 sleep(4)
163                                 self.deactivateCacheDialog()
164                         else:
165                                 if streamplayer:
166                                         if streamplayer.connected:
167                                                 streamplayer.logout()
168                                 self.session.deleteDialog(self.cacheDialog)
169                                 self.close()
170
171         def selectServer(self):
172                 list = []
173                 if streamplayer:
174                         list.append(("LT Stream2Dream", "LT Stream2Dream"))
175                 if vlcServerConfig:
176                         serverList = vlcServerConfig.getServerlist()
177                         for x in serverList:
178                                 list.append((x.getName(), x))
179                 if len(list):
180                         self.session.openWithCallback(self.serverChosen, ChoiceBox, title="Waehle den Server...", list=list)
181
182         def serverChosen(self, callback):
183                 if callback:
184                         server = callback[1]
185                         if server == "LT Stream2Dream":
186                                 if not streamplayer.connected:
187                                         self.transcodeServer = "LT Stream2Dream"
188                                         self.setTitle("ORF.at IPTV - Server: LT Stream2Dream")
189                                         self.connectToStream2Dream()
190                         else:
191                                 if streamplayer:
192                                         if streamplayer.connected:
193                                                 streamplayer.logout()
194                                 self.transcodeServer = server
195                                 self.setTitle("ORF.at IPTV - Server: %s"%server.getName())
196
197         def connectToStream2Dream(self):
198                 streamplayer.login()
199                 try:
200                         list = listdir("/tmp/mp")
201                 except:
202                         list = []
203                 if len(list) < 2:
204                         self.session.open(MessageBox, "Die Verbindung zu LT Stream2Dream konnte nicht hergestellt werden!", MessageBox.TYPE_ERROR)
205                         streamplayer.logout()
206                         self.transcodeServer = None
207                         self.setTitle("ORF.at IPTV - Server:")
208
209         def chechCachedFile(self):
210                 try:
211                         f = open ("/tmp/mpstream/progress.txt")
212                         content = f.read()
213                         f.close()
214                         list = content.split("-")
215                         cacheMB = int(list[0])
216                         if cacheMB > 5: # Starte nach 5 MB Bufferung
217                                 self.cacheTimer.stop()
218                                 self.playCachedFile()
219                 except:
220                         pass
221
222         def deactivateCacheDialog(self):
223                 self.cacheDialog.stop()
224                 self.working = False
225
226         def playCachedFile(self):
227                 self.deactivateCacheDialog()
228                 ref = eServiceReference(1, 0, "/tmp/mpstream/MPStream.ts")
229                 self.session.openWithCallback(self.stopStream2Dream, ChangedMoviePlayer, ref)
230
231         def stopStream2Dream(self, callback=None):
232                 streamplayer.stop()
233                 sleep(4)
234 \r
235         def downloadList(self):
236                 self.working = True\r
237                 getPage("http://iptv.orf.at").addCallback(self.downloadListCallback).addErrback(self.downloadListError)\r
238 \r
239         def downloadListError(self, error=""):\r
240                 print str(error)
241                 self.working = False
242                 self.session.open(MessageBox, "Fehler beim Verbindungsversuch!", MessageBox.TYPE_ERROR)\r
243 \r
244         def downloadListCallback(self, page=""):
245                 while page.__contains__("javascript:iptvPopup('"):
246                         idx = page.index("javascript:iptvPopup('")
247                         page = page[idx+22:]
248                         idx = page.index("'")
249                         movie = page[:idx]
250                         idx = movie.index("?")
251                         movie = "%s%s" % ("mms://stream4.orf.at/news/", movie[idx+1:])
252                         self.movies.append(movie)
253                         idx = page.index('<IMG SRC=')
254                         page = page[idx+9:]
255                         idx = page.index(' ALT')
256                         pic = "%s%s" % ("http://iptv.orf.at/", page[:idx])
257                         self.pics.append(pic)
258                 self.selectionChanged(0)
259
260         def up(self):
261                 if self.working == False:
262                         self.selectionChanged(-1)
263
264         def down(self):
265                 if self.working == False:
266                         self.selectionChanged(1)
267
268         def selectionChanged(self, direction):
269                 if len(self.movies) > 0:
270                         self.working = True
271                         self.selectedEntry += direction
272                         if self.selectedEntry < 0:
273                                 self.selectedEntry = len(self.movies) - 1
274                         elif self.selectedEntry > len(self.movies) - 1:
275                                 self.selectedEntry = 0
276                         downloadPage(self.pics[self.selectedEntry], self.pic).addCallback(self.downloadPicCallback).addErrback(self.downloadPicError)
277                 else:
278                         self.downloadListError()
279
280         def downloadPicCallback(self, page=""):
281                 picture = LoadPixmap(self.pic)
282                 size = picture.size()\r
283                 width = size.width()\r
284                 height = size.height()\r
285                 self["pic"].instance.setPixmap(picture)
286                 self["pic"].instance.resize(eSize(width, height))
287                 left = int((550 / 2) - (width / 2))
288                 top = int((450 / 2) - (height / 2))
289                 self["pic"].moveTo(left, top, 1)\r
290                 self["pic"].startMoving()
291                 self["pic"].show()
292                 self.working = False
293
294         def downloadPicError(self, error=""):
295                 print str(error)
296                 self["pic"].hide()
297                 self.working = False
298                 self.session.open(MessageBox, "Fehler beim Herunterladen des Eintrags!", MessageBox.TYPE_ERROR)
299
300 ####################################################
301
302 def main(session, **kwargs):
303         session.open(ORFMain)
304
305 def Plugins(**kwargs):
306         return PluginDescriptor(name="ORF.at IPTV", description="IPTV-Sendungen von ORF.at anschauen", where=[PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU], icon="orf.png", fnc=main)