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