1 from BeautifulSoup import BeautifulSoup
2 from urllib2 import urlopen
4 from datetime import time
14 def get_stations(self, name):
15 """ Get station by direction
16 {'Directionname': [('Station name', 'url')]}
18 if not self._stations.has_key(name):
21 if not self.lines.has_key(name):
24 bs = BeautifulSoup(urlopen(self.lines[name]))
25 tables = bs.findAll('table', {'class': 'text_10pix'})
27 dir = tables[i].div.contents[-1].strip(' ')
30 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
32 sta.append((tr.a.text, settings.line_overview + tr.a['href']))
34 sta.append((tr.text.strip(' '), None))
37 self._stations[name] = st
39 return self._stations[name]
43 """ Dictionary of Line names with url as value
46 bs = BeautifulSoup(urlopen(settings.line_overview))
48 lines = bs.findAll('td', {'class': 'linie'})
52 href = settings.line_overview + line.a['href']
54 self._lines[line.text] = href
56 self._lines[line.img['alt']] = href
60 def get_departures(self, url):
61 """ Get list of next departures
62 integer if time until next departure
63 time if time of next departure
66 #TODO parse line name and direction for station site parsing
68 bs = BeautifulSoup(urlopen(url))
69 result_lines = bs.findAll('table')[-1].findAll('tr')
72 for tr in result_lines[1:]:
75 #TODO replace with logger
76 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
80 time = th[-2].text.split(' ')
82 print 'Invalid time: %s' % time
87 if time.find('rze...') >= 0:
90 # if time to next departure in cell convert to int
93 # check if time of next departue in cell
94 t = time.strip(' ').split(':')
95 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
100 #TODO replace with logger
101 print "[DEBUG] Invalid data:\n%s" % time
105 if __name__ == '__main__':
106 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
107 parser.add_argument('-l', metavar='name', type=str, help='line name')
108 parser.add_argument('-s', metavar='name', type=str, help='station name')
110 args = parser.parse_args()
121 stations = itip.get_stations(l)
122 for key in stations.keys():
125 for station in stations[key]:
127 if s.startswith(station[0]) or station[0].startswith(s):
129 print '* %s\n %s .....' % (key, station[0]), itip.get_departures(station[1])
131 print ' %s' % station[0]
134 line = {'U-Bahn': '|', 'Strassenbahn': '|', 'Bus': '|', 'Andere': '|', 'Nightline': '|'}
135 lines_sorted = lines.keys()
137 for li in lines_sorted:
139 type = 'Strassenbahn'
140 elif li.endswith('A') or li.endswith('B') and li[1].isdigit():
142 elif li.startswith('U'):
144 elif li.startswith('N'):
149 line[type] += ' %s |' % li
150 for kv in line.items():