-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscotty
executable file
·109 lines (89 loc) · 3.73 KB
/
scotty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
from gotovienna import argparse
from gotovienna.utils import *
from gotovienna.routing import *
parser = argparse.ArgumentParser(description='Get public transport route for Vienna')
parser.add_argument('-ot', metavar='type', type=str,
help='origin type: %s' % ' | '.join(POSITION_TYPES),
default=None, choices=POSITION_TYPES)
parser.add_argument('-dt', metavar='type', type=str,
help='destination type: %s' % ' | '.join(POSITION_TYPES),
default=None, choices=POSITION_TYPES)
parser.add_argument('origin', nargs='?',
help='origin station name')
parser.add_argument('destination', nargs='?',
help='destination station name')
args = parser.parse_args()
if not args.origin:
args.origin = raw_input('Origin: ')
if not args.destination:
args.destination = raw_input('Destination: ')
def do_search(args):
if isinstance(args.origin, unicode):
args.origin = args.origin.encode('utf-8', 'ignore')
elif isinstance(args.destination, unicode):
args.destination = args.destination.encode('utf-8', 'ignore')
result = search((args.origin, args.ot),
(args.destination, args.dt))
return sParser(result.read())
print >>sys.stderr, 'Searching...\n',
parser = do_search(args)
print >>sys.stderr, 'done.'
while parser.state == PageType.CORRECTION:
origin_corr, destination_corr = parser.get_correction()
if origin_corr:
print
print '* Origin ambiguous:'
lo = None
while not lo or not lo.isdigit() or int(lo) > len(origin_corr):
for idx, correction in enumerate(origin_corr):
print '%3d. %s' % (idx+1, correction)
lo = sys.stdin.readline().strip()
args.origin = origin_corr[int(lo) - 1]
if destination_corr:
print
print '* Destination ambiguous:'
ld = None
while not ld or not ld.isdigit() or int(ld) > len(destination_corr):
for idx, correction in enumerate(destination_corr):
print '%3d. %s' % (idx+1, correction)
ld = sys.stdin.readline().strip()
args.destination = destination_corr[int(ld) - 1]
parser = do_search(args)
if parser.state == PageType.RESULT:
parser = parser.get_result()
overviews = parser.overview
details = parser.details
l = ''
while not l == 'q':
for idx, overview in enumerate(overviews):
timespan = overview['timespan']
if not timespan:
# XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
continue
str_timespan = timespan[0].strftime('[%Y-%d-%m] %H:%M')
str_timespan += '-' + timespan[1].strftime('%H:%M')
timedelta = timespan[1] - timespan[0]
print '%d. %s (%s)' % (idx + 1,
str_timespan,
timedelta)
print 'q. Quit'
l = sys.stdin.readline().strip()
print
print '~' * 79
if l.isdigit() and int(l) <= len(details):
for detail in details[int(l) - 1]:
if detail['timespan'] and detail['station']:
time = '%s - %s' % (detail['timespan'][0].strftime(TIMEFORMAT),
detail['timespan'][1].strftime(TIMEFORMAT))
print '[%s] %s\n%s' % (time,
' -> '.join(detail['station']),
'\n'.join(detail['info']))
else:
print '\n'.join(detail['info'])
print '-' * 79
print
else:
print 'Error - unknown page returned.'