added parsing result overview page
[pywienerlinien] / parseHtml.py
1 from BeautifulSoup import BeautifulSoup
2 import urllib2
3 from datetime import time, datetime
4
5
6 class Parser:
7     _overview = None
8     _details = None
9
10     def __init__(self, html):
11         self.soup = BeautifulSoup(html)
12
13     def __iter__(self):
14         for detail in self.details():
15             yield detail
16         raise IndexError()
17
18     def _parse_details(self):
19         return self.soup.findAll('div', {'class': 'data_table tourdetail'})
20
21     @property
22     def details(self):
23         if not self._details:
24             self._details = self._parse_details()
25
26         return self._details
27
28     def _parse_overview(self):
29         """
30         Returns dict containing
31         date: datetime
32         time: [time, time]
33         duration: time
34         change: int
35         price: float
36         """
37         # get overview table
38         table = self.soup.find('table', {'id': 'tbl_fahrten'})
39         # get rows
40         rows = table.findAll('tr')[1:]
41         overview = map(lambda x: {
42                        'date': datetime.strptime(x.find('td', {'class': 'col_date'}).text, '%d.%m.%Y'), # grab date
43                        'time': map(lambda x: time(*map(lambda x: int(x), x.split(':'))), x.find('td', {'class': 'col_time'}).text.split('  - ')), # extract times
44                        'duration': time(*map(lambda x: int(x), x.find('td', {'class': 'col_duration'}).text.split(':'))), # grab duration
45                        'change': int(x.find('td', {'class': 'col_change'}).text), # grab changes
46                        'price': float(x.find('td', {'class': 'col_price'}).text.replace(',', '.')) # grab price
47                        },
48             rows)
49
50         return overview
51
52     @property
53     def overview(self):
54         if not self._overview:
55             self._overview = self._parse_overview()
56
57         return self._overview
58
59
60 class iTipParser:
61
62     def __init__(self):
63         raise NotImplementedError