1 # -*- coding: utf-8 -*-
3 from BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
5 from datetime import time
9 from gotovienna import defaults
16 def get_stations(self, name):
17 """ Get station by direction
18 {'Directionname': [('Station name', 'url')]}
20 if not self._stations.has_key(name):
23 if not self.lines.has_key(name):
26 bs = BeautifulSoup(urlopen(self.lines[name]))
27 tables = bs.findAll('table', {'class': 'text_10pix'})
29 dir = tables[i].div.contents[-1].strip(' ')
32 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
34 sta.append((tr.a.text, defaults.line_overview + tr.a['href']))
36 sta.append((tr.text.strip(' '), None))
39 self._stations[name] = st
41 return self._stations[name]
45 """ Dictionary of Line names with url as value
48 bs = BeautifulSoup(urlopen(defaults.line_overview))
50 lines = bs.findAll('td', {'class': 'linie'})
54 href = defaults.line_overview + line.a['href']
56 self._lines[line.text] = href
58 self._lines[line.img['alt']] = href
62 def get_departures(self, url):
63 """ Get list of next departures
64 integer if time until next departure
65 time if time of next departure
68 #TODO parse line name and direction for station site parsing
71 # FIXME prevent from calling this method with None
74 bs = BeautifulSoup(urlopen(url))
75 result_lines = bs.findAll('table')[-1].findAll('tr')
78 for tr in result_lines[1:]:
81 #TODO replace with logger
82 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
86 time = th[-2].text.split(' ')
88 print 'Invalid time: %s' % time
93 if time.find('rze...') >= 0:
96 # if time to next departure in cell convert to int
99 # check if time of next departue in cell
100 t = time.strip(' ').split(':')
101 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
106 #TODO replace with logger
107 print "[DEBUG] Invalid data:\n%s" % time