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

Commit f4f9f0d

Browse files
authored
Merge pull request #16 from Bastlwastl42/develop
Fix support for new station IDs
2 parents 68d3864 + b9eaa79 commit f4f9f0d

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

mvg_api/__init__.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111
nearby_url = "https://www.mvg.de/api/fahrinfo/location/nearby?latitude={lat}&longitude={lon}"
1212
routing_url = "https://www.mvg.de/api/fahrinfo/routing/?"
1313
interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions"
14+
id_prefix = "de:09162:"
15+
16+
def _convert_id(old_id: int) -> str:
17+
return id_prefix + str(old_id)
18+
19+
def _station_sanity_check(id:str):
20+
"""
21+
New ID format has these specifications:
22+
starts with de
23+
has two : (checked with split)
24+
second and third field is integer (not checked)
25+
:param id: station id to be checked
26+
:return: Boolean on id sanity
27+
"""
28+
split_id = id.split(":")
29+
if not len(split_id)==3:
30+
return False
31+
if not split_id[0]=='de':
32+
return False
33+
return True
1434

1535

1636
def _perform_api_request(url):
@@ -63,7 +83,7 @@ def get_nearby_stations(lat, lon):
6383
'type': 'station',
6484
'latitude': 48.12046,
6585
'longitude': 11.61869,
66-
'id': 1060,
86+
'id': 'de:09162:1060',
6787
'place': 'München',
6888
'name': 'Innsbrucker Ring',
6989
'hasLiveData': True,
@@ -121,7 +141,7 @@ def get_locations(query):
121141
'type': 'station',
122142
'latitude': 48.12046,
123143
'longitude': 11.61869,
124-
'id': 1060,
144+
'id': 'de:09162:1060',
125145
'place': 'München',
126146
'name': 'Innsbrucker Ring',
127147
'hasLiveData': True,
@@ -171,11 +191,15 @@ def get_route(start, dest,
171191
change_limit=None):
172192
"""Plans a route from start to dest
173193
194+
Change in 1.3.1: accepts both 'old-style' integer IDs which were used
195+
by the API before this version and the new string IDs which
196+
look like `de:09162:6`.
197+
174198
Parameters
175199
----------
176-
start : int/tuple
200+
start : int/str/tuple
177201
The `station_id` of the starting station or a tuple of coordinates
178-
dest : int/tuple
202+
dest : int/str/tuple
179203
`station_id` of the destination station or a tuple of coordinates
180204
time : datetime, optional
181205
arrival_time : bool, optional
@@ -189,20 +213,27 @@ def get_route(start, dest,
189213
url = routing_url
190214
options = []
191215

192-
if isinstance(start, int):
193-
options.append("fromStation=" + str(start))
194-
elif isinstance(start, tuple) and len(start) == 2:
216+
217+
if isinstance(start, tuple) and len(start) == 2:
195218
options.append("fromLatitude=" + str(start[0]))
196219
options.append("fromLongitude=" + str(start[1]))
220+
elif isinstance(start, int):
221+
options.append("fromStation=" + _convert_id(start))
222+
elif _station_sanity_check(start):
223+
options.append("fromStation=" + start)
197224
else:
198225
raise ValueError("A start must be given;\
199-
either int station id or tuple latitude longitude")
226+
either int station id, 'new style' string ids \
227+
or a tuple with latitude and longitude")
200228

201-
if isinstance(dest, int):
202-
options.append("toStation=" + str(dest))
203-
elif isinstance(dest, tuple) and len(dest) == 2:
229+
230+
if isinstance(dest, tuple) and len(dest) == 2:
204231
options.append("toLatitude=" + str(dest[0]))
205232
options.append("toLongitude=" + str(dest[1]))
233+
elif isinstance(dest, int):
234+
options.append("toStation=" + _convert_id(dest))
235+
elif _station_sanity_check(dest):
236+
options.append("toStation=" + dest)
206237
else:
207238
raise ValueError("A destination must be given;\
208239
either int station id or tuple latitude longitude")
@@ -237,6 +268,10 @@ def get_route(start, dest,
237268
def get_departures(station_id):
238269
"""Get the next departures for `station_id`.
239270
271+
Change in 1.3.1: accepts both 'old-style' integer IDs which were used
272+
by the API before this version and the new string IDs which
273+
look like `de:09162:6`.
274+
240275
To get the `station_id` associated with a station name,
241276
use :func:`get_id_for_station`.
242277
@@ -259,11 +294,13 @@ def get_departures(station_id):
259294
`departureTimeMinutes`, the time left to the departure in minutes,
260295
is added to the response from the api for your convenience.
261296
"""
262-
if not isinstance(station_id, int):
297+
if isinstance(station_id, int):
298+
station_id = _convert_id(station_id)
299+
elif not _station_sanity_check(station_id):
263300
raise TypeError("Please give the int station_id of the station.\
264301
You can find it out by running \
265302
get_id_for_station('Station name')")
266-
url = departure_url.format(id=str(station_id))
303+
url = departure_url.format(id=station_id)
267304
departures = _perform_api_request(url)['departures']
268305
for departure in departures:
269306
# For some reason, mvg gives you a Unix timestamp, but in milliseconds.

0 commit comments

Comments
 (0)