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