port to epicload,
[vuplus_dvbapp-plugin] / unwetterzentrale / src / plugin.py
1 # -*- coding: utf-8 -*-
2 #
3 # Wetter Infos von
4 # www.unwetterzentrale.de und www.uwz.at
5 #
6 # Author: barabas
7 #
8
9 import xml.sax.saxutils as util
10
11 from Plugins.Plugin import PluginDescriptor
12 from twisted.web.client import getPage
13 from twisted.internet import reactor
14 from Screens.Screen import Screen
15 from Screens.Console import Console
16 from Components.ActionMap import ActionMap
17 from Components.Label import Label
18 from Components.MenuList import MenuList
19 from Components.AVSwitch import AVSwitch
20 from Components.Pixmap import Pixmap
21 from enigma import eTimer, ePicLoad
22 from re import sub, search, findall
23 from os import unlink
24
25 def getAspect():
26         val = AVSwitch().getAspectRatioSetting()
27         return val/2
28         
29 ###############################################################################  
30
31 class PictureView(Screen):
32                 skin = """
33                         <screen position="0,0" size="720,576" flags="wfNoBorder" title="UWZ" >
34                                 <eLabel position="0,0" zPosition="1" size="720,576" backgroundColor="black" />
35                                 <ePixmap position="635,540" zPosition="2" size="36,20" pixmap="skin_default/buttons/key_info.png" alphatest="on" />
36                                 <widget name="picture" position="80,10" zPosition="2" size="550,550" />
37                         </screen>"""
38                         
39                 def __init__(self, session):
40                                 self.skin = PictureView.skin
41                                 Screen.__init__(self, session)
42         
43                                 self.aspect = getAspect()
44                                 self.picfile = "/tmp/uwz.png"   
45                                 
46                                 self["picture"] = Pixmap()
47                                 
48                                 self["actions"] = ActionMap(["OkCancelActions","MovieSelectionActions"],
49                                 {
50                                                         "cancel": self.exit,
51                                                                         "ok": self.exit,
52                         "showEventInfo": self.HelpView,
53                                 }, -1)
54
55                                 self.picload = ePicLoad()
56                                 sc = AVSwitch().getFramebufferScale()
57                                 self.picload.setPara((550, 550, sc[0], sc[1], 0, 0, '#ff000000'))
58                                 self.picload.PictureData.get().append(self.gotPic)
59                                 self.onLayoutFinish.append(self.getPic)
60
61                 def getPic(self):
62                                 self.picload.startDecode(self.picfile)
63
64                 def gotPic(self, picInfo = None):
65                                 ptr = self.picload.getData()
66                                 if ptr:
67                                         self["picture"].instance.setPixmap(ptr.__deref__())
68
69                 def HelpView(self):
70                                 self.session.openWithCallback(self.getPic ,HelpPictureView)
71         
72                 def exit(self):
73                                 self.close()
74                                  
75
76 class HelpPictureView(Screen):
77                 skin = """
78                         <screen position="25,200" size="670,290" title="Warnstufen" >
79                                 <eLabel position="0,0" zPosition="1" size="670,290" backgroundColor="black" />
80                                 <ePixmap position="320,260" zPosition="2" size="36,20" pixmap="skin_default/arrowdown.png" alphatest="on" />
81                                 <widget name="picture" position="-10,20" zPosition="2" size="690,225" />
82                         </screen>"""
83                                 
84                 def __init__(self, session):
85                                 self.skin = HelpPictureView.skin
86                                 Screen.__init__(self, session)
87                                 
88                                 self["picture"] = Pixmap()
89                                 
90                                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions"],
91                                 {
92                                         "cancel": self.exit,
93                                                         "ok": self.exit,
94                                                 "left": self.prevPic,
95                                         "right": self.nextPic
96                                 }, -1)
97                                 
98                                 self.aspect = getAspect()
99                                 self.list = (
100                                         pluginpath + "/W_gruen.gif",
101                                         pluginpath + "/W_gelb.gif",
102                                         pluginpath + "/W_orange.gif",
103                                         pluginpath + "/W_rot.gif",
104                                         pluginpath + "/W_violett.gif"
105                                 )
106                                 self.index = 0
107
108                                 self.picload = ePicLoad()
109                                 sc = AVSwitch().getFramebufferScale()
110                                 self.picload.setPara((690, 225, sc[0], sc[1], 0, 0, '#ff000000'))
111                                 self.picload.PictureData.get().append(self.gotPic)
112
113                                 self.onShown.append(self.getPic)
114                                 
115                 def getPic(self):
116                                 self.picload.startDecode(self.list[self.index])
117
118                 def gotPic(self, picInfo = None):
119                                 ptr = self.picload.getData()
120                                 if ptr:
121                                         self["picture"].instance.setPixmap(ptr.__deref__())
122         
123                 def nextPic(self):
124                                 self.index += 1
125                                 if self.index > 4:
126                                         self.index = 0
127                                 self.getPic()
128                 
129                 def prevPic(self):
130                                 self.index -= 1
131                                 if self.index < 0:
132                                         self.index = 4
133                                 self.getPic()
134                                 
135                 def exit(self):
136                                 self.close()
137                                                                 
138 class UnwetterMain(Screen):
139
140                 skin = """
141                                 <screen position="110,83" size="530,430" title="Unwetterzentrale" >
142                                                 <widget name="hmenu" position="5,0" zPosition="1" size="530,220" scrollbarMode="showOnDemand" />
143                                                 <widget name="thumbnail" position="185,250" zPosition="2" size="140,150" />
144                                                 <widget name="thumbland" position="435,390" zPosition="2" size="90,40"  />
145                                                 <ePixmap position="20,380" zPosition="2" size="36,20" pixmap="skin_default/buttons/key_menu.png" alphatest="on" />
146                                                 <widget name="statuslabel" position="5,410" zPosition="2" size="530,20" font="Regular;16" halign=\"left\"/>
147                                 </screen>"""
148
149                 def __init__(self, session):
150                                 self.loadinginprogress = False  
151                                 self.skin = UnwetterMain.skin
152                                 self.session = session
153                                 Screen.__init__(self, session)       
154                                 
155                                 self["statuslabel"] = Label()
156                                 self["thumbland"] = Pixmap()
157                                 self["thumbnail"] = Pixmap()
158                                 self["hmenu"] = MenuList([])            
159                                 self["actions"] = ActionMap(["OkCancelActions", "DirectionActions", "MovieSelectionActions"],
160                                 {
161                                         "ok":   self.ok,
162                                         "up": self.up,
163                                         "right": self.rightDown,
164                                         "left": self.leftUp,
165                                         "down": self.down,
166                                         "cancel": self.exit,
167                                         "contextMenu": self.switchDeA,
168                                 }, -1)
169                                 
170                                 self.aspect = getAspect()                       
171                                 self.menueintrag = []
172                                 self.link = []
173                                 self.picfile = "/tmp/uwz.png"   
174                                 self.picweatherfile = pluginpath + "/wetterreport.jpg"
175                                 self.reportfile = "/tmp/uwz.report"
176                                 
177                                 try:
178                                                 f = open(pluginpath + "/last.cfg","r")
179                                                 self.land = f.read()
180                                                 f.close
181                                 except:
182                                                 self.land = "de"
183                                 
184                                 if self.land == "de":                           
185                                                 self.baseurl = "http://www.unwetterzentrale.de/uwz/"
186                                                 self.menuurl = self.baseurl + "index.html"
187                                                 self.weatherreporturl = self.baseurl + "lagebericht.html"
188                                 else:
189                                                 self.baseurl = "http://www.uwz.at/"
190                                                 self.menuurl = self.baseurl + "index.php"
191                                                 self.weatherreporturl = self.baseurl + "lagebericht.php"
192                                                                                 
193                                 self.downloadMenu()
194
195                                 self.picload = ePicLoad()
196
197 #                               self.onLayoutFinish.append(self.go)     
198
199                                 self.ThumbTimer = eTimer()
200                                 self.ThumbTimer.callback.append(self.showThumb)
201                                 self.ThumbTimer.start(1000, True)                       
202                                                 
203                 def hauptmenu(self,output):                     
204                                 self.loadinginprogress = False
205                                 trans = { '&szlig;' : 'ß' , '&auml;' : 'ä' , '&ouml;' : 'ö' , '&uuml;' : 'ü' , '&Auml;' : 'Ä', '&Ouml;' : 'Ö' , '&Uuml;' : 'Ü'}
206                                 output= util.unescape(output,trans)
207                         
208                                 if self.land == "de":
209                                                 startpos = output.find('<!-- Anfang Navigation -->')
210                                                 endpos = output.find('<!-- Ende Navigation -->')
211                                                 bereich = output[startpos:endpos]                                                       
212                                                 a = findall(r'href=(?P<text>.*?)</a>',bereich)                                          
213                                                 for x in a[1:16]:
214                                                         x = x.replace('">',"#").replace('"',"").split('#')
215                                                         if not len(x) > 1:
216                                                                 break
217                                                         name = x[1]
218                                                         link = self.baseurl + x[0]
219                                                         self.menueintrag.append(name)
220                                                         self.link.append(link)          
221                                 else:
222                                                 startpos = output.find('<div id="mainWindow">')
223                                                 endpos = output.find('<a class="menua" href="http://www.austrowetter.at"')
224                                                 bereich = output[startpos:endpos]                               
225                                                 a = findall(r'<a class="menub" href=(?P<text>.*?)</a>',bereich)
226                                                 for x in a[1:13]:
227                                                         x = x.replace('">',"#").replace('"',"").replace(' style=font-weight:;',"")
228                                                         if x != '#&nbsp;':
229                                                                         x = x.split('#')
230                                                                         if not len(x) > 1:
231                                                                                 break
232                                                                         name = x[1]
233                                                                         link = self.baseurl + x[0]
234                                                                         self.menueintrag.append(name)
235                                                                         self.link.append(link)                          
236
237                                 self["statuslabel"].setText("")                 
238                                 self["hmenu"].l.setList(self.menueintrag)
239                                 self["hmenu"].instance.moveSelectionTo(0)
240                                 self.showThumbLand()
241                                                                 
242                 def ok(self):
243                                 self.go()
244                                 c = self["hmenu"].getCurrent()
245                                 if c is not None:
246                                         x = self.menueintrag.index(c)
247                                         if x != 0:
248                                                 self.session.open(PictureView)          
249                                         else:
250                                                 self.downloadWeatherReport()
251                                                                 
252                 def go(self):
253                                 c = self["hmenu"].getCurrent()
254                                 if c is not None:
255                                         x = self.menueintrag.index(c)
256                                         # Wetterlagebericht ist Index 0
257                                         if x != 0:
258                                                 url = self.link[x]
259                                                 self["statuslabel"].setText("Loading Data")
260                                                 self.downloadPicUrl(url)
261                                         self.ThumbTimer.start(1500, True)
262                                                 
263                 def up(self):
264                                 self["hmenu"].up()
265                                 self.go()
266                                                                 
267                 def down(self):
268                                 self["hmenu"].down()
269                                 self.go()                               
270
271                 def leftUp(self):
272                                 self["hmenu"].pageUp()
273                                 self.go()
274                 
275                 def rightDown(self):
276                                 self["hmenu"].pageDown()
277                                 self.go()
278
279                 def showThumbLand(self):
280                                 picture = ""
281                                 if self.land == "de":
282                                                 picture = pluginpath + "/uwz.png"
283                                 else:
284                                                 picture = pluginpath + "/uwzat.png"
285                                 picload = self.picload
286                                 sc = AVSwitch().getFramebufferScale()
287                                 picload.setPara((90, 40, sc[0], sc[1], 0, 0, '#ff000000'))
288                                 l = picload.PictureData.get()
289                                 del l[:]
290                                 l.append(self.gotThumbLand)
291                                 picload.startDecode(picture)
292
293                 def gotThumbLand(self, picInfo = None):
294                                 ptr = self.picload.getData()
295                                 if ptr:
296                                                 self["thumbland"].instance.setPixmap(ptr.__deref__())
297                                                 
298                 def showThumb(self):
299                                 picture = ""
300                                 if self.land == "de":
301                                                 width = 142 ; height = 150
302                                 else:
303                                                 width = 142 ;   height = 135
304                                 c = self["hmenu"].getCurrent()
305                                 if c is not None:
306                                         x = self.menueintrag.index(c)
307                                         if x != 0:
308                                                 picture = self.picfile
309                                         else:
310                                                 picture = self.picweatherfile   
311                                                 height = 150                            
312
313                                 picload = self.picload
314                                 sc = AVSwitch().getFramebufferScale()
315                                 picload.setPara((width, height, sc[0], sc[1], 0, 0, '#ff000000'))
316                                 l = picload.PictureData.get()
317                                 del l[:]
318                                 l.append(self.gotThumb)
319                                 picload.startDecode(picture)
320
321                 def gotThumb(self, picInfo = None):
322                                 ptr = self.picload.getData()
323                                 if ptr:
324                                         self["statuslabel"].setText("")
325                                         self["thumbnail"].show()
326                                         self["thumbnail"].instance.setPixmap(ptr.__deref__())
327                                 else:
328                                         self["thumbnail"].hide()
329                         
330                 def getPicUrl(self,output):
331                                 self.loadinginprogress = False
332                                 if self.land == "de":   
333                                                 startpos = output.find('<!-- Anfang msg_Box Content -->')
334                                                 endpos = output.find('<!-- Ende msg_Box Content -->')
335                                                 bereich = output[startpos:endpos]
336                                                 picurl = search(r'<img src="(?P<text>.*?)" width=',bereich)
337                                                 picurl = self.baseurl + picurl.group(1)
338                                 else:
339                                                 picurl = search(r'<img src="showMap(?P<text>.*?)" alt=',output)
340                                                 picurl = self.baseurl + "showMap" + picurl.group(1).replace('&amp;','&')
341                                 self.downloadPic(picurl)
342
343                 def getPic(self,output):
344                                 self.loadinginprogress = False
345                                 f = open(self.picfile, "wb")
346                                 f.write(output)
347                                 f.close                 
348         
349                 def getWeatherReport(self,output):
350                                 self.loadinginprogress = False          
351                                 if self.land == "de":
352                                                 trans = { '&szlig;' : 'ß' , '&auml;' : 'ä' , '&ouml;' : 'ö' , '&uuml;' : 'ü' , '&Auml;' : 'Ä', '&Ouml;' : 'Ö' , '&Uuml;' : 'Ü'}
353                                                 output= util.unescape(output,trans)
354                                                 startpos = output.find('<!-- Anfang msg_Box Content -->')
355                                                 endpos = output.find('<!-- Ende msg_Box Content -->')
356                                                 bereich = output[startpos:endpos]
357                                 else:
358                                                 startpos = output.find('<div class="content">')
359                                                 endpos = output.find('<div class="rs_title">Unwettermeldungen</div>')
360                                                 bereich = output[startpos:endpos]
361                                                 u_bereich = bereich.decode("iso-8859-1")
362                                                 bereich = u_bereich.encode("utf-8")
363                                                 bereich = sub('<br />',"\n",bereich)
364
365                                 bereich = sub('<[^>]*>',"",bereich)                             
366                                 bereich = sub('Fronten- und Isobarenkarte.*',"",bereich)
367                                 bereich = bereich.strip()
368                                 bereich = sub("\n\s*\n*", "\n\n", bereich)
369
370                                 f = open(self.reportfile, "w")
371                                 f.write("%s" % bereich)
372                                 f.close
373                                 self.session.open(Console,_("Warnlagebericht"),["cat %s" % self.reportfile])            
374                                                                                                                                                         
375                 def downloadError(self,output):
376                                 self.loadinginprogress = False
377                                 self["statuslabel"].setText("Fehler beim Download")
378                 
379                 def downloadMenu(self):
380                                 self.loadinginprogress = True
381                                 getPage(self.menuurl).addCallback(self.hauptmenu).addErrback(self.downloadError)                
382
383                 def downloadPicUrl(self,url):
384                                 self.loadinginprogress = True
385                                 getPage(url).addCallback(self.getPicUrl).addErrback(self.downloadError)         
386
387                 def downloadPic(self,picurl):
388                                 self.loadinginprogress = True
389 #                               self["statuslabel"].setText("Lade Bild: %s" % picurl)
390                                 getPage(picurl).addCallback(self.getPic).addErrback(self.downloadError)
391                 
392                 def downloadWeatherReport(self):
393                                 self.loadinginprogress = True
394 #                               self["statuslabel"].setText("Lade Report: %s" % self.weatherreporturl)
395                                 getPage(self.weatherreporturl).addCallback(self.getWeatherReport).addErrback(self.downloadError)
396                 
397                 def switchDeA(self):                                                            
398                                 self.menueintrag = []
399                                 self.link = []
400                                 if self.land == "de":
401                                                 self.land = "a"
402                                                 self.baseurl = "http://www.uwz.at/"
403                                                 self.menuurl = self.baseurl + "index.php"
404                                                 self.weatherreporturl = self.baseurl + "lagebericht.php"
405                                 else:
406                                                 self.land = "de"
407                                                 self.baseurl = "http://www.unwetterzentrale.de/uwz/"
408                                                 self.menuurl = self.baseurl + "index.html"
409                                                 self.weatherreporturl = self.baseurl + "lagebericht.html"
410                                 
411                                 f = open(pluginpath + "/last.cfg","w")
412                                 f.write(self.land)
413                                 f.close
414                                 self.downloadMenu()
415                                 self.ThumbTimer.start(1500, True)       
416                                 
417                 def exit(self):
418                                 if self.loadinginprogress:
419                                         reactor.callLater(1,self.exit)
420                                 else:
421                                         try:
422                                                 unlink(self.picfile)
423                                                 unlink(self.reportfile)
424                                         except OSError:
425                                                 pass
426                                         self.close()
427
428
429 #############################                                           
430                                                                                                 
431 def main(session, **kwargs):
432                         session.open(UnwetterMain)
433                                                                 
434 def Plugins(path,**kwargs):
435                         global pluginpath
436                         pluginpath = path
437                         return PluginDescriptor(
438                                         name="Unwetterzentrale", 
439                                         description="www.unwetterzentrale.de und www.uwz.at", 
440                                         icon="uwz.png",
441                                         where = PluginDescriptor.WHERE_PLUGINMENU,
442                                         fnc=main)