moved iParser to own file
authorFlorian Schweikert <kelvan@logic.at>
Fri, 2 Sep 2011 19:07:12 +0000 (21:07 +0200)
committerFlorian Schweikert <kelvan@logic.at>
Fri, 2 Sep 2011 19:07:12 +0000 (21:07 +0200)
finished get lines method
maybe fixed parseCorrection

iTip.py [new file with mode: 0644]
parseCorrection.py [new file with mode: 0644]

diff --git a/iTip.py b/iTip.py
new file mode 100644 (file)
index 0000000..1b82e83
--- /dev/null
+++ b/iTip.py
@@ -0,0 +1,33 @@
+from BeautifulSoup import BeautifulSoup
+import urllib2
+import settings
+
+class iParser:
+
+    def __init__(self):
+        self._stations = {}
+        self._lines = []
+
+    def get_stations(self, letter):
+        if not self._stations.has_key(letter):
+            bs = BeautifulSoup(urllib2.urlopen(settings.stations % letter))
+            self._stations[letter] = map(lambda x: x['value'], bs.find('select', {'id': 'letter'}).findAll('option'))
+
+        return self._stations[letter]
+
+    def get_lines(self):
+        if not self._lines:
+            bs = BeautifulSoup(urllib2.urlopen(settings.line_overview))
+            # get tables
+            lines = bs.findAll('td', {'class': 'linie'})
+            self._lines = []
+            
+            for line in lines:
+                if line.a:
+                    print line.text
+                    if line.text:
+                        self._lines.append((line.text, line.a['href']))
+                    elif line.img:
+                        self._lines.append((line.img['alt'], line.a['href']))
+                        
+        return self._lines
\ No newline at end of file
diff --git a/parseCorrection.py b/parseCorrection.py
new file mode 100644 (file)
index 0000000..be37f5d
--- /dev/null
@@ -0,0 +1,31 @@
+from BeautifulSoup import BeautifulSoup
+
+class ParserError(Exception):
+     def __init__(self, value='', code=0):
+         self.value = value
+         self.code = code
+
+     def __str__(self):
+         return repr(self.value)
+
+class Parser:
+
+    def __init__(self, html):
+        self.soup = BeautifulSoup(html)
+        
+    def find_options(self):
+        nlo = self.soup.find('select', {'id': 'nameList_origin'})
+        nld = self.soup.find('select', {'id': 'nameList_destination'})
+        
+        if not nlo or not nld:
+            raise ParserError('Unable to parse html')
+        
+        origin = nlo.findAll('option')
+        destination = nld.findAll('option')
+        
+        if not origin:
+            origin = []
+        if not destination:
+            destination = []
+        
+        return (origin, destination)
\ No newline at end of file