remove unneeded imports
[enigma2-plugins.git] / trafficinfo / src / plugin.py
1 # by 3c5x9@2007
2 from enigma import eTimer
3 from Screens.Screen import Screen
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.MenuList import MenuList
7
8 from Components.MultiContent import MultiContentEntryText
9 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_WRAP
10
11 from Plugins.Plugin import PluginDescriptor
12 import xml.dom.minidom
13
14 from twisted.web.client import getPage
15 from twisted.internet import reactor
16
17 ###############################################################################        
18 class TrafficInfoMain(Screen):
19     skin = """
20         <screen position="110,83" size="530,430" title="Verkehrsinfo" >
21                         
22             <widget name="sectionlist" position="0,0" size="530,125" scrollbarMode="showOnDemand" />            
23             <widget name="itemlist" position="0,130" size="530,125" scrollbarMode="showOnDemand" />            
24             <widget name="itemdetails" position="0,260" size="530,140" font="Regular;20" halign=\"center\" valign=\"center\"/>            
25             <widget name="statuslabel" position="0,400" size="530,30" halign=\"left\"/>           
26         </screen>
27         """
28     def __init__(self, session,args = 0):
29         self.loadinginprogress = False 
30         self.skin = TrafficInfoMain.skin
31         self.session = session
32         Screen.__init__(self, session)
33         self.menu = args
34         self["sectionlist"] = MenuList([])
35         self["itemlist"] = ItemList([])
36         self["statuslabel"] = Label("")
37         self["itemdetails"] = Label("")
38         self["actions"] = ActionMap(["ChannelSelectBaseActions","WizardActions", "DirectionActions","MenuActions","NumberActions"], 
39             {
40              "ok":                  self.go,
41              "back":            self.exit,
42              "nextBouquet":     self.selectSectionlist,
43              "prevBouquet":     self.selectItemlist,
44              "down":            self.down,
45              "up":                  self.up,
46              "left":            self.left,
47              "right":           self.right,
48              }, -1)
49         self.statuslabelcleartimer = eTimer()
50         self.statuslabelcleartimer.timeout.get().append(self.clearStatusLabel)
51         
52         self["itemlist"].onSelectionChanged.append(self.onItemSelected)
53         self.selectSectionlist()
54         
55         self.onShown.append(self.getSections)
56         
57     def exit(self):
58         if self.loadinginprogress:
59             reactor.callLater(1,self.exit)
60         else:
61             self.close()
62
63     def selectSectionlist(self):
64         self.currList = "sectionlist"
65         self["sectionlist"].selectionEnabled(1)
66         self["itemlist"].selectionEnabled(0)
67         
68     def selectItemlist(self):
69         self.currList = "itemlist"
70         self["sectionlist"].selectionEnabled(0)
71         self["itemlist"].selectionEnabled(1)
72         self["itemlist"].selectionChanged()
73         
74     def go(self):
75         if self.currList == "sectionlist":
76             self.onSectionSelected()
77
78     def up(self):
79         self[self.currList].up()
80     
81     def down(self):
82         self[self.currList].down()
83         
84     def left(self):
85         self[self.currList].pageUp()
86     
87     def right(self):
88         self[self.currList].pageDown()
89         
90     def onSectionSelected(self):
91         c = self["sectionlist"].getCurrent()
92         if c is not None:
93             self.setTitle("Verkehrsinfo: "+c[1].name)
94             self.getItemsOfSection(c[1])
95         
96     def onItemSelected(self):
97         if self["itemlist"].getCurrent() is not None:
98             c = self["itemlist"].getCurrent()[0]
99             if c is not None:
100                 self["itemdetails"].setText(c.text)
101             
102     ###########
103     def clearStatusLabel(self):
104         self["statuslabel"].setText("")
105         
106     def setStatusLabel(self,text):
107         self.statuslabelcleartimer.stop()         
108         self["statuslabel"].setText(text)
109         self.statuslabelcleartimer.start(3000)
110         
111     def getSections(self):
112         self.setStatusLabel("loading sections")
113         self.loadinginprogress = True    
114         getPage("http://wap.verkehrsinfo.de/wvindex.php3").addCallback(self.sectionsLoaded).addErrback(self.sectionsLoadingFaild)
115     
116     def sectionsLoadingFaild(self,raw):
117         self.loadinginprogress = False
118         self.setStatusLabel("loading sections failed"+raw)
119         
120     def sectionsLoaded(self,raw):
121         self.loadinginprogress = False
122         try:
123             xmldoc = xml.dom.minidom.parseString( raw)
124             self.sections = []
125             for i in xmldoc.getElementsByTagName("a"):
126                 link = "/"+i.getAttribute("href")
127                 name = i.toxml().split(">")[1].split("<")[0]
128                 self.sections.append(TrafficInfoSection(name,link))
129             self.onSectionsLoaded()
130         except xml.parsers.expat.ExpatError,e:
131             print e
132             print raw
133             self.setStatusLabel("loading sections failed")
134
135     def onSectionsLoaded(self):
136         self.setStatusLabel("sections loaded")
137         list = []
138         for i in self.sections:
139             if i.name.startswith("BRD") is not True:
140                 list.append((i.name,i))
141         list.sort()
142         self["sectionlist"].l.setList(list)
143         self["sectionlist"].instance.moveSelectionTo(0)
144
145     def onItemsLoaded(self):
146         list=[]
147         for item in self.trafficitems:
148             res = [ item ]
149             res.append(MultiContentEntryText(pos=(0, 0), size=(75, 20), font=0, flags = RT_HALIGN_LEFT|RT_WRAP, text = item.street))
150             res.append(MultiContentEntryText(pos=(75,0), size=(455, 20), font=1, flags = RT_HALIGN_LEFT, text = item.direction))
151             list.append(res)
152         self["itemlist"].l.setList(list)
153         self["itemlist"].instance.moveSelectionTo(0)
154         self.setStatusLabel("messages loaded")
155
156     ##########
157     def getItemsOfSection(self,section):
158         print "loading section",section.name  ,section.link
159         self.setStatusLabel("loading messages "+section.name)
160         self.loadinginprogress = True    
161         getPage("http://wap.verkehrsinfo.de"+section.link).addCallback(self.trafficitemsLoaded).addErrback(self.trafficitemsLoadingFaild)
162
163     def trafficitemsLoadingFaild(self,raw):
164         self.loadinginprogress = False
165         print "loading items faild",raw
166         self.setStatusLabel("loading messages faild"+raw)
167         
168     def trafficitemsLoaded(self,raw):
169         self.loadinginprogress = False
170         try:
171             raw = raw.replace("&amp","")
172             xmldoc = xml.dom.minidom.parseString( raw)
173             self.trafficitems = []
174             for item in  xmldoc.getElementsByTagName("p"):
175                 self.trafficitems.append(self.parseItem(item))
176             self.onItemsLoaded()
177         except xml.parsers.expat.ExpatError,e:
178             print e
179             print raw
180             self.setStatusLabel("loading messages faild! Parsing Error")
181         
182     def parseItem(self,item):
183         source=item.toxml()
184         i= item.getElementsByTagName("b")
185         source=source.replace(i[0].toxml(),"")
186         street = i[0].toxml().replace("<b>","").replace("</b>","").replace("\n","")
187         
188         source=source.replace(i[1].toxml(),"")
189         direction = i[1].toxml().replace("<b>","").replace("</b>","").replace("\n","")
190         details = source.replace("<p>","").replace("</p>","").replace("<small>","").replace("</small>","").replace("<br/>","").replace("\n","")
191         if street == "<b/>":
192             street = "Info"
193         return TrafficInfoItem(street,direction,details)
194
195 class ItemList(MenuList):
196     def __init__(self, items):
197         MenuList.__init__(self,items)
198         self.l = eListboxPythonMultiContent()
199         self.l.setFont(0, gFont("Regular", 20))
200         self.l.setFont(1, gFont("Regular", 18))
201     GUI_WIDGET = eListbox
202
203     def getCurrentEntry(self):
204         return self.l.getCurrentSelection()
205
206 ####################
207 class TrafficInfoSection:
208     def __init__(self, name,link):
209         self.name = name.encode("utf-8")
210         self.link = link.encode("utf-8")
211
212     def __str__(self):
213         return "name="+self.name+", link="+self.link
214
215 ####################
216 class TrafficInfoItem:
217     def __init__(self, street,direction,text):
218         self.street = street.encode("utf-8")
219         self.direction = direction.encode("utf-8")
220         self.text = text.encode("utf-8")
221
222     def __str__(self):
223         return "street="+self.street+", dir="+self.direction+", text="+self.text
224
225 #############################
226 def main(session, **kwargs):
227     session.open(TrafficInfoMain)
228
229 def Plugins(**kwargs):
230   return PluginDescriptor(name="Verkehrsinfo",description="view german traffic jam informations",where = PluginDescriptor.WHERE_PLUGINMENU,fnc = main)
231