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