X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=scotty;h=ca7922cbdd66f422f3fc27ef0daf0ee394dc17f9;hb=HEAD;hp=30a701f48195e4f30b3df279bac159fc3b3f729e;hpb=d88697dfaf9f6b881f488f6f40762d6e5e08cbae;p=pywienerlinien diff --git a/scotty b/scotty index 30a701f..ca7922c 100755 --- a/scotty +++ b/scotty @@ -1,23 +1,23 @@ #!/usr/bin/env python # -*- coding: UTF-8 -*- -from BeautifulSoup import BeautifulSoup, NavigableString -from urllib2 import urlopen -from urllib import urlencode -import settings -from datetime import datetime, time -from textwrap import wrap -import argparse import sys -import os.path +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='stop', choices=POSITION_TYPES) -parser.add_argument('-dt', metavar='type', type=str, help='destination type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES) -parser.add_argument('origin', nargs='?') -parser.add_argument('destination', nargs='?') +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() @@ -27,84 +27,83 @@ if not args.origin: if not args.destination: args.destination = raw_input('Destination: ') -print >>sys.stderr, 'Searching...', -html = search((args.origin, args.ot), (args.destination, args.dt)).read() +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.' -parser = sParser(html) -state = parser.check_page() - -if state == PageType.CORRECTION: - try: - cor = parser.get_correction() - if cor[0]: - print - print '* Origin ambiguous:' - lo = None - while not lo or not lo.isdigit() or int(lo) > len(cor[0]): - i = 1 - for c in cor[0]: - print '%d. %s' % (i, c) - i += 1 - lo = sys.stdin.readline().strip() - - args.origin = cor[0][int(lo) - 1] - - if cor[1]: - print - print '* Destination ambiguous:' - ld = None - while not ld or not ld.isdigit() or int(ld) > len(cor[1]): - j = 1 - for c in cor[1]: - print '%d. %s' % (j, c) - j += 1 - ld = sys.stdin.readline().strip() - - args.destination = cor[1][int(ld) - 1] - - html = search((args.origin.encode('UTF-8'), args.ot), (args.destination.encode('UTF-8'), args.dt)).read() - - parser = sParser(html) - state = parser.check_page() - - except ParserError: - print 'PANIC at correction page' - -if state == PageType.RESULT: - parser = rParser(html) - try: - overviews = parser.overview - details = parser.details - l = '' - while not l == 'q': - for idx, overview in enumerate(overviews): - if not overview['date'] or not overview['time']: - # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?! - continue - - print '%d. [%s] %s-%s (%s)' % (idx + 1, - overview['date'], - overview['time'][0], - overview['time'][1], - overview['duration']) - print 'q. Quit' - l = sys.stdin.readline().strip() - print - print '~' * 100 - - if l.isdigit() and int(l) <= len(details): - for detail in details[int(l) - 1]: - if detail['time'] and detail['station']: - time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT)) - print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info'])) - else: - print '\n'.join(detail['info']) - print '-' * 100 - print - - except ParserError: - print 'parsererror' - -elif state == PageType.UNKNOWN: - print 'PANIC unknown result' +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.'