1 # -*- coding: utf-8 -*-
3 from BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
5 from datetime import time
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(' ')
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_departures(self, url):
64 """ Get list of next departures
65 integer if time until next departure
66 time if time of next departure
69 #TODO parse line name and direction for station site parsing
72 # FIXME prevent from calling this method with None
75 bs = BeautifulSoup(urlopen(url))
76 result_lines = bs.findAll('table')[-1].findAll('tr')
79 for tr in result_lines[1:]:
82 #TODO replace with logger
83 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
87 time = th[-2].text.split(' ')
89 print 'Invalid time: %s' % time
94 if time.find('rze...') >= 0:
97 # if time to next departure in cell convert to int
100 # check if time of next departue in cell
101 t = time.strip(' ').split(':')
102 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
107 #TODO replace with logger
108 print "[DEBUG] Invalid data:\n%s" % time
113 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
114 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
116 def get_line_sort_key(name):
117 """Return a sort key for a line name
119 >>> get_line_sort_key('U6')
122 >>> get_line_sort_key('D')
125 >>> get_line_sort_key('59A')
128 txt = ''.join(x for x in name if not x.isdigit())
129 num = ''.join(x for x in name if x.isdigit()) or '0'
131 return (txt, int(num))
133 def get_line_type(name):
134 """Get the type of line for the given name
136 >>> get_line_type('U1')
138 >>> get_line_type('59A')
143 elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
145 elif name.startswith('U'):
147 elif name.startswith('N'):
149 elif name in ('D', 'O', 'VRT', 'WLB'):
154 def categorize_lines(lines):
155 """Return a categorized version of a list of line names
157 >>> categorize_lines(['U4', 'U3', '59A'])
158 [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
160 categorized_lines = collections.defaultdict(list)
162 for line in sorted(lines):
163 line_type = get_line_type(line)
164 categorized_lines[line_type].append(line)
166 for lines in categorized_lines.values():
167 lines.sort(key=get_line_sort_key)
169 return [(LINE_TYPE_NAMES[key], categorized_lines[key])
170 for key in sorted(categorized_lines)]