routing for other cities; correction unstable (eg scotty "Rathaus, Linz"
[pywienerlinien] / scotty
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3
4 from BeautifulSoup import BeautifulSoup, NavigableString
5 from urllib2 import urlopen
6 from urllib import urlencode
7 import settings
8 from datetime import datetime, time
9 from textwrap import wrap
10 import argparse
11 import sys
12 import os.path
13
14 from gotovienna.routing import *
15
16 parser = argparse.ArgumentParser(description='Get public transport route for Vienna')
17 parser.add_argument('-ot', metavar='type', type=str, help='origin type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES)
18 parser.add_argument('-dt', metavar='type', type=str, help='destination type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES)
19 parser.add_argument('origin')
20 parser.add_argument('destination')
21
22 args = parser.parse_args()
23
24 finished = False
25 while not finished:
26
27     html = search((args.origin, args.ot), (args.destination, args.dt)).read()
28     
29     parser = sParser(html)
30     state = parser.check_page()
31
32     if state == PageType.CORRECTION:
33         try:
34             cor = parser.get_correction()
35             print "A", args.origin, args.destination
36             origin, origin_place = split_station(args.origin)
37             destination, destination_place = split_station(args.destination)
38             
39             print "B", origin, origin_place, destination, destination_place
40             
41             # FIXME refactoring
42             
43             if cor.has_key('origin'):
44                 print
45                 print '* Origin ambiguous:'
46                 l = None
47                 while not l or not l.isdigit() or int(l) > len(cor['origin']):
48                     i = 1
49                     for c in cor['origin']:
50                         print '%d. %s' % (i, c)
51                         i += 1
52                     l = sys.stdin.readline().strip()
53     
54                 origin = cor['origin'][int(l) - 1]
55     
56             if cor.has_key('destination'):
57                 print
58                 print '* Destination ambiguous:'
59                 l = None
60                 while not l or not l.isdigit() or int(l) > len(cor['destination']):
61                     i = 1
62                     for c in cor['destination']:
63                         print '%d. %s' % (i, c)
64                         i += 1
65                     l = sys.stdin.readline().strip()
66     
67                 destination = cor['destination'][int(l) - 1]
68                 
69             if cor.has_key('origin_place'):
70                 print
71                 print '* Origin place ambiguous:'
72                 l = None
73                 while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
74                     i = 1
75                     for c in cor['origin_place']:
76                         print '%d. %s' % (i, c)
77                         i += 1
78                     l = sys.stdin.readline().strip()
79     
80                 origin_place = cor['origin_place'][int(l) - 1]
81     
82             if cor.has_key('destination_place'):
83                 print
84                 print '* Destination place ambiguous:'
85                 l = None
86                 while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
87                     i = 1
88                     for c in cor['destination_place']:
89                         print '%d. %s' % (i, c)
90                         i += 1
91                     l = sys.stdin.readline().strip()
92     
93                 destination_place = cor['destination_place'][int(l) - 1]
94                 
95             print origin, origin_place, destination, destination_place
96             args.origin = '%s, %s' % (origin, origin_place)
97             args.destination = '%s, %s' %(destination, destination_place)
98             
99         except ParserError:
100             print 'PANIC at correction page'
101             finished = True
102     
103     elif state == PageType.RESULT:
104         parser = rParser(html)
105         try:
106             overviews = parser.overview
107             details = parser.details
108             l = ''
109             while not l == 'q':
110                 for idx, overview in enumerate(overviews):
111                     if not overview['date'] or not overview['time']:
112                         # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
113                         continue
114     
115                     print '%d. [%s] %s-%s (%s)' % (idx + 1,
116                             overview['date'],
117                             overview['time'][0],
118                             overview['time'][1],
119                             overview['duration'])
120                 print 'q. Quit'
121                 l = sys.stdin.readline().strip()
122                 print
123                 print '~' * 80
124     
125                 if l.isdigit() and int(l) <= len(details):
126                     for detail in details[int(l) - 1]:
127                         if detail['time'] and detail['station']:
128                             time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
129                             print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
130                         else:
131                             print '\n'.join(detail['info'])
132                         print '-' * 80
133                 print
134         
135             finished = True
136         
137         except ParserError:
138             print 'parsererror'
139     
140     elif state == PageType.UNKNOWN:
141         print 'PANIC unknown result'