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
13 def __init__(self, line, station, direction, time, lowfloor):
15 self.station = station
16 self.direction = direction
18 self.lowfloor = lowfloor
20 def get_departure_time(self):
21 """ return time object of departure time
23 if type(self.time) == time:
27 def get_departure_deltatime(self):
28 """ return int representing minutes until departure
30 if type(self.time) == int:
36 if type(self.time) == int:
38 elif type(self.time) == time:
39 return self.time.strftime('%H:%M')
46 def get_stations(self, name):
47 """ Get station by direction
48 {'Directionname': [('Station name', 'url')]}
50 if not self._stations.has_key(name):
53 if not self.lines.has_key(name):
56 bs = BeautifulSoup(urlopen(self.lines[name]))
57 tables = bs.findAll('table', {'class': 'text_10pix'})
59 dir = tables[i].div.contents[-1].strip()[6:-6]
62 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
64 sta.append((tr.a.text, defaults.line_overview + tr.a['href']))
66 sta.append((tr.text.strip(' '), None))
69 self._stations[name] = st
71 return self._stations[name]
75 """ Dictionary of Line names with url as value
78 bs = BeautifulSoup(urlopen(defaults.line_overview))
80 lines = bs.findAll('td', {'class': 'linie'})
84 href = defaults.line_overview + line.a['href']
86 self._lines[line.text] = href
88 self._lines[line.img['alt']] = href
92 def get_url_from_direction(self, line, direction, station):
93 stations = self.get_stations(line)
95 for stationname, url in stations.get(direction, []):
96 if stationname == station:
101 def get_departures(self, url):
102 """ Get list of next departures as Departure object
105 #TODO parse line name and direction for station site parsing
108 # FIXME prevent from calling this method with None
109 print "ERROR empty url"
112 # open url for 90 min timeslot / get departure for next 90 min
113 bs = BeautifulSoup(urlopen(url + "&departureSizeTimeSlot=90"))
115 lines = bs.findAll('table')[-2].findAll('tr')
117 station = lines[0].span.text.replace(' ', '')
118 line = lines[0].findAll('span')[-1].text.replace(' ', '')
120 station = lines[1].td.span.text.replace(' ', '')
123 result_lines = bs.findAll('table')[-1].findAll('tr')
126 for tr in result_lines[1:]:
127 d = {'station': station}
128 th = tr.findAll('th')
130 #TODO replace with logger
131 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
133 # underground site looks different -.-
136 d['direction'] = th[0].text.replace(' ', '')
140 d['lowfloor'] = th[-1].has_key('img') and th[-1].img.has_key('alt')
141 d['line'] = th[0].text.replace(' ', '')
142 d['direction'] = th[1].text.replace(' ', '')
145 tim = t.text.split(' ')
147 # print '[WARNING] Invalid time: %s' % time
148 # TODO: Issue a warning OR convert "HH:MM" format to countdown
153 if tim.find('rze...') >= 0:
156 # if time to next departure in cell convert to int
159 # check if time of next departue in cell
160 t = tim.strip(' ').split(':')
161 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
166 #TODO replace with logger
167 print "[DEBUG] Invalid data:\n%s" % time
170 dep.append(Departure(**d))
175 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
176 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
178 def get_line_sort_key(name):
179 """Return a sort key for a line name
181 >>> get_line_sort_key('U6')
184 >>> get_line_sort_key('D')
187 >>> get_line_sort_key('59A')
190 txt = ''.join(x for x in name if not x.isdigit())
191 num = ''.join(x for x in name if x.isdigit()) or '0'
193 return (txt, int(num))
195 def get_line_type(name):
196 """Get the type of line for the given name
198 >>> get_line_type('U1')
200 >>> get_line_type('59A')
205 elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
207 elif name.startswith('U'):
209 elif name.startswith('N'):
211 elif name in ('D', 'O', 'VRT', 'WLB'):
216 def categorize_lines(lines):
217 """Return a categorized version of a list of line names
219 >>> categorize_lines(['U4', 'U3', '59A'])
220 [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
222 categorized_lines = collections.defaultdict(list)
224 for line in sorted(lines):
225 line_type = get_line_type(line)
226 categorized_lines[line_type].append(line)
228 for lines in categorized_lines.values():
229 lines.sort(key=get_line_sort_key)
231 return [(LINE_TYPE_NAMES[key], categorized_lines[key])
232 for key in sorted(categorized_lines)]
236 def __init__(self, name):
237 self._stations = None
238 self.parser = ITipParser()
239 if name.strip() in self.parser.lines():
240 self.name = name.strip()
242 raise LineNotFoundError('There is no line "%s"' % name.strip())
246 if not self._stations:
247 self._stations = parser.get_stations(self.name)
248 return self._stations
250 def get_departures(self, stationname):
251 stationname = stationname.strip().lower()
252 stations = self.stations
256 for direction in stations.keys():
257 # filter stations starting with stationname
258 stations[direction] = filter(lambda station: station[0].lower().starts_with(stationname), stations)
259 found = found or bool(stations[direction])
262 # TODO return departures
263 raise NotImplementedError()
265 raise StationNotFoundError('There is no stationname called "%s" at route of line "%s"' % (stationname, self.name))