fc1730d49799442e20bf7c73ec58985271a55926
[pywienerlinien] / gotovienna / realtime.py
1 # -*- coding: utf-8 -*-
2
3 from BeautifulSoup import BeautifulSoup
4 from urllib2 import urlopen
5 from datetime import time
6 import argparse
7 import re
8
9 from gotovienna import defaults
10
11 class ITipParser:
12     def __init__(self):
13         self._stations = {}
14         self._lines = {}
15
16     def get_stations(self, name):
17         """ Get station by direction
18         {'Directionname': [('Station name', 'url')]}
19         """
20         if not self._stations.has_key(name):
21             st = {}
22
23             if not self.lines.has_key(name):
24                 return None
25
26             bs = BeautifulSoup(urlopen(self.lines[name]))
27             tables = bs.findAll('table', {'class': 'text_10pix'})
28             for i in range(2):
29                 dir = tables[i].div.contents[-1].strip(' ')
30
31                 sta = []
32                 for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
33                     if tr.a:
34                         sta.append((tr.a.text, defaults.line_overview + tr.a['href']))
35                     else:
36                         sta.append((tr.text.strip(' '), None))
37
38                 st[dir] = sta
39             self._stations[name] = st
40
41         return self._stations[name]
42
43     @property
44     def lines(self):
45         """ Dictionary of Line names with url as value
46         """
47         if not self._lines:
48             bs = BeautifulSoup(urlopen(defaults.line_overview))
49             # get tables
50             lines = bs.findAll('td', {'class': 'linie'})
51
52             for line in lines:
53                 if line.a:
54                     href = defaults.line_overview + line.a['href']
55                     if line.text:
56                         self._lines[line.text] = href
57                     elif line.img:
58                         self._lines[line.img['alt']] = href
59
60         return self._lines
61
62     def get_departures(self, url):
63         """ Get list of next departures
64         integer if time until next departure
65         time if time of next departure
66         """
67
68         #TODO parse line name and direction for station site parsing
69
70         if not url:
71             # FIXME prevent from calling this method with None
72             return []
73
74         bs = BeautifulSoup(urlopen(url))
75         result_lines = bs.findAll('table')[-1].findAll('tr')
76
77         dep = []
78         for tr in result_lines[1:]:
79             th = tr.findAll('th')
80             if len(th) < 2:
81                 #TODO replace with logger
82                 print "[DEBUG] Unable to find th in:\n%s" % str(tr)
83                 continue
84
85             # parse time
86             time = th[-2].text.split(' ')
87             if len(time) < 2:
88                 print 'Invalid time: %s' % time
89                 continue
90
91             time = time[1]
92
93             if time.find('rze...') >= 0:
94                     dep.append(0)
95             elif time.isdigit():
96                 # if time to next departure in cell convert to int
97                 dep.append(int(time))
98             else:
99                 # check if time of next departue in cell
100                 t = time.strip('&nbsp;').split(':')
101                 if len(t) == 2 and all(map(lambda x: x.isdigit(), t)):
102                     t = map(int, t)
103                     dep.append(time(*t))
104                 else:
105                     # Unexpected content
106                     #TODO replace with logger
107                     print "[DEBUG] Invalid data:\n%s" % time
108
109         return dep
110