2 # -*- coding: UTF-8 -*-
4 from BeautifulSoup import BeautifulSoup
5 from urllib2 import urlopen
7 from datetime import time
17 def get_stations(self, name):
18 """ Get station by direction
19 {'Directionname': [('Station name', 'url')]}
21 if not self._stations.has_key(name):
24 if not self.lines.has_key(name):
27 bs = BeautifulSoup(urlopen(self.lines[name]))
28 tables = bs.findAll('table', {'class': 'text_10pix'})
30 dir = tables[i].div.contents[-1].strip(' ')
33 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
35 sta.append((tr.a.text, settings.line_overview + tr.a['href']))
37 sta.append((tr.text.strip(' '), None))
40 self._stations[name] = st
42 return self._stations[name]
46 """ Dictionary of Line names with url as value
49 bs = BeautifulSoup(urlopen(settings.line_overview))
51 lines = bs.findAll('td', {'class': 'linie'})
55 href = settings.line_overview + line.a['href']
57 self._lines[line.text] = href
59 self._lines[line.img['alt']] = href
63 def get_departures(self, url):
64 """ Get list of next departures
65 integer if time until next departure
66 time if time of next departure
69 #TODO parse line name and direction for station site parsing
72 # FIXME prevent from calling this method with None
75 bs = BeautifulSoup(urlopen(url))
76 result_lines = bs.findAll('table')[-1].findAll('tr')
79 for tr in result_lines[1:]:
82 #TODO replace with logger
83 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
87 time = th[-2].text.split(' ')
89 print 'Invalid time: %s' % time
94 if time.find('rze...') >= 0:
97 # if time to next departure in cell convert to int
100 # check if time of next departue in cell
101 t = time.strip(' ').split(':')
102 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
107 #TODO replace with logger
108 print "[DEBUG] Invalid data:\n%s" % time
112 if __name__ == '__main__':
113 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
114 parser.add_argument('-l', metavar='name', type=str, help='line name')
115 parser.add_argument('-s', metavar='name', type=str, help='station name')
117 args = parser.parse_args()
126 s = args.s.decode('UTF-8')
131 stations = itip.get_stations(l)
132 for key in stations.keys():
135 for station in stations[key]:
137 if s.startswith(station[0]) or station[0].startswith(s):
139 print '* %s\n %s .....' % (key, station[0]), itip.get_departures(station[1])
141 print ' %s' % station[0]
144 line = {'U-Bahn': '|', 'Strassenbahn': '|', 'Bus': '|', 'Andere': '|', 'Nightline': '|'}
145 lines_sorted = lines.keys()
147 for li in lines_sorted:
149 type = 'Strassenbahn'
150 elif li.endswith('A') or li.endswith('B') and li[1].isdigit():
152 elif li.startswith('U'):
154 elif li.startswith('N'):
159 line[type] += ' %s |' % li
160 for kv in line.items():