1 from BeautifulSoup import BeautifulSoup
2 from urllib2 import urlopen
4 from datetime import time
12 def get_stations(self, name):
13 """ Get station by direction
14 {'Directionname': [('Station name', 'url')]}
16 if not self._stations.has_key(name):
19 if not self.lines.has_key(name):
22 bs = BeautifulSoup(urlopen(self.lines[name]))
23 tables = bs.findAll('table', {'class': 'text_10pix'})
25 dir = tables[i].div.contents[-1].strip(' ')
28 for tr in tables[0].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
30 sta.append((tr.a.text, settings.line_overview + tr.a['href']))
32 sta.append((tr.text.strip(' '), None))
35 self._stations[name] = st
37 return self._stations[name]
41 """ Dictionary of Line names with url as value
44 bs = BeautifulSoup(urlopen(settings.line_overview))
46 lines = bs.findAll('td', {'class': 'linie'})
50 href = settings.line_overview + line.a['href']
52 self._lines[line.text] = href
54 self._lines[line.img['alt']] = href
58 def get_departures(self, url):
59 """ Get list of next departures
60 integer if time until next departure
61 time if time of next departure
64 #TODO parse line name and direction for station site parsing
66 bs = BeautifulSoup(urlopen(url))
67 result_lines = bs.findAll('table')[-1].findAll('tr')
70 for tr in result_lines[1:]:
73 #TODO replace with logger
74 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
78 time = th[-2].text.split(' ')
80 print 'Invalid time: %s' % time
86 # if time to next departure in cell convert to int
89 # check if time of next departue in cell
90 t = time.strip(' ').split(':')
91 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
96 #TODO replace with logger
97 print "[DEBUG] Invalid data:\n%s" % time