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 origin, origin_place = split_station(args.origin)
39 destination, destination_place = split_station(args.destination)
43 if cor.has_key('origin'):
45 print '* Origin ambiguous:'
47 while not l or not l.isdigit() or int(l) > len(cor['origin']):
49 for c in cor['origin']:
50 print '%d. %s' % (i, c)
52 l = sys.stdin.readline().strip()
54 origin = cor['origin'][int(l) - 1]
56 if cor.has_key('destination'):
58 print '* Destination ambiguous:'
60 while not l or not l.isdigit() or int(l) > len(cor['destination']):
62 for c in cor['destination']:
63 print '%d. %s' % (i, c)
65 l = sys.stdin.readline().strip()
67 destination = cor['destination'][int(l) - 1]
69 if cor.has_key('origin_place'):
71 print '* Origin place ambiguous:'
73 while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
75 for c in cor['origin_place']:
76 print '%d. %s' % (i, c)
78 l = sys.stdin.readline().strip()
80 origin_place = cor['origin_place'][int(l) - 1]
82 if cor.has_key('destination_place'):
84 print '* Destination place ambiguous:'
86 while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
88 for c in cor['destination_place']:
89 print '%d. %s' % (i, c)
91 l = sys.stdin.readline().strip()
93 destination_place = cor['destination_place'][int(l) - 1]
95 args.origin = '%s, %s' % (origin, origin_place)
96 args.destination = '%s, %s' %(destination, destination_place)
99 print 'PANIC at correction page'
102 elif state == PageType.RESULT:
103 parser = rParser(html)
105 overviews = parser.overview
106 details = parser.details
109 for idx, overview in enumerate(overviews):
110 timespan = overview['timespan']
112 # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
115 str_timespan = timespan[0].strftime('[%y-%d-%m] %H:%M')
116 str_timespan += '-' + timespan[1].strftime('%H:%M')
117 timedelta = timespan[1] - timespan[0]
118 print '%d. %s (%s)' % (idx + 1,
122 l = sys.stdin.readline().strip()
126 if l.isdigit() and int(l) <= len(details):
127 for detail in details[int(l) - 1]:
128 if detail['timespan'] and detail['station']:
129 time = '%s - %s' % (detail['timespan'][0].strftime(TIMEFORMAT), detail['timespan'][1].strftime(TIMEFORMAT))
130 print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
132 print '\n'.join(detail['info'])
141 elif state == PageType.UNKNOWN:
142 print 'PANIC unknown result'