added parsing result overview page
authorFlorian Schweikert <kelvan@logic.at>
Fri, 25 Feb 2011 13:35:33 +0000 (14:35 +0100)
committerFlorian Schweikert <kelvan@logic.at>
Fri, 25 Feb 2011 13:35:33 +0000 (14:35 +0100)
parseHtml.py [new file with mode: 0644]

diff --git a/parseHtml.py b/parseHtml.py
new file mode 100644 (file)
index 0000000..aab72b5
--- /dev/null
@@ -0,0 +1,63 @@
+from BeautifulSoup import BeautifulSoup
+import urllib2
+from datetime import time, datetime
+
+
+class Parser:
+    _overview = None
+    _details = None
+
+    def __init__(self, html):
+        self.soup = BeautifulSoup(html)
+
+    def __iter__(self):
+        for detail in self.details():
+            yield detail
+        raise IndexError()
+
+    def _parse_details(self):
+        return self.soup.findAll('div', {'class': 'data_table tourdetail'})
+
+    @property
+    def details(self):
+        if not self._details:
+            self._details = self._parse_details()
+
+        return self._details
+
+    def _parse_overview(self):
+        """
+        Returns dict containing
+        date: datetime
+        time: [time, time]
+        duration: time
+        change: int
+        price: float
+        """
+        # get overview table
+        table = self.soup.find('table', {'id': 'tbl_fahrten'})
+        # get rows
+        rows = table.findAll('tr')[1:]
+        overview = map(lambda x: {
+                       'date': datetime.strptime(x.find('td', {'class': 'col_date'}).text, '%d.%m.%Y'), # grab date
+                       'time': map(lambda x: time(*map(lambda x: int(x), x.split(':'))), x.find('td', {'class': 'col_time'}).text.split('  - ')), # extract times
+                       'duration': time(*map(lambda x: int(x), x.find('td', {'class': 'col_duration'}).text.split(':'))), # grab duration
+                       'change': int(x.find('td', {'class': 'col_change'}).text), # grab changes
+                       'price': float(x.find('td', {'class': 'col_price'}).text.replace(',', '.')) # grab price
+                       },
+            rows)
+
+        return overview
+
+    @property
+    def overview(self):
+        if not self._overview:
+            self._overview = self._parse_overview()
+
+        return self._overview
+
+
+class iTipParser:
+
+    def __init__(self):
+        raise NotImplementedError