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