Restructuring; removing ui files
[pywienerlinien] / scotty
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3
4 import argparse
5 import sys
6
7 from gotovienna.routing import *
8
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='?')
14
15 args = parser.parse_args()
16
17 if not args.origin:
18     args.origin = raw_input('Origin: ')
19
20 if not args.destination:
21     args.destination = raw_input('Destination: ')
22
23 print >>sys.stderr, 'Searching...',
24 html = search((args.origin, args.ot), (args.destination, args.dt)).read()
25 print >>sys.stderr, 'done.'
26
27 parser = sParser(html)
28 state = parser.check_page()
29
30 if state == PageType.CORRECTION:
31     try:
32         cor = parser.get_correction()
33         if cor[0]:
34             print
35             print '* Origin ambiguous:'
36             lo = None
37             while not lo or not lo.isdigit() or int(lo) > len(cor[0]):
38                 i = 1
39                 for c in cor[0]:
40                     print '%d. %s' % (i, c)
41                     i += 1
42                 lo = sys.stdin.readline().strip()
43
44             args.origin = cor[0][int(lo) - 1]
45
46         if cor[1]:
47             print
48             print '* Destination ambiguous:'
49             ld = None
50             while not ld or not ld.isdigit() or int(ld) > len(cor[1]):
51                 j = 1
52                 for c in cor[1]:
53                     print '%d. %s' % (j, c)
54                     j += 1
55                 ld = sys.stdin.readline().strip()
56
57             args.destination = cor[1][int(ld) - 1]
58
59         html = search((args.origin.encode('UTF-8'), args.ot), (args.destination.encode('UTF-8'), args.dt)).read()
60
61         parser = sParser(html)
62         state = parser.check_page()
63
64     except ParserError:
65         print 'PANIC at correction page'
66
67 if state == PageType.RESULT:
68     parser = rParser(html)
69     try:
70         overviews = parser.overview
71         details = parser.details
72         l = ''
73         while not l == 'q':
74             for idx, overview in enumerate(overviews):
75                 if not overview['date'] or not overview['time']:
76                     # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
77                     continue
78
79                 print '%d. [%s] %s-%s (%s)' % (idx + 1,
80                         overview['date'],
81                         overview['time'][0],
82                         overview['time'][1],
83                         overview['duration'])
84             print 'q. Quit'
85             l = sys.stdin.readline().strip()
86             print
87             print '~' * 100
88
89             if l.isdigit() and int(l) <= len(details):
90                 for detail in details[int(l) - 1]:
91                     if detail['time'] and detail['station']:
92                         time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
93                         print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
94                     else:
95                         print '\n'.join(detail['info'])
96                     print '-' * 100
97             print
98
99     except ParserError:
100         print 'parsererror'
101
102 elif state == PageType.UNKNOWN:
103     print 'PANIC unknown result'