Fixing some errorhandling bugs
[pywienerlinien] / gotovienna / realtime.py
1 # -*- coding: utf-8 -*-
2
3 from gotovienna.BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
5 from datetime import time
6 import re
7 import collections
8 from errors import LineNotFoundError, StationNotFoundError
9
10 from gotovienna import defaults
11
12 class Departure:
13     def __init__(self, line, station, direction, time, lowfloor):
14         self.line = line
15         self.station = station
16         self.direction = direction
17         self.time = time
18         self.lowfloor = lowfloor
19
20     def get_departure_time(self):
21         """ return time object of departure time
22         """
23         if type(self.time) == time:
24             return self.time
25         else:
26             pass
27     def get_departure_deltatime(self):
28         """ return int representing minutes until departure
29         """
30         if type(self.time) == int:
31             return self.time
32         else:
33             pass
34
35     def get_ftime(self):
36         if type(self.time) == int:
37             return str(self.time)
38         elif type(self.time) == time:
39             return self.time.strftime('%H:%M')
40
41 class ITipParser:
42     def __init__(self):
43         self._stations = {}
44         self._lines = {}
45
46     def get_stations(self, name):
47         """ Get station by direction
48         {'Directionname': [('Station name', 'url')]}
49         """
50         if not self._stations.has_key(name):
51             st = {}
52
53             if not self.lines.has_key(name):
54                 return None
55
56             bs = BeautifulSoup(urlopen(self.lines[name]))
57             tables = bs.findAll('table', {'class': 'text_10pix'})
58             for i in range(2):
59                 dir = tables[i].div.contents[-1].strip()[6:-6]
60
61                 sta = []
62                 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
63                     if tr.a:
64                         sta.append((tr.a.text, defaults.line_overview + tr.a['href']))
65                     else:
66                         sta.append((tr.text.strip(' '), None))
67
68                 st[dir] = sta
69             self._stations[name] = st
70
71         return self._stations[name]
72
73     @property
74     def lines(self):
75         """ Dictionary of Line names with url as value
76         """
77         if not self._lines:
78             bs = BeautifulSoup(urlopen(defaults.line_overview))
79             # get tables
80             lines = bs.findAll('td', {'class': 'linie'})
81
82             for line in lines:
83                 if line.a:
84                     href = defaults.line_overview + line.a['href']
85                     if line.text:
86                         self._lines[line.text] = href
87                     elif line.img:
88                         self._lines[line.img['alt']] = href
89
90         return self._lines
91
92     def get_url_from_direction(self, line, direction, station):
93         stations = self.get_stations(line)
94
95         for stationname, url in stations.get(direction, []):
96             if stationname == station:
97                 return url
98
99         return None
100
101     def get_departures(self, url):
102         """ Get list of next departures as Departure object
103         """
104
105         #TODO parse line name and direction for station site parsing
106
107         if not url:
108             # FIXME prevent from calling this method with None
109             print "ERROR empty url"
110             return []
111
112         # open url for 90 min timeslot / get departure for next 90 min
113         retry = 0
114         tries = 2
115         while retry < tries:
116             bs = BeautifulSoup(urlopen(url + "&departureSizeTimeSlot=90"))
117             try:
118                 lines = bs.find('form', {'name': 'mainform'}).table.findAll('tr')[1]
119             except AttributeError:
120                 print 'FetchError'
121                 msg = bs.findAll('span', {'class': 'rot fett'})
122                 if len(msg) > 0 and str(msg[0].text).find(u'technischen St') > 0:
123                     print 'Temporary problem'
124                     print '\n'.join(map(lambda x: x.text.replace('&nbsp;', ''), msg))
125                     # FIXME Change to error message after fixing qml gui
126                     return []
127                 # FIXME more testing
128                 retry += 1
129                 if retry == tries:
130                     return []
131         if len(lines.findAll('td', {'class': 'info'})) > 0:
132             station = lines.span.text.replace('&nbsp;', '')
133             line = lines.findAll('span')[-1].text.replace('&nbsp;', '')
134         else:
135             station = lines.td.span.text.replace('&nbsp;', '')
136             line = lines.find('td', {'align': 'right'}).span.text.replace('&nbsp;', '')
137
138         result_lines = bs.findAll('table')[-1].findAll('tr')
139
140         dep = []
141         for tr in result_lines[1:]:
142             d = {'station': station}
143             th = tr.findAll('th')
144             if len(th) < 2:
145                 #TODO replace with logger
146                 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
147             elif len(th) == 2:
148                 # underground site looks different -.-
149                 d['lowfloor'] = True
150                 d['line'] = line
151                 d['direction'] = th[0].text.replace('&nbsp;', '')
152                 t = th[-1]
153             else:
154                 # all other lines
155                 d['lowfloor'] = th[-1].has_key('img') and th[-1].img.has_key('alt')
156                 d['line'] = th[0].text.replace('&nbsp;', '')
157                 d['direction'] = th[1].text.replace('&nbsp;', '')
158                 t = th[-2]
159             # parse time
160             tim = t.text.split(' ')
161             if len(tim) < 2:
162                 # print '[WARNING] Invalid time: %s' % time
163                 # TODO: Issue a warning OR convert "HH:MM" format to countdown
164                 tim = tim[0]
165             else:
166                 tim = tim[1]
167
168             if tim.find('rze...') >= 0:
169                     d['time'] = 0
170             elif tim.isdigit():
171                 # if time to next departure in cell convert to int
172                 d['time'] = int(tim)
173             else:
174                 # check if time of next departue in cell
175                 t = tim.strip('&nbsp;').split(':')
176                 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
177                     t = map(int, t)
178                     d['time'] = time(*t)
179                 else:
180                     # Unexpected content
181                     #TODO replace with logger
182                     print "[DEBUG] Invalid data:\n%s" % time
183
184             print d
185             dep.append(Departure(**d))
186
187         return dep
188
189
190 UBAHN, TRAM, BUS, NIGHTLINE, OTHER = range(5)
191 LINE_TYPE_NAMES = ['U-Bahn', 'Strassenbahn', 'Bus', 'Nightline', 'Andere']
192
193 def get_line_sort_key(name):
194     """Return a sort key for a line name
195
196     >>> get_line_sort_key('U6')
197     ('U', 6)
198
199     >>> get_line_sort_key('D')
200     ('D', 0)
201
202     >>> get_line_sort_key('59A')
203     ('A', 59)
204     """
205     txt = ''.join(x for x in name if not x.isdigit())
206     num = ''.join(x for x in name if x.isdigit()) or '0'
207
208     return (txt, int(num))
209
210 def get_line_type(name):
211     """Get the type of line for the given name
212
213     >>> get_line_type('U1')
214     UBAHN
215     >>> get_line_type('59A')
216     BUS
217     """
218     if name.isdigit():
219         return TRAM
220     elif name.endswith('A') or name.endswith('B') and name[1].isdigit():
221         return BUS
222     elif name.startswith('U'):
223         return UBAHN
224     elif name.startswith('N'):
225         return NIGHTLINE
226     elif name in ('D', 'O', 'VRT', 'WLB'):
227         return TRAM
228
229     return OTHER
230
231 def categorize_lines(lines):
232     """Return a categorized version of a list of line names
233
234     >>> categorize_lines(['U4', 'U3', '59A'])
235     [('U-Bahn', ['U3', 'U4']), ('Bus', ['59A'])]
236     """
237     categorized_lines = collections.defaultdict(list)
238
239     for line in sorted(lines):
240         line_type = get_line_type(line)
241         categorized_lines[line_type].append(line)
242
243     for lines in categorized_lines.values():
244         lines.sort(key=get_line_sort_key)
245
246     return [(LINE_TYPE_NAMES[key], categorized_lines[key])
247             for key in sorted(categorized_lines)]
248
249
250 class Line:
251     def __init__(self, name):
252         self._stations = None
253         self.parser = ITipParser()
254         if name.strip() in self.parser.lines():
255             self.name = name.strip()
256         else:
257             raise LineNotFoundError('There is no line "%s"' % name.strip())
258
259     @property
260     def stations(self):
261         if not self._stations:
262             self._stations = parser.get_stations(self.name)
263         return self._stations
264
265     def get_departures(self, stationname):
266         stationname = stationname.strip().lower()
267         stations = self.stations
268
269         found = false
270
271         for direction in stations.keys():
272             # filter stations starting with stationname
273             stations[direction] = filter(lambda station: station[0].lower().starts_with(stationname), stations)
274             found = found or bool(stations[direction])
275
276         if found:
277             # TODO return departures
278             raise NotImplementedError()
279         else:
280             raise StationNotFoundError('There is no stationname called "%s" at route of line "%s"' % (stationname, self.name))