1 # -*- coding: utf-8 -*-
3 from BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
6 from datetime import time
15 def get_stations(self, name):
16 """ Get station by direction
17 {'Directionname': [('Station name', 'url')]}
19 if not self._stations.has_key(name):
22 if not self.lines.has_key(name):
25 bs = BeautifulSoup(urlopen(self.lines[name]))
26 tables = bs.findAll('table', {'class': 'text_10pix'})
28 dir = tables[i].div.contents[-1].strip(' ')
31 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
33 sta.append((tr.a.text, settings.line_overview + tr.a['href']))
35 sta.append((tr.text.strip(' '), None))
38 self._stations[name] = st
40 return self._stations[name]
44 """ Dictionary of Line names with url as value
47 bs = BeautifulSoup(urlopen(settings.line_overview))
49 lines = bs.findAll('td', {'class': 'linie'})
53 href = settings.line_overview + line.a['href']
55 self._lines[line.text] = href
57 self._lines[line.img['alt']] = href
61 def get_departures(self, url):
62 """ Get list of next departures
63 integer if time until next departure
64 time if time of next departure
67 #TODO parse line name and direction for station site parsing
70 # FIXME prevent from calling this method with None
73 bs = BeautifulSoup(urlopen(url))
74 result_lines = bs.findAll('table')[-1].findAll('tr')
77 for tr in result_lines[1:]:
80 #TODO replace with logger
81 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
85 time = th[-2].text.split(' ')
87 print 'Invalid time: %s' % time
92 if time.find('rze...') >= 0:
95 # if time to next departure in cell convert to int
98 # check if time of next departue in cell
99 t = time.strip(' ').split(':')
100 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
105 #TODO replace with logger
106 print "[DEBUG] Invalid data:\n%s" % time