2 # -*- coding: UTF-8 -*-
7 from gotovienna.utils import *
8 from gotovienna.routing import *
10 parser = argparse.ArgumentParser(description='Get public transport route for Vienna')
11 parser.add_argument('-ot', metavar='type', type=str, help='origin type: %s' % ' | '.join(POSITION_TYPES), default=None, choices=POSITION_TYPES)
12 parser.add_argument('-dt', metavar='type', type=str, help='destination type: %s' % ' | '.join(POSITION_TYPES), default=None, choices=POSITION_TYPES)
13 parser.add_argument('origin', nargs='?', help='origin station name')
14 parser.add_argument('destination', nargs='?', help='destination station name')
16 args = parser.parse_args()
19 args.origin = raw_input('Origin: ')
21 if not args.destination:
22 args.destination = raw_input('Destination: ')
25 if isinstance(args.origin, unicode):
26 args.origin = args.origin.encode('utf-8', 'ignore')
27 elif isinstance(args.destination, unicode):
28 args.destination = args.destination.encode('utf-8', 'ignore')
30 result = search((args.origin, args.ot),
31 (args.destination, args.dt))
33 return sParser(result.read())
35 print >>sys.stderr, 'Searching...\n',
36 parser = do_search(args)
37 print >>sys.stderr, 'done.'
42 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
44 parser = sParser(html)
45 state = parser.check_page()
47 if state == PageType.CORRECTION:
49 cor = parser.get_correction()
50 origin, origin_place = split_station(args.origin)
51 destination, destination_place = split_station(args.destination)
55 if cor.has_key('origin'):
57 print '* Origin ambiguous:'
59 while not l or not l.isdigit() or int(l) > len(cor['origin']):
61 for c in cor['origin']:
62 print '%d. %s' % (i, c)
64 l = sys.stdin.readline().strip()
66 origin = cor['origin'][int(l) - 1]
68 if cor.has_key('destination'):
70 print '* Destination ambiguous:'
72 while not l or not l.isdigit() or int(l) > len(cor['destination']):
74 for c in cor['destination']:
75 print '%d. %s' % (i, c)
77 l = sys.stdin.readline().strip()
79 destination = cor['destination'][int(l) - 1]
81 if cor.has_key('origin_place'):
83 print '* Origin place ambiguous:'
85 while not l or not l.isdigit() or int(l) > len(cor['origin_place']):
87 for c in cor['origin_place']:
88 print '%d. %s' % (i, c)
90 l = sys.stdin.readline().strip()
92 origin_place = cor['origin_place'][int(l) - 1]
94 if cor.has_key('destination_place'):
96 print '* Destination place ambiguous:'
98 while not l or not l.isdigit() or int(l) > len(cor['destination_place']):
100 for c in cor['destination_place']:
101 print '%d. %s' % (i, c)
103 l = sys.stdin.readline().strip()
105 destination_place = cor['destination_place'][int(l) - 1]
107 args.origin = '%s, %s' % (origin, origin_place)
108 args.destination = '%s, %s' %(destination, destination_place)
111 print 'PANIC at correction page'
114 elif state == PageType.RESULT:
115 parser = rParser(html)
117 overviews = parser.overview
118 details = parser.details
121 for idx, overview in enumerate(overviews):
122 timespan = overview['timespan']
124 # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
127 str_timespan = timespan[0].strftime('[%y-%d-%m] %H:%M')
128 str_timespan += '-' + timespan[1].strftime('%H:%M')
129 timedelta = timespan[1] - timespan[0]
130 print '%d. %s (%s)' % (idx + 1,
134 l = sys.stdin.readline().strip()
138 if l.isdigit() and int(l) <= len(details):
139 for detail in details[int(l) - 1]:
140 if detail['timespan'] and detail['station']:
141 time = '%s - %s' % (detail['timespan'][0].strftime(TIMEFORMAT), detail['timespan'][1].strftime(TIMEFORMAT))
142 print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
144 print '\n'.join(detail['info'])
153 elif state == PageType.UNKNOWN:
154 print 'PANIC unknown result'
156 while parser.state == PageType.CORRECTION:
157 origin_corr, destination_corr = parser.get_correction()
161 print '* Origin ambiguous:'
163 while not lo or not lo.isdigit() or int(lo) > len(origin_corr):
164 for idx, correction in enumerate(origin_corr):
165 print '%3d. %s' % (idx+1, correction)
166 lo = sys.stdin.readline().strip()
168 args.origin = origin_corr[int(lo) - 1]
172 print '* Destination ambiguous:'
174 while not ld or not ld.isdigit() or int(ld) > len(destination_corr):
175 for idx, correction in enumerate(destination_corr):
176 print '%3d. %s' % (idx+1, correction)
177 ld = sys.stdin.readline().strip()
179 args.destination = destination_corr[int(ld) - 1]
181 parser = do_search(args)
183 if parser.state == PageType.RESULT:
184 parser = parser.get_result()
185 overviews = parser.overview
186 details = parser.details
189 for idx, overview in enumerate(overviews):
190 if not overview['date'] or not overview['time']:
191 # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
194 print '%d. [%s] %s-%s (%s)' % (idx + 1,
198 overview['duration'])
200 l = sys.stdin.readline().strip()
204 if l.isdigit() and int(l) <= len(details):
205 for detail in details[int(l) - 1]:
206 if detail['time'] and detail['station']:
207 time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
208 print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
210 print '\n'.join(detail['info'])
214 print 'Error - unknown page returned.'