-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrnv.py
More file actions
executable file
·120 lines (93 loc) · 3.26 KB
/
Copy pathrnv.py
File metadata and controls
executable file
·120 lines (93 loc) · 3.26 KB
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
#!/usr/bin/env python3
"""
rnv
Usage:
rnv departures IDENTIFIER [-f FILTER -n COUNT -t TIME]
rnv stations
rnv line
rnv news [count | show]
rnv ticker [count | show]
-f FILTER --filter=FILTER Define transport filter
-n COUNT --count=COUNT Only display first COUNT connections
-t TIME --time=TIME Query connection at TIME
"""
import docopt
import json
import functools
import rnv_api
dump_json = functools.partial(json.dumps,
indent=4,
ensure_ascii=False,
sort_keys=True)
def get_departures_from_arg(args):
"""Gets departures from arguments and prints them
Gets arguments from dictionary, if necessary retrieves the HafasID from
station name and calls the api to get the departure data.
"""
transport_filter = args.get('--filter', None)
time = args.get('--time', None)
if args['IDENTIFIER'].isnumeric():
hafas_id = int(args['IDENTIFIER'])
else:
hafas_id = int(rnv_api.get_hafasid_from_name(args['IDENTIFIER']))
json_data = rnv_api.get_departures(hafas_id,
transport_filter=transport_filter,
time=time)
print("Current Time: {0}".format(json_data['time']))
if json_data.get('ticker', False):
print("Ticker: {0}".format(json_data['ticker']))
if args.get('--count', False):
n = int(args.get('--count', 0))
for dep in json_data['listOfDepartures'][:n]:
print(dump_json(dep))
else:
for dep in json_data['listOfDepartures']:
print(dump_json(dep))
def get_news_from_arg(args):
"""Get news from arguments
Differntiates between 'count' and 'show', calls the API
and prints the result.
"""
if(args.get('count', False)):
count = rnv_api.get_news_count()
print("{} news items available".format(count))
else:
news = rnv_api.get_news()
if news:
print(dump_json(news))
else:
print("No news available")
def get_ticker_from_arg(args):
"""Get ticker from arguments
Differntiates between 'count' and 'show', calls the API
and prints the result.
"""
if(args.get('count', False)):
count = rnv_api.get_ticker_count()
print("{} ticker items available".format(count))
else:
ticker = rnv_api.get_ticker()
if ticker:
print(dump_json(ticker))
else:
print("No new ticker items available")
def get_line_from_arg(args):
hafas_id = int(rnv_api.get_hafasid_from_name('sandhofen'))
print(dump_json(rnv_api.get_line(hafas_id, '45', '288920652', '454REFAUS')))
def main():
"""Differentiates between commands
"""
arguments = docopt.docopt(__doc__)
if arguments.get('departures', False):
get_departures_from_arg(arguments)
elif arguments.get('stations', False):
json_data = rnv_api.get_stations()
print(dump_json(json_data))
elif arguments.get('news', False):
get_news_from_arg(arguments)
elif arguments.get('ticker', False):
get_ticker_from_arg(arguments)
elif arguments.get('line', False):
get_line_from_arg(arguments)
if __name__ == '__main__':
main()