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

Commit 52ab474

Browse files
committed
add support for 'old style' integer IDs to keep backward compatibility
1 parent 0b04d28 commit 52ab474

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

mvg_api/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
nearby_url = "https://www.mvg.de/api/fahrinfo/location/nearby?latitude={lat}&longitude={lon}"
1313
routing_url = "https://www.mvg.de/api/fahrinfo/routing/?"
1414
interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions"
15+
id_prefix = "de:09162:"
16+
17+
def _convert_id(old_id: int) -> str:
18+
return id_prefix + str(old_id)
1519

1620
def _station_sanity_check(id:str):
1721
"""
@@ -206,16 +210,21 @@ def get_route(start, dest,
206210
if isinstance(start, tuple) and len(start) == 2:
207211
options.append("fromLatitude=" + str(start[0]))
208212
options.append("fromLongitude=" + str(start[1]))
213+
elif isinstance(start, int):
214+
options.append("fromStation=" + _convert_id(start))
209215
elif _station_sanity_check(start):
210216
options.append("fromStation=" + start)
211217
else:
212218
raise ValueError("A start must be given;\
213-
either int station id or tuple latitude longitude")
219+
either int station id, 'new style' string ids \
220+
or a tuple with latitude and longitude")
214221

215222

216223
if isinstance(dest, tuple) and len(dest) == 2:
217224
options.append("toLatitude=" + str(dest[0]))
218225
options.append("toLongitude=" + str(dest[1]))
226+
elif isinstance(dest, int):
227+
options.append("toStation=" + _convert_id(dest))
219228
elif _station_sanity_check(dest):
220229
options.append("toStation=" + dest)
221230
else:
@@ -274,7 +283,9 @@ def get_departures(station_id):
274283
`departureTimeMinutes`, the time left to the departure in minutes,
275284
is added to the response from the api for your convenience.
276285
"""
277-
if not _station_sanity_check(station_id):
286+
if isinstance(station_id, int):
287+
station_id = _convert_id(station_id)
288+
elif not _station_sanity_check(station_id):
278289
raise TypeError("Please give the int station_id of the station.\
279290
You can find it out by running \
280291
get_id_for_station('Station name')")

0 commit comments

Comments
 (0)