1 # -*- coding: utf-8 -*-
3 from gotovienna.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_url_from_direction(self, line, direction, station):
63 stations = self.get_stations(line)
65 for stationname, url in stations.get(direction, []):
66 if stationname == station:
71 def get_departures(self, url):
72 """ Get list of next departures
73 integer if time until next departure
74 time if time of next departure
77 #TODO parse line name and direction for station site parsing
80 # FIXME prevent from calling this method with None
83 bs = BeautifulSoup(urlopen(url))
84 result_lines = bs.findAll('table')[-1].findAll('tr')
87 for tr in result_lines[1:]:
90 #TODO replace with logger
91 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
95 time = th[-2].text.split(' ')
97 #print 'Invalid time: %s' % time
98 # TODO: Issue a warning OR convert "HH:MM" format to countdown
103 if time.find('rze...') >= 0:
106 # if time to next departure in cell convert to int
107 dep.append(int(time))
109 # check if time of next departue in cell
110 t = time.strip(' ').split(':')
111 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
116 #TODO replace with logger
117 print "[DEBUG] Invalid data:\n%s" % time
122 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
123 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
125 def get_line_sort_key(name):
126 """Return a sort key for a line name
128 >>> get_line_sort_key('U6')
131 >>> get_line_sort_key('D')
134 >>> get_line_sort_key('59A')
137 txt = ''.join(x for x in name if not x.isdigit())
138 num = ''.join(x for x in name if x.isdigit()) or '0'
140 return (txt, int(num))
142 def get_line_type(name):
143 """Get the type of line for the given name
145 >>> get_line_type('U1')
147 >>> get_line_type('59A')
152 elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
154 elif name.startswith('U'):
156 elif name.startswith('N'):
158 elif name in ('D', 'O', 'VRT', 'WLB'):
163 def categorize_lines(lines):
164 """Return a categorized version of a list of line names
166 >>> categorize_lines(['U4', 'U3', '59A'])
167 [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
169 categorized_lines = collections.defaultdict(list)
171 for line in sorted(lines):
172 line_type = get_line_type(line)
173 categorized_lines[line_type].append(line)
175 for lines in categorized_lines.values():
176 lines.sort(key=get_line_sort_key)
178 return [(LINE_TYPE_NAMES[key], categorized_lines[key])
179 for key in sorted(categorized_lines)]