Skip to content
This repository was archived by the owner on Jun 29, 2023. It is now read-only.

Commit 0b04d28

Browse files
author
Sebastian Lehrack
committed
Updated a sanity check function for new format of station ids.
1 parent e544376 commit 0b04d28

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

mvg_api/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
routing_url = "https://www.mvg.de/api/fahrinfo/routing/?"
1414
interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions"
1515

16+
def _station_sanity_check(id:str):
17+
"""
18+
New ID format has these specifications:
19+
starts with de
20+
has two : (checked with split)
21+
second and third field is integer (not checked)
22+
:param id: station id to be checked
23+
:return: Boolean on id sanity
24+
"""
25+
split_id = id.split(":")
26+
if not len(split_id)==3:
27+
return False
28+
if not split_id[0]=='de':
29+
return False
30+
return True
31+
1632

1733
def _perform_api_request(url):
1834
resp = requests.get(url, headers={'X-MVG-Authorization-Key': api_key, 'User-Agent': 'python-mvg-api/1 (+https://github.com/leftshift/python_mvg_api)', 'Accept': 'application/json'})
@@ -186,20 +202,22 @@ def get_route(start, dest,
186202
url = routing_url
187203
options = []
188204

189-
if isinstance(start, int):
190-
options.append("fromStation=" + str(start))
191-
elif isinstance(start, tuple) and len(start) == 2:
205+
206+
if isinstance(start, tuple) and len(start) == 2:
192207
options.append("fromLatitude=" + str(start[0]))
193208
options.append("fromLongitude=" + str(start[1]))
209+
elif _station_sanity_check(start):
210+
options.append("fromStation=" + start)
194211
else:
195212
raise ValueError("A start must be given;\
196213
either int station id or tuple latitude longitude")
197214

198-
if isinstance(dest, int):
199-
options.append("toStation=" + str(dest))
200-
elif isinstance(dest, tuple) and len(dest) == 2:
215+
216+
if isinstance(dest, tuple) and len(dest) == 2:
201217
options.append("toLatitude=" + str(dest[0]))
202218
options.append("toLongitude=" + str(dest[1]))
219+
elif _station_sanity_check(dest):
220+
options.append("toStation=" + dest)
203221
else:
204222
raise ValueError("A destination must be given;\
205223
either int station id or tuple latitude longitude")
@@ -256,11 +274,11 @@ def get_departures(station_id):
256274
`departureTimeMinutes`, the time left to the departure in minutes,
257275
is added to the response from the api for your convenience.
258276
"""
259-
if not isinstance(station_id, int):
277+
if not _station_sanity_check(station_id):
260278
raise TypeError("Please give the int station_id of the station.\
261279
You can find it out by running \
262280
get_id_for_station('Station name')")
263-
url = departure_url.format(id=str(station_id))
281+
url = departure_url.format(id=station_id)
264282
departures = _perform_api_request(url)['departures']
265283
for departure in departures:
266284
# For some reason, mvg gives you a Unix timestamp, but in milliseconds.

0 commit comments

Comments
 (0)