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