-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ns_schedule.py
121 lines (84 loc) · 2.93 KB
/
ns_schedule.py
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
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime
class NoArgsError(Exception):
pass
class NoApiKeyError(Exception):
pass
def create_headers():
api_key = os.environ.get('NS_APIKEY')
if api_key is None:
raise NoApiKeyError
return {'Ocp-Apim-Subscription-Key': api_key}
def call_api(origin, destination):
params = urllib.parse.urlencode({'fromStation': origin, 'toStation': destination})
url = 'https://gateway.apiportal.ns.nl/reisinformatie-api/api/v3/trips?' + params
req = urllib.request.Request(url, headers=create_headers())
response = urllib.request.urlopen(req)
return json.loads(response.read())
def first_station(trip):
first_leg = trip['legs'][0]
return first_leg['origin']
def last_station(trip):
last_leg = trip['legs'][-1]
return last_leg['destination']
def duration(time):
return "%d:%02d" % (time / 60, time % 60)
def format_date(date):
return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S%z').strftime('%H:%M')
def create_title(trip):
return "%s -> %s\t\t\tDuration: (%s)\tTransfers: %dx" % (
format_date(first_station(trip)['plannedDateTime']),
format_date(last_station(trip)['plannedDateTime']),
duration(trip['plannedDurationInMinutes']),
trip['transfers']
)
def product_names(trip):
transfers = [leg['product']['longCategoryName'] for leg in trip['legs']]
return ' + '.join(transfers)
def create_subtitle(trip):
if trip['status'] == 'CANCELLED':
return "%s\t\t\t%s" % (
trip['status'],
product_names(trip)
)
else:
return "Track: %s\t\t\t%s" % (
first_station(trip)['plannedTrack'],
product_names(trip)
)
def new_item_from_trip(trip):
return {'title': create_title(trip),
'subtitle': create_subtitle(trip),
'arg': trip['shareUrl']['uri']}
def extract_arguments():
try:
if len(sys.argv) == 3:
if sys.argv[1] and sys.argv[2]:
return sys.argv[1], sys.argv[2]
raise IndexError
except IndexError:
raise NoArgsError
def retrieve_schedule():
try:
(origin, destination) = extract_arguments()
response = call_api(origin, destination)
return [new_item_from_trip(trip) for trip in response['trips']]
except NoApiKeyError:
return [{'title': 'Please specify API key in settings'}]
except NoArgsError:
return [{'title': 'Please provide origin and destination stations'}]
except ValueError:
return [{'title': 'Can\'t parse server response'}]
except urllib.error.HTTPError:
return [{'title': 'Error contacting server'}]
def create_json(items):
return json.dumps({'items': items})
def main(): # pragma: nocover
print((create_json(retrieve_schedule())))
if __name__ == '__main__': # pragma: nocover
main()