2 # -*- coding: UTF-8 -*-
4 from BeautifulSoup import BeautifulSoup, NavigableString
5 from urllib2 import urlopen
6 from urllib import urlencode
8 from datetime import datetime, time
9 from textwrap import wrap
14 from gotovienna.routing import *
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')
22 args = parser.parse_args()
27 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
29 parser = sParser(html)
30 state = parser.check_page()
32 if state == PageType.CORRECTION:
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)
39 print "B", origin, origin_place, destination, destination_place
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 print origin, origin_place, destination, destination_place
96 args.origin = '%s, %s' % (origin, origin_place)
97 args.destination = '%s, %s' %(destination, destination_place)
100 print 'PANIC at correction page'
103 elif state == PageType.RESULT:
104 parser = rParser(html)
106 overviews = parser.overview
107 details = parser.details
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?!
115 print '%d. [%s] %s-%s (%s)' % (idx + 1,
119 overview['duration'])
121 l = sys.stdin.readline().strip()
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']))
131 print '\n'.join(detail['info'])
140 elif state == PageType.UNKNOWN:
141 print 'PANIC unknown result'