1 # -*- coding: utf-8 -*-
3 from gotovienna.BeautifulSoup import BeautifulSoup
4 #from urllib2 import urlopen
5 from UrlOpener import urlopen
6 from datetime import time
9 from errors import LineNotFoundError, StationNotFoundError
11 from cache import Stations
13 from gotovienna import defaults
16 def __init__(self, line, station, direction, time, lowfloor):
18 self.station = station
19 self.direction = direction
21 self.lowfloor = lowfloor
23 def get_departure_time(self):
24 """ return time object of departure time
26 if type(self.time) == time:
30 def get_departure_deltatime(self):
31 """ return int representing minutes until departure
33 if type(self.time) == int:
39 if type(self.time) == int:
41 elif type(self.time) == time:
42 return self.time.strftime('%H:%M')
46 self._lines = cache.lines
48 def get_stations(self, name):
49 """ Get station by direction
50 {'Directionname': [('Station name', 'url')]}
52 if not name in self.lines:
58 bs = BeautifulSoup(urlopen(self.lines[name]))
59 tables = bs.findAll('table', {'class': 'text_10pix'})
61 dir = tables[i].div.contents[-1].strip()[6:-6]
64 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
66 sta.append((tr.a.text, defaults.line_overview + tr.a['href']))
68 sta.append((tr.text.strip(' '), None))
76 """ Dictionary of Line names with url as value
79 bs = BeautifulSoup(urlopen(defaults.line_overview))
81 lines = bs.findAll('td', {'class': 'linie'})
85 href = defaults.line_overview + line.a['href']
87 self._lines[line.text] = href
89 self._lines[line.img['alt']] = href
93 def get_url_from_direction(self, line, direction, station):
94 stations = self.get_stations(line)
96 for stationname, url in stations.get(direction, []):
97 if stationname == station:
102 def get_departures(self, url):
103 """ Get list of next departures as Departure object
106 #TODO parse line name and direction for station site parsing
109 # FIXME prevent from calling this method with None
110 print "ERROR empty url"
113 # open url for 90 min timeslot / get departure for next 90 min
117 bs = BeautifulSoup(urlopen(url + "&departureSizeTimeSlot=90"))
119 lines = bs.find('form', {'name': 'mainform'}).table.findAll('tr')[1]
120 except AttributeError:
122 msg = bs.findAll('span', {'class': 'rot fett'})
123 if len(msg) > 0 and str(msg[0].text).find(u'technischen St') > 0:
124 print 'Temporary problem'
125 print '\n'.join(map(lambda x: x.text.replace(' ', ''), msg))
126 # FIXME Change to error message after fixing qml gui
132 if len(lines.findAll('td', {'class': 'info'})) > 0:
133 station = lines.span.text.replace(' ', '')
134 line = lines.findAll('span')[-1].text.replace(' ', '')
136 station = lines.td.span.text.replace(' ', '')
137 line = lines.find('td', {'align': 'right'}).span.text.replace(' ', '')
139 result_lines = bs.findAll('table')[-1].findAll('tr')
142 for tr in result_lines[1:]:
143 d = {'station': station}
144 th = tr.findAll('th')
146 #TODO replace with logger
147 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
149 # underground site looks different -.-
152 d['direction'] = th[0].text.replace(' ', '')
156 d['lowfloor'] = th[-1].has_key('img') and th[-1].img.has_key('alt')
157 d['line'] = th[0].text.replace(' ', '')
158 d['direction'] = th[1].text.replace(' ', '')
161 tim = t.text.split(' ')
163 # print '[WARNING] Invalid time: %s' % time
164 # TODO: Issue a warning OR convert "HH:MM" format to countdown
169 if tim.find('rze...') >= 0:
172 # if time to next departure in cell convert to int
175 # check if time of next departue in cell
176 t = tim.strip(' ').split(':')
177 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
182 #TODO replace with logger
183 print "[DEBUG] Invalid data:\n%s" % time
186 dep.append(Departure(**d))
191 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
192 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
194 def get_line_sort_key(name):
195 """Return a sort key for a line name
197 >>> get_line_sort_key('U6')
200 >>> get_line_sort_key('D')
203 >>> get_line_sort_key('59A')
206 txt = ''.join(x for x in name if not x.isdigit())
207 num = ''.join(x for x in name if x.isdigit()) or '0'
209 return (txt, int(num))
211 def get_line_type(name):
212 """Get the type of line for the given name
214 >>> get_line_type('U1')
216 >>> get_line_type('59A')
221 elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
223 elif name.startswith('U'):
225 elif name.startswith('N'):
227 elif name in ('D', 'O', 'VRT', 'WLB'):
232 def categorize_lines(lines):
233 """Return a categorized version of a list of line names
235 >>> categorize_lines(['U4', 'U3', '59A'])
236 [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
238 categorized_lines = collections.defaultdict(list)
240 for line in sorted(lines):
241 line_type = get_line_type(line)
242 categorized_lines[line_type].append(line)
244 for lines in categorized_lines.values():
245 lines.sort(key=get_line_sort_key)
247 return [(LINE_TYPE_NAMES[key], categorized_lines[key])
248 for key in sorted(categorized_lines)]