2 # -*- coding: UTF-8 -*-
7 from gotovienna.routing import *
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='?')
15 args = parser.parse_args()
18 args.origin = raw_input('Origin: ')
20 if not args.destination:
21 args.destination = raw_input('Destination: ')
23 print >>sys.stderr, 'Searching...',
24 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
25 print >>sys.stderr, 'done.'
30 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
32 parser = sParser(html)
33 state = parser.check_page()
35 if state == PageType.CORRECTION:
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)
42 print "B", origin, origin_place, destination, destination_place
46 if cor.has_key('origin'):
48 print '* Origin ambiguous:'
50 while not l or not l.isdigit() or int(l) > len(cor['origin']):
52 for c in cor['origin']:
53 print '%d. %s' % (i, c)
55 l = sys.stdin.readline().strip()
57 origin = cor['origin'][int(l) - 1]
59 if cor.has_key('destination'):
61 print '* Destination ambiguous:'
63 while not l or not l.isdigit() or int(l) > len(cor['destination']):
65 for c in cor['destination']:
66 print '%d. %s' % (i, c)
68 l = sys.stdin.readline().strip()
70 destination = cor['destination'][int(l) - 1]
72 if cor.has_key('origin_place'):
74 print '* Origin place ambiguous:'
76 while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
78 for c in cor['origin_place']:
79 print '%d. %s' % (i, c)
81 l = sys.stdin.readline().strip()
83 origin_place = cor['origin_place'][int(l) - 1]
85 if cor.has_key('destination_place'):
87 print '* Destination place ambiguous:'
89 while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
91 for c in cor['destination_place']:
92 print '%d. %s' % (i, c)
94 l = sys.stdin.readline().strip()
96 destination_place = cor['destination_place'][int(l) - 1]
98 print origin, origin_place, destination, destination_place
99 args.origin = '%s, %s' % (origin, origin_place)
100 args.destination = '%s, %s' %(destination, destination_place)
103 print 'PANIC at correction page'
106 elif state == PageType.RESULT:
107 parser = rParser(html)
109 overviews = parser.overview
110 details = parser.details
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?!
118 print '%d. [%s] %s-%s (%s)' % (idx + 1,
122 overview['duration'])
124 l = sys.stdin.readline().strip()
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']))
134 print '\n'.join(detail['info'])
143 elif state == PageType.UNKNOWN:
144 print 'PANIC unknown result'