use choicebox instead of own menu
[enigma2-plugins.git] / orfat / src / ORFIPTV.py
1 ##\r
2 ## ORF.at IPTV streaming module\r
3 ## by AliAbdul\r
4 ##\r
5 from Components.ActionMap import ActionMap
6 from Components.Pixmap import MovingPixmap
7 from enigma import ePoint, eSize
8 from Plugins.Extensions.VlcPlayer.VlcServerConfig import vlcServerConfig
9 from Screens.ChoiceBox import ChoiceBox\r
10 from Screens.MessageBox import MessageBox\r
11 from Screens.Screen import Screen\r
12 from Tools.Directories import fileExists\r
13 from Tools.LoadPixmap import LoadPixmap\r
14 from twisted.web.client import downloadPage, getPage
15
16 ##########################################################\r
17 \r
18 class ORFMain(Screen):
19         skin = """
20         <screen position="85,65" size="550,450" title="ORF.at IPTV" backgroundColor="#6699cc" >
21                 <ePixmap pixmap="skin_default/arrowup.png" position="256,10" size="37,70" alphatest="blend" />\r
22                 <widget name="pic" position="0,0" size="0,0" />
23                 <ePixmap pixmap="skin_default/arrowdown.png" position="256,370" size="37,70" alphatest="blend" />\r
24         </screen>"""
25         \r
26         def __init__(self, session):\r
27                 Screen.__init__(self, session)
28                 \r
29                 self.session = session
30                 self.movies = []
31                 self.pics = []
32                 self.selectedEntry = 0
33                 self.pic = "/tmp/orf.jpg"
34                 self.working = False
35                 
36                 self["pic"] = MovingPixmap()\r
37                 self["actions"] = ActionMap(["WizardActions"],
38                         {
39                                 "ok": self.okClicked,
40                                 "back": self.close,
41                                 "up": self.up,
42                                 "down": self.down
43                         }, -1)
44                 
45                 self.onLayoutFinish.append(self.downloadList)\r
46 \r
47         def okClicked(self):
48                 if self.working == False:
49                         if len(self.movies) > 0:
50                                 movie = self.movies[self.selectedEntry]
51                                 serverList = vlcServerConfig.getServerlist()
52                                 if len(serverList) == 0:
53                                         self.session.open(MessageBox, "Kein Server im vlc-Plugin konfiguriert!", MessageBox.TYPE_ERROR)
54                                 elif len(serverList) == 1:
55                                         serverList[0].play(self.session, movie, "ORF.at IPTV")
56                                 else:
57                                         list = []
58                                         for x in serverList:
59                                                 list.append((x.getName(), x))
60                                         self.session.openWithCallback(self.serverChosen, ChoiceBox, title="Waehle den vlc-Server...", list=list)
61
62         def serverChosen(self, server):
63                 if server is not None:
64                         server[1].play(self.session, self.movies[self.selectedEntry], "ORF.at IPTV")
65 \r
66         def downloadList(self):
67                 self.working = True\r
68                 getPage("http://iptv.orf.at").addCallback(self.downloadListCallback).addErrback(self.downloadListError)\r
69 \r
70         def downloadListError(self, error=""):\r
71                 print str(error)
72                 self.working = False
73                 self.session.open(MessageBox, "Fehler beim Verbindungsversuch!", MessageBox.TYPE_ERROR)\r
74 \r
75         def downloadListCallback(self, page=""):
76                 while page.__contains__("javascript:iptvPopup('"):
77                         idx = page.index("javascript:iptvPopup('")
78                         page = page[idx+22:]
79                         idx = page.index("'")
80                         movie = page[:idx]
81                         idx = movie.index("?")
82                         movie = "%s%s" % ("mms://stream4.orf.at/news/", movie[idx+1:])
83                         self.movies.append(movie)
84                         idx = page.index('<IMG SRC=')
85                         page = page[idx+9:]
86                         idx = page.index(' ALT')
87                         pic = "%s%s" % ("http://iptv.orf.at/", page[:idx])
88                         self.pics.append(pic)
89                 self.selectionChanged(0)
90
91         def up(self):
92                 if self.working == False:
93                         self.selectionChanged(-1)
94
95         def down(self):
96                 if self.working == False:
97                         self.selectionChanged(1)
98
99         def selectionChanged(self, direction):
100                 if len(self.movies) > 0:
101                         self.working = True
102                         self.selectedEntry += direction
103                         if self.selectedEntry < 0:
104                                 self.selectedEntry = len(self.movies) - 1
105                         elif self.selectedEntry > len(self.movies) - 1:
106                                 self.selectedEntry = 0
107                         downloadPage(self.pics[self.selectedEntry], self.pic).addCallback(self.downloadPicCallback).addErrback(self.downloadPicError)
108                 else:
109                         self.downloadListError()
110
111         def downloadPicCallback(self, page=""):
112                 picture = LoadPixmap(self.pic)
113                 size = picture.size()\r
114                 width = size.width()\r
115                 height = size.height()\r
116                 self["pic"].instance.setPixmap(picture)
117                 self["pic"].instance.resize(eSize(width, height))
118                 left = int((550 / 2) - (width / 2))
119                 top = int((450 / 2) - (height / 2))
120                 self["pic"].moveTo(left, top, 1)\r
121                 self["pic"].startMoving()
122                 self["pic"].show()
123                 self.working = False
124
125         def downloadPicError(self, error=""):
126                 print str(error)
127                 self["pic"].hide()
128                 self.working = False
129                 self.session.open(MessageBox, "Fehler beim Herunterladen des Eintrags!", MessageBox.TYPE_ERROR)