initial commit
[masstransit] / gtk_transilien.py
1 #!/usr/bin/python
2 import pygtk
3 pygtk.require("2.0")
4 import gtk
5 import urllib2
6 import HTMLParser
7 import hildon
8 import ConfigParser
9
10 from portrait import FremantleRotation
11
12 #main_window = mainWindow # your main hildon.StackableWindow
13 app_name = 'NameOfYourApp' # the name of your app
14 app_version = '1.0' # the version number of your app
15 initial_mode = FremantleRotation.AUTOMATIC
16
17 class tableParser(HTMLParser.HTMLParser):
18         def __init__(self):
19                 HTMLParser.HTMLParser.__init__(self)
20                 self.table_horaires3 = False
21                 self.code_de_mission = False
22                 self.a_code_de_mission = False
23                 self.heure_de_passage = False
24                 self.liste_train = []
25                 self.liste_horaire = []
26                 
27         def handle_starttag(self, tag, attrs):
28                 if (tag == 'table' and (dict(attrs)['class'] == 'horaires3')):
29                         self.table_horaires3 = True
30                 
31                 elif self.table_horaires3 and tag == 'td':
32                         try:
33                                 self.code_de_mission = (
34                                         dict(attrs)['headers'] == 'Code_de_mission')
35                                 self.heure_de_passage = (
36                                         dict(attrs)['headers'] == 'Heure_de_passage')
37                         except KeyError:
38                                 if dict(attrs).has_key('headers'):
39                                         raise
40                                 else:
41                                         pass
42                 else:
43                         self.a_code_de_mission = (tag == 'a' and self.code_de_mission)
44                 
45         def handle_data(self, data):
46                 if self.a_code_de_mission:
47                         self.liste_train.append(data.strip())
48                 if self.heure_de_passage:
49                         self.liste_horaire.append(data.strip())
50                         
51         def handle_endtag(self,tag):
52                 self.a_code_de_mission ^= (self.a_code_de_mission and tag == 'a')
53                 self.heure_de_passage ^= (self.heure_de_passage and tag == 'td')
54
55 class TransilienUI:
56         def __init__(self):
57                 mainWindow = hildon.Window()
58                 mainWindow.set_title("Horaires des Prochains Trains")
59                 mainWindow.connect("destroy", self.on_mainWindow_destroy)
60
61                 rotation_object = FremantleRotation(app_name, mainWindow, app_version, initial_mode)
62                 refreshButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
63                               hildon.BUTTON_ARRANGEMENT_HORIZONTAL, "Actualiser")
64                 refreshButton.connect("clicked", self.on_refreshButton_clicked)
65
66                 self.treestore = gtk.TreeStore(str, str)
67                 self.treeview = gtk.TreeView(self.treestore)
68
69                 self.tvcolumn_train = gtk.TreeViewColumn('Train', gtk.CellRendererText(), text=0)
70                 self.treeview.append_column(self.tvcolumn_train)
71                 
72                 self.tvcolumn_horaire = gtk.TreeViewColumn('Horaire', gtk.CellRendererText(), text=1)
73                 self.treeview.append_column(self.tvcolumn_horaire)
74
75
76                 picker_button_source = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
77                 picker_button_source.set_title("Gare de Depart")
78                 self.combo_source = hildon.TouchSelectorEntry(text=True)
79                 self.combo_dest = hildon.TouchSelectorEntry(text=True)
80
81                 self.c = ConfigParser.ConfigParser()
82                 self.c.read('example.cfg')
83                 for i in self.c.items('ListeDesGares'):
84                         self.combo_source.append_text(i[0])
85                         self.combo_dest.append_text(i[0])
86                 picker_button_source.set_selector(self.combo_source)
87                 
88
89                 picker_button_dest = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
90                 picker_button_dest.set_title("Gare d'arrivee")
91                 picker_button_dest.set_selector(self.combo_dest)
92                 
93                 
94                 vBox = gtk.VBox()
95                 hBox = gtk.HBox()
96                 vBox.pack_start(hBox)
97                 hBox.pack_start(picker_button_source)
98                 hBox.pack_start(picker_button_dest)
99                 vBox.pack_start(self.treeview)
100                 vBox.pack_start(refreshButton)
101                 
102
103                 mainWindow.add(vBox)
104                 mainWindow.show_all()
105
106         def on_mainWindow_destroy(self, widget):
107                 gtk.main_quit()
108
109         def on_refreshButton_clicked(self, widget):
110                 self.treestore.clear()
111                 p = tableParser()
112                 print self.c.get('ListeDesGares', self.combo_source.get_current_text())
113                 print self.c.get('ListeDesGares', self.combo_dest.get_current_text())
114                 p.feed(urllib2.urlopen('http://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart='+self.c.get('ListeDesGares', self.combo_source.get_current_text())+'&codeTr3aDest='+self.c.get('ListeDesGares', self.combo_dest.get_current_text())+'&urlModule=/site/pid/184&gareAcc=true').read())
115                 z=0
116                 for i in p.liste_train:
117                         print p.liste_horaire[z]
118                         self.treestore.append(None, [i, p.liste_horaire[z]])
119                         z += 1
120
121 if __name__ == "__main__":
122         TransilienUI()
123         gtk.main()