routing for other cities; correction unstable (eg scotty "Rathaus, Linz"
authorFlorian Schweikert <kelvan@logic.at>
Wed, 28 Sep 2011 21:08:39 +0000 (23:08 +0200)
committerFlorian Schweikert <kelvan@logic.at>
Wed, 28 Sep 2011 21:08:39 +0000 (23:08 +0200)
"Karlsplatz, Graz")

gotovienna/routing.py
scotty
ui/meego.qml [new file with mode: 0644]

index bb6cf96..a89e693 100644 (file)
@@ -24,6 +24,41 @@ class PageType:
     UNKNOWN, CORRECTION, RESULT = range(3)
 
 
+def extract_city(station):
+    """ Extract city from string if present,
+    else return default city
+    
+    >>> extract_city('Karlsplatz, Wien')
+    'Wien'
+    """
+    if len(station.split(',')) > 1:
+        return station.split(',')[-1].strip()
+    else:
+        return 'Wien'
+        
+def extract_station(station):
+    """ Remove city from string
+    
+    >>> extract_station('Karlsplatz, Wien')
+    'Karlsplatz'
+    """
+    if len(station.split(',')) > 1:
+        return station[:station.rindex(',')].strip()
+    else:
+        return station
+    
+def split_station(station):
+    """ >>> split_station('Karlsplatz, Wien')
+    ('Karlsplatz', 'Wien')
+    >>> split_station('Karlsplatz')
+    ('Karlsplatz', 'Wien')
+    """
+    print "split:", station
+    if len(station.split(',')) > 1:
+        return (station[:station.rindex(',')].strip(), station.split(',')[-1].strip())
+    else:
+        return (station, 'Wien')
+
 def search(origin_tuple, destination_tuple, dtime=None):
     """ build route request
     returns html result (as urllib response)
@@ -32,7 +67,12 @@ def search(origin_tuple, destination_tuple, dtime=None):
         dtime = datetime.now()
 
     origin, origin_type = origin_tuple
+    origin, origin_city = split_station(origin)
+    
     destination, destination_type = destination_tuple
+    destination, destination_city = split_station(destination)
+
+
     if not origin_type in POSITION_TYPES or\
         not destination_type in POSITION_TYPES:
         raise ParserError('Invalid position type')
@@ -44,6 +84,8 @@ def search(origin_tuple, destination_tuple, dtime=None):
     post['type_destination'] = destination_type
     post['itdDateDayMonthYear'] = dtime.strftime('%d.%m.%Y')
     post['itdTime'] = dtime.strftime('%H:%M')
+    post['place_origin'] = origin_city
+    post['place_destination'] = destination_city
     params = urlencode(post)
     url = '%s?%s' % (settings.action, params)
 
@@ -74,22 +116,29 @@ class sParser:
         return PageType.UNKNOWN
 
     def get_correction(self):
-        nlo = self.soup.find('select', {'id': 'nameList_origin'})
-        nld = self.soup.find('select', {'id': 'nameList_destination'})
-
-        if not nlo and not nld:
-            raise ParserError('Unable to parse html')
-
-        if nlo:
-            origin = map(lambda x: x.text, nlo.findAll('option'))
+        names_origin = self.soup.find('select', {'id': 'nameList_origin'})
+        names_destination = self.soup.find('select', {'id': 'nameList_destination'})
+        places_origin = self.soup.find('select', {'id': 'placeList_origin'})
+        places_destination = self.soup.find('select', {'id': 'placeList_destination'})
+        
+
+        if names_origin or names_destination or places_origin or places_destination:
+            dict = {}
+            
+            if names_origin:
+                dict['origin'] = map(lambda x: x.text, names_origin.findAll('option'))
+            if names_destination:
+                dict['destination'] = map(lambda x: x.text, names_destination.findAll('option'))
+                
+            if places_origin:
+                dict['place_origin'] = map(lambda x: x.text, names_origin.findAll('option'))
+            if names_destination:
+                dict['place_destination'] = map(lambda x: x.text, names_destination.findAll('option'))
+    
+            return dict
+        
         else:
-            origin = []
-        if nld:
-            destination = map(lambda x: x.text, nld.findAll('option'))
-        else:
-            destination = []
-
-        return (origin, destination)
+            raise ParserError('Unable to parse html')
 
     def get_result(self):
         return rParser(str(self.soup))
diff --git a/scotty b/scotty
index 4f88de1..8375815 100755 (executable)
--- a/scotty
+++ b/scotty
@@ -20,82 +20,122 @@ parser.add_argument('origin')
 parser.add_argument('destination')
 
 args = parser.parse_args()
-html = search((args.origin, args.ot), (args.destination, args.dt)).read()
 
-parser = sParser(html)
-state = parser.check_page()
-
-if state == PageType.CORRECTION:
-    try:
-        cor = parser.get_correction()
-        if cor[0]:
-            print
-            print '* Origin ambiguous:'
-            lo = None
-            while not lo or not lo.isdigit() or int(lo) > len(cor[0]):
-                i = 1
-                for c in cor[0]:
-                    print '%d. %s' % (i, c)
-                    i += 1
-                lo = sys.stdin.readline().strip()
-
-            args.origin = cor[0][int(lo) - 1]
-
-        if cor[1]:
-            print
-            print '* Destination ambiguous:'
-            ld = None
-            while not ld or not ld.isdigit() or int(ld) > len(cor[1]):
-                j = 1
-                for c in cor[1]:
-                    print '%d. %s' % (j, c)
-                    j += 1
-                ld = sys.stdin.readline().strip()
-
-            args.destination = cor[1][int(ld) - 1]
-
-        html = search((args.origin.encode('UTF-8'), args.ot), (args.destination.encode('UTF-8'), args.dt)).read()
-
-        parser = sParser(html)
-        state = parser.check_page()
-
-    except ParserError:
-        print 'PANIC at correction page'
-
-if state == PageType.RESULT:
-    parser = rParser(html)
-    try:
-        overviews = parser.overview
-        details = parser.details
-        l = ''
-        while not l == 'q':
-            for idx, overview in enumerate(overviews):
-                if not overview['date'] or not overview['time']:
-                    # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
-                    continue
-
-                print '%d. [%s] %s-%s (%s)' % (idx + 1,
-                        overview['date'],
-                        overview['time'][0],
-                        overview['time'][1],
-                        overview['duration'])
-            print 'q. Quit'
-            l = sys.stdin.readline().strip()
-            print
-            print '~' * 100
-
-            if l.isdigit() and int(l) <= len(details):
-                for detail in details[int(l) - 1]:
-                    if detail['time'] and detail['station']:
-                        time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
-                        print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
-                    else:
-                        print '\n'.join(detail['info'])
-                    print '-' * 100
-            print
-
-    except ParserError:
-        print 'parsererror'
-
-elif state == PageType.UNKNOWN:
-    print 'PANIC unknown result'
+finished = False
+while not finished:
+
+    html = search((args.origin, args.ot), (args.destination, args.dt)).read()
+    
+    parser = sParser(html)
+    state = parser.check_page()
+
+    if state == PageType.CORRECTION:
+        try:
+            cor = parser.get_correction()
+            print "A", args.origin, args.destination
+            origin, origin_place = split_station(args.origin)
+            destination, destination_place = split_station(args.destination)
+            
+            print "B", origin, origin_place, destination, destination_place
+            
+            # FIXME refactoring
+            
+            if cor.has_key('origin'):
+                print
+                print '* Origin ambiguous:'
+                l = None
+                while not l or not l.isdigit() or int(l) > len(cor['origin']):
+                    i = 1
+                    for c in cor['origin']:
+                        print '%d. %s' % (i, c)
+                        i += 1
+                    l = sys.stdin.readline().strip()
+    
+                origin = cor['origin'][int(l) - 1]
+    
+            if cor.has_key('destination'):
+                print
+                print '* Destination ambiguous:'
+                l = None
+                while not l or not l.isdigit() or int(l) > len(cor['destination']):
+                    i = 1
+                    for c in cor['destination']:
+                        print '%d. %s' % (i, c)
+                        i += 1
+                    l = sys.stdin.readline().strip()
+    
+                destination = cor['destination'][int(l) - 1]
+                
+            if cor.has_key('origin_place'):
+                print
+                print '* Origin place ambiguous:'
+                l = None
+                while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
+                    i = 1
+                    for c in cor['origin_place']:
+                        print '%d. %s' % (i, c)
+                        i += 1
+                    l = sys.stdin.readline().strip()
+    
+                origin_place = cor['origin_place'][int(l) - 1]
+    
+            if cor.has_key('destination_place'):
+                print
+                print '* Destination place ambiguous:'
+                l = None
+                while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
+                    i = 1
+                    for c in cor['destination_place']:
+                        print '%d. %s' % (i, c)
+                        i += 1
+                    l = sys.stdin.readline().strip()
+    
+                destination_place = cor['destination_place'][int(l) - 1]
+                
+            print origin, origin_place, destination, destination_place
+            args.origin = '%s, %s' % (origin, origin_place)
+            args.destination = '%s, %s' %(destination, destination_place)
+            
+        except ParserError:
+            print 'PANIC at correction page'
+            finished = True
+    
+    elif state == PageType.RESULT:
+        parser = rParser(html)
+        try:
+            overviews = parser.overview
+            details = parser.details
+            l = ''
+            while not l == 'q':
+                for idx, overview in enumerate(overviews):
+                    if not overview['date'] or not overview['time']:
+                        # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
+                        continue
+    
+                    print '%d. [%s] %s-%s (%s)' % (idx + 1,
+                            overview['date'],
+                            overview['time'][0],
+                            overview['time'][1],
+                            overview['duration'])
+                print 'q. Quit'
+                l = sys.stdin.readline().strip()
+                print
+                print '~' * 80
+    
+                if l.isdigit() and int(l) <= len(details):
+                    for detail in details[int(l) - 1]:
+                        if detail['time'] and detail['station']:
+                            time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
+                            print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
+                        else:
+                            print '\n'.join(detail['info'])
+                        print '-' * 80
+                print
+        
+            finished = True
+        
+        except ParserError:
+            print 'parsererror'
+    
+    elif state == PageType.UNKNOWN:
+        print 'PANIC unknown result'
diff --git a/ui/meego.qml b/ui/meego.qml
new file mode 100644 (file)
index 0000000..bcba25e
--- /dev/null
@@ -0,0 +1,6 @@
+import QtQuick 1.0
+
+Rectangle {
+    width: 480
+    height: 854
+}