[FIX] AttributeError if station without link
[pywienerlinien] / iTip.py
diff --git a/iTip.py b/iTip.py
index cbae266..4b63f61 100644 (file)
--- a/iTip.py
+++ b/iTip.py
@@ -2,6 +2,8 @@ from BeautifulSoup import BeautifulSoup
 from urllib2 import urlopen
 import settings
 from datetime import time
+import argparse
+import re
 
 class iParser:
 
@@ -21,11 +23,11 @@ class iParser:
             
             bs = BeautifulSoup(urlopen(self.lines[name]))
             tables = bs.findAll('table', {'class': 'text_10pix'})
-            for i in range (2):
+            for i in range(2):
                 dir = tables[i].div.contents[-1].strip(' ')
                 
                 sta = []
-                for tr in tables[0].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
+                for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
                     if tr.a:
                         sta.append((tr.a.text, settings.line_overview + tr.a['href']))
                     else:
@@ -63,6 +65,10 @@ class iParser:
         
         #TODO parse line name and direction for station site parsing
         
+        if not url:
+            # FIXME prevent from calling this method with None
+            return []
+
         bs = BeautifulSoup(urlopen(url))
         result_lines = bs.findAll('table')[-1].findAll('tr')
         
@@ -82,7 +88,9 @@ class iParser:
             
             time = time[1]
             
-            if time.isdigit():
+            if time.find('rze...') >= 0:
+                    dep.append(0)
+            elif time.isdigit():
                 # if time to next departure in cell convert to int
                 dep.append(int(time))
             else:
@@ -96,4 +104,55 @@ class iParser:
                     #TODO replace with logger
                     print "[DEBUG] Invalid data:\n%s" % time
                 
-        return dep
\ No newline at end of file
+        return dep
+    
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
+    parser.add_argument('-l', metavar='name', type=str, help='line name')
+    parser.add_argument('-s', metavar='name', type=str, help='station name')   
+
+    args = parser.parse_args()
+    
+    itip = iParser()
+    lines = itip.lines
+    if args.l:
+        l = args.l.upper()
+    else:
+        l = None
+    if args.s:
+        s = args.s.decode('UTF-8')
+    else:
+        s = ''
+    
+    if l and l in lines:
+        stations = itip.get_stations(l)
+        for key in stations.keys():
+            if not s:
+                print '* %s:' % key
+            for station in stations[key]:
+                if s:
+                    if s.startswith(station[0]) or station[0].startswith(s):
+                        # FIXME
+                        print '* %s\n  %s .....' % (key, station[0]), itip.get_departures(station[1])
+                else:
+                    print '    %s' % station[0]
+    
+    elif not l:
+        line = {'U-Bahn': '|', 'Strassenbahn': '|', 'Bus': '|', 'Andere': '|', 'Nightline': '|'}
+        lines_sorted = lines.keys()
+        lines_sorted.sort()
+        for li in lines_sorted:
+            if li.isdigit():
+                type = 'Strassenbahn'
+            elif li.endswith('A') or li.endswith('B') and li[1].isdigit():
+                type = 'Bus'
+            elif li.startswith('U'):
+                type = 'U-Bahn'
+            elif li.startswith('N'):
+                type = 'Nightline'
+            else:
+                type = 'Andere'
+                
+            line[type] += ' %s |' % li
+        for kv in line.items():
+            print "%s:\n%s" % kv