-
Notifications
You must be signed in to change notification settings - Fork 0
/
itip
executable file
·170 lines (139 loc) · 5.69 KB
/
itip
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gotovienna import argparse
from gotovienna.utils import *
from gotovienna.realtime import *
from schwarzkappler.provider_enabled import get_all_warnings
from datetime import date, time, datetime
parser = argparse.ArgumentParser(description='Get realtime public transport information for Vienna')
parser.add_argument('-d', metavar='type', help='destination station',
default=None, dest='dest_station')
parser.add_argument('line', nargs='?', help='line name (e.g. 59A)')
parser.add_argument('station', nargs='?', help='station name (e.g. Karlsplatz)')
args = parser.parse_args()
itip = ITipParser()
def show_schwarzkappler_warnings():
# Schwarzkappler alert ;)
warnings = get_all_warnings(args.line)
warns = []
st = []
for warning in warnings:
if warning.has_key('station'):
if warning['station'] in st:
# duplicated
continue
else:
st.append(warning['station'])
t = ''
report_date = warning['reportDate']
if type(report_date) == date:
if report_date == date.today():
t = 'today'
else:
# static warnings from other days aren't interesting
continue
elif type(report_date == datetime):
delta = now - report_date
m = delta.seconds/60
hours = m/60
minutes = m%60
if hours:
t = '%d hours and %d minutes ago' % (hours, minutes)
else:
t = '%d minutes ago' % minutes
if warning.has_key('station'):
warns.append('%s (%s)' % (warning['station'], t))
else:
warns.append('%s (%s)' % ('generic', t))
if warns:
print inred('Schwarzkappler alerts')
for warn in warns:
print ' ', warn
else:
print ingreen('No schwarzkappler warnings')
print
if args.dest_station:
station = args.dest_station.decode('utf-8')
print itip.get_departures_by_station(station)
else:
if args.line:
# Convert line name to uppercase (e.g. 'u4' -> 'U4')
args.line = args.line.upper()
if args.station:
args.station = args.station.decode('utf-8')
if args.line in itip.lines:
ITEM_WIDTH = 33
ITEM_SPACING = 4
now = datetime.now()
# FIXME: change get_stations() to return (headers, stations) directly
stations = sorted(itip.get_stations(args.line).items())
headers, stations = zip(*stations)
maxlength = max(len(stops) for stops in stations)
for stops in stations:
# Pad station list with empty items for printing, so that
# different-sized lists aren't truncated (with zip below)
stops.extend([('', '')] * (maxlength - len(stops)))
stations_table = zip(*stations)
fmt = '%%-%ds' % ITEM_WIDTH
spacer = ' ' * ITEM_SPACING
print
print spacer, spacer.join(inblue(fmt % ('Richtung %s' % name))
for name in headers)
print spacer, spacer.join('-' * ITEM_WIDTH for name in headers)
def match_station(query, station):
return query and station and (query.lower() in station.lower())
for row in stations_table:
print spacer, spacer.join(ingreen(fmt % name)
if match_station(args.station, name)
else fmt % name
for name, url in row)
print
# Get matching stations
stations = zip(headers, stations)
details = [(direction, name, url) for direction, stops in stations
for name, url in stops if match_station(args.station, name)]
# User entered a station, but no matches were found
if args.station and not details:
print inred('No station matched your query.')
print
# Format a departure time (in minutes from now) for display
def format_departure(departure):
minutes = departure['time']
if type(minutes) == time:
return inblue(minutes.strftime('%H:%M'))
elif minutes == 0:
return inred('now')
elif minutes == 1:
return inblue('1') + ' min'
else:
return inblue('%d' % minutes) + ' mins'
# Print the departure times for all matched stations
for direction, name, url in details:
print ingreen(name), '->', inblue(direction)
departures = itip.get_departures(url)
if departures:
print ' Next departures:', ', '.join(format_departure(x)
for x in departures)
else:
print ' No departure information.'
print
show_schwarzkappler_warnings()
else:
ITEMS_PER_LINE = 12
ITEM_WIDTH = 5
LINE_WIDTH = (ITEMS_PER_LINE * ITEM_WIDTH + ITEMS_PER_LINE)
if args.line:
print
print inred('The given line was not found. Valid lines:')
print
for label, remaining in categorize_lines(itip.lines):
prefix, fill, postfix = '|== ', '=', '==- -'
before, after = prefix + label + ' ', postfix
padding = LINE_WIDTH - len(before + after)
before = before.replace(label, inblue(label))
print ''.join((before, fill * padding, after))
while remaining:
this_row = [remaining.pop(0) for _ in
range(min(len(remaining), ITEMS_PER_LINE))]
print ' '.join(('%%%ds' % ITEM_WIDTH) % x for x in this_row)
print