1 # -*- coding: utf-8 -*-
3 from gotovienna.BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
5 from datetime import time
8 from errors import LineNotFoundError, StationNotFoundError
10 from gotovienna import defaults
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()[6:-6]
33 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
35 sta.append((tr.a.text, defaults.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(defaults.line_overview))
51 lines = bs.findAll('td', {'class': 'linie'})
55 href = defaults.line_overview + line.a['href']
57 self._lines[line.text] = href
59 self._lines[line.img['alt']] = href
63 def get_url_from_direction(self, line, direction, station):
64 stations = self.get_stations(line)
66 for stationname, url in stations.get(direction, []):
67 if stationname == station:
72 def get_departures(self, url):
73 """ Get list of next departures
74 integer if time until next departure
75 time if time of next departure
78 #TODO parse line name and direction for station site parsing
81 # FIXME prevent from calling this method with None
84 # open url for 90 min timeslot / get departure for next 90 min
85 bs = BeautifulSoup(urlopen(url + "&departureSizeTimeSlot=90"))
86 result_lines = bs.findAll('table')[-1].findAll('tr')
89 for tr in result_lines[1:]:
92 #TODO replace with logger
93 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
99 time = t.text.split(' ')
101 #print 'Invalid time: %s' % time
102 # TODO: Issue a warning OR convert "HH:MM" format to countdown
107 if time.find('rze...') >= 0:
110 # if time to next departure in cell convert to int
111 dep.append(int(time))
113 # check if time of next departue in cell
114 t = time.strip(' ').split(':')
115 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
120 #TODO replace with logger
121 print "[DEBUG] Invalid data:\n%s" % time
126 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
127 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
129 def get_line_sort_key(name):
130 """Return a sort key for a line name
132 >>> get_line_sort_key('U6')
135 >>> get_line_sort_key('D')
138 >>> get_line_sort_key('59A')
141 txt = ''.join(x for x in name if not x.isdigit())
142 num = ''.join(x for x in name if x.isdigit()) or '0'
144 return (txt, int(num))
146 def get_line_type(name):
147 """Get the type of line for the given name
149 >>> get_line_type('U1')
151 >>> get_line_type('59A')
156 elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
158 elif name.startswith('U'):
160 elif name.startswith('N'):
162 elif name in ('D', 'O', 'VRT', 'WLB'):
167 def categorize_lines(lines):
168 """Return a categorized version of a list of line names
170 >>> categorize_lines(['U4', 'U3', '59A'])
171 [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
173 categorized_lines = collections.defaultdict(list)
175 for line in sorted(lines):
176 line_type = get_line_type(line)
177 categorized_lines[line_type].append(line)
179 for lines in categorized_lines.values():
180 lines.sort(key=get_line_sort_key)
182 return [(LINE_TYPE_NAMES[key], categorized_lines[key])
183 for key in sorted(categorized_lines)]
187 def __init__(self, name):
188 self._stations = None
189 self.parser = ITipParser()
190 if name.strip() in self.parser.lines():
191 self.name = name.strip()
193 raise LineNotFoundError('There is no line "%s"' % name.strip())
197 if not self._stations:
198 self._stations = parser.get_stations(self.name)
199 return self._stations
201 def get_departures(self, stationname):
202 stationname = stationname.strip().lower()
203 stations = self.stations
207 for direction in stations.keys():
208 # filter stations starting with stationname
209 stations[direction] = filter(lambda station: station[0].lower().starts_with(stationname), stations)
210 found = found or bool(stations[direction])
213 # TODO return departures
214 raise NotImplementedError()
216 raise StationNotFoundError('There is no stationname called "%s" at route of line "%s"' % (stationname, self.name))