11
11
nearby_url = "https://www.mvg.de/api/fahrinfo/location/nearby?latitude={lat}&longitude={lon}"
12
12
routing_url = "https://www.mvg.de/api/fahrinfo/routing/?"
13
13
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
14
34
15
35
16
36
def _perform_api_request (url ):
@@ -63,7 +83,7 @@ def get_nearby_stations(lat, lon):
63
83
'type': 'station',
64
84
'latitude': 48.12046,
65
85
'longitude': 11.61869,
66
- 'id': 1060,
86
+ 'id': 'de:09162: 1060' ,
67
87
'place': 'München',
68
88
'name': 'Innsbrucker Ring',
69
89
'hasLiveData': True,
@@ -121,7 +141,7 @@ def get_locations(query):
121
141
'type': 'station',
122
142
'latitude': 48.12046,
123
143
'longitude': 11.61869,
124
- 'id': 1060,
144
+ 'id': 'de:09162: 1060' ,
125
145
'place': 'München',
126
146
'name': 'Innsbrucker Ring',
127
147
'hasLiveData': True,
@@ -171,11 +191,15 @@ def get_route(start, dest,
171
191
change_limit = None ):
172
192
"""Plans a route from start to dest
173
193
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
+
174
198
Parameters
175
199
----------
176
- start : int/tuple
200
+ start : int/str/ tuple
177
201
The `station_id` of the starting station or a tuple of coordinates
178
- dest : int/tuple
202
+ dest : int/str/ tuple
179
203
`station_id` of the destination station or a tuple of coordinates
180
204
time : datetime, optional
181
205
arrival_time : bool, optional
@@ -189,20 +213,27 @@ def get_route(start, dest,
189
213
url = routing_url
190
214
options = []
191
215
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 :
195
218
options .append ("fromLatitude=" + str (start [0 ]))
196
219
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 )
197
224
else :
198
225
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" )
200
228
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 :
204
231
options .append ("toLatitude=" + str (dest [0 ]))
205
232
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 )
206
237
else :
207
238
raise ValueError ("A destination must be given;\
208
239
either int station id or tuple latitude longitude" )
@@ -237,6 +268,10 @@ def get_route(start, dest,
237
268
def get_departures (station_id ):
238
269
"""Get the next departures for `station_id`.
239
270
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
+
240
275
To get the `station_id` associated with a station name,
241
276
use :func:`get_id_for_station`.
242
277
@@ -259,11 +294,13 @@ def get_departures(station_id):
259
294
`departureTimeMinutes`, the time left to the departure in minutes,
260
295
is added to the response from the api for your convenience.
261
296
"""
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 ):
263
300
raise TypeError ("Please give the int station_id of the station.\
264
301
You can find it out by running \
265
302
get_id_for_station('Station name')" )
266
- url = departure_url .format (id = str ( station_id ) )
303
+ url = departure_url .format (id = station_id )
267
304
departures = _perform_api_request (url )['departures' ]
268
305
for departure in departures :
269
306
# For some reason, mvg gives you a Unix timestamp, but in milliseconds.
0 commit comments