VERSION 0.1.2 Correction indentation
[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
18 class tableParser(HTMLParser.HTMLParser):
19     def __init__(self):
20         HTMLParser.HTMLParser.__init__(self)
21         self.table_horaires3 = False
22         self.code_de_mission = False
23         self.a_code_de_mission = False
24         self.heure_de_passage = False
25         self.liste_train = []
26         self.liste_horaire = []
27         
28     def handle_starttag(self, tag, attrs):
29         if (tag == 'table' and (dict(attrs)['class'] == 'horaires3')):
30             self.table_horaires3 = True
31         
32         elif self.table_horaires3 and tag == 'td':
33             try:
34                 self.code_de_mission = (
35                     dict(attrs)['headers'] == 'Code_de_mission')
36                 self.heure_de_passage = (
37                     dict(attrs)['headers'] == 'Heure_de_passage')
38             except KeyError:
39                 if dict(attrs).has_key('headers'):
40                     raise
41                 else:
42                     pass
43         else:
44             self.a_code_de_mission = (tag == 'a' and self.code_de_mission)
45         
46     def handle_data(self, data):
47         if self.a_code_de_mission:
48             self.liste_train.append(data.strip())
49         if self.heure_de_passage:
50             self.liste_horaire.append(data.strip())
51             
52     def handle_endtag(self,tag):
53         self.a_code_de_mission ^= (self.a_code_de_mission and tag == 'a')
54         self.heure_de_passage ^= (self.heure_de_passage and tag == 'td')
55
56 class Trajet(object):
57     def __init__(self, gare_source, gare_dest):
58         self.gare_source = gare_source
59         self.gare_dest = gare_dest
60         self.parse() 
61     def get_liste_train(self):
62         return self.p.liste_train 
63     def get_liste_horaire(self):
64         return self.p.liste_horaire
65     def parse(self):
66         self.p = tableParser()
67         print "URL:" 
68         print 'http://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart='+self.gare_source.shortname+'&codeTr3aDest='+self.gare_dest.shortname+'&urlModule=/site/pid/184&gareAcc=true'
69         self.p.feed(urllib2.urlopen('http://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart='+self.gare_source.shortname+'&codeTr3aDest='+self.gare_dest.shortname+'&urlModule=/site/pid/184&gareAcc=true').read())
70
71 class ConfFile(object):
72     def __init__(self, fichier):
73         self.c = ConfigParser.ConfigParser()
74         self.c.read(fichier)
75     def get_short_name(self, longname):
76         return self.c.get('ListeDesGares', longname)
77     def get_liste_des_gares(self):
78         return self.c.items('ListeDesGares') 
79
80 class LongNameGare(object):
81     def __init__(self, longname):
82         self.longname = longname
83     def get_gare(self, conffile):
84         short_name = conffile.get_short_name(self.longname)
85         return Gare(short_name)
86
87 class Gare(object):
88     def __init__(self, shortname):
89         self.shortname = shortname
90
91
92
93
94 class TransilienUI:
95     def __init__(self):
96         mainWindow = hildon.Window()
97         mainWindow.set_title("Horaires des Prochains Trains")
98         mainWindow.connect("destroy", self.on_mainWindow_destroy)
99
100         rotation_object = FremantleRotation(app_name, mainWindow, app_version, initial_mode)
101         refreshButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
102                               hildon.BUTTON_ARRANGEMENT_HORIZONTAL, "Actualiser")
103         refreshButton.connect("clicked", self.on_refreshButton_clicked)
104
105         self.treestore = gtk.TreeStore(str, str)
106         self.treeview = gtk.TreeView(self.treestore)
107
108         self.tvcolumn_train = gtk.TreeViewColumn('Train', gtk.CellRendererText(), text=0)
109         self.treeview.append_column(self.tvcolumn_train)
110         
111         self.tvcolumn_horaire = gtk.TreeViewColumn('Horaire', gtk.CellRendererText(), text=1)
112         self.treeview.append_column(self.tvcolumn_horaire)
113
114
115         picker_button_source = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
116         picker_button_source.set_title("Gare de Depart")
117         self.combo_source = hildon.TouchSelectorEntry(text=True)
118         self.combo_dest = hildon.TouchSelectorEntry(text=True)
119
120         for i in ConfFile('example.cfg').get_liste_des_gares():
121             self.combo_source.append_text(i[0])
122             self.combo_dest.append_text(i[0])
123         picker_button_source.set_selector(self.combo_source)
124         
125
126         picker_button_dest = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
127         picker_button_dest.set_title("Gare d'arrivee")
128         picker_button_dest.set_selector(self.combo_dest)
129         
130         vBox = gtk.VBox()
131         hBox = gtk.HBox()
132         vBox.pack_start(hBox)
133         hBox.pack_start(picker_button_source)
134         hBox.pack_start(picker_button_dest)
135         vBox.pack_start(self.treeview)
136         vBox.pack_start(refreshButton)
137         
138
139         mainWindow.add(vBox)
140         mainWindow.show_all()
141
142     def on_mainWindow_destroy(self, widget):
143         gtk.main_quit()
144
145     def on_refreshButton_clicked(self, widget):
146         self.treestore.clear()
147         trajet = Trajet(LongNameGare(self.combo_source.get_current_text()).get_gare(ConfFile('example.cfg')), LongNameGare(self.combo_dest.get_current_text()).get_gare(ConfFile('example.cfg')))
148         z=0
149         for i in trajet.get_liste_train():
150             liste_horaire = trajet.get_liste_horaire()
151             self.treestore.append(None, [i, liste_horaire[z]])
152             z += 1
153
154
155
156 if __name__ == "__main__":
157     TransilienUI()
158     gtk.main()