Create "gotovienna" package; split CLI code
[pywienerlinien] / itip
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from BeautifulSoup import BeautifulSoup
5 from urllib2 import urlopen
6 import settings
7 from datetime import time
8 import argparse
9 import re
10
11 from gotovienna.realtime import ITipParser
12
13
14 parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
15 parser.add_argument('-l', metavar='name', type=str, help='line name')
16 parser.add_argument('-s', metavar='name', type=str, help='station name')   
17
18 args = parser.parse_args()
19
20 itip = ITipParser()
21 lines = itip.lines
22 if args.l:
23     l = args.l.upper()
24 else:
25     l = None
26 if args.s:
27     s = args.s.decode('UTF-8')
28 else:
29     s = ''
30
31 if l and l in lines:
32     stations = itip.get_stations(l)
33     for key in stations.keys():
34         if not s:
35             print '* %s:' % key
36         for station in stations[key]:
37             if s:
38                 if s.startswith(station[0]) or station[0].startswith(s):
39                     # FIXME
40                     print '* %s\n  %s .....' % (key, station[0]), itip.get_departures(station[1])
41             else:
42                 print '    %s' % station[0]
43
44 elif not l:
45     line = {'U-Bahn': '|', 'Strassenbahn': '|', 'Bus': '|', 'Andere': '|', 'Nightline': '|'}
46     lines_sorted = lines.keys()
47     lines_sorted.sort()
48     for li in lines_sorted:
49         if li.isdigit():
50             type = 'Strassenbahn'
51         elif li.endswith('A') or li.endswith('B') and li[1].isdigit():
52             type = 'Bus'
53         elif li.startswith('U'):
54             type = 'U-Bahn'
55         elif li.startswith('N'):
56             type = 'Nightline'
57         else:
58             type = 'Andere'
59             
60         line[type] += ' %s |' % li
61     for kv in line.items():
62         print "%s:\n%s" % kv