|
13 | 13 | routing_url = "https://www.mvg.de/api/fahrinfo/routing/?"
|
14 | 14 | interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions"
|
15 | 15 |
|
| 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 | + |
16 | 32 |
|
17 | 33 | def _perform_api_request(url):
|
18 | 34 | 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,
|
186 | 202 | url = routing_url
|
187 | 203 | options = []
|
188 | 204 |
|
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: |
192 | 207 | options.append("fromLatitude=" + str(start[0]))
|
193 | 208 | options.append("fromLongitude=" + str(start[1]))
|
| 209 | + elif _station_sanity_check(start): |
| 210 | + options.append("fromStation=" + start) |
194 | 211 | else:
|
195 | 212 | raise ValueError("A start must be given;\
|
196 | 213 | either int station id or tuple latitude longitude")
|
197 | 214 |
|
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: |
201 | 217 | options.append("toLatitude=" + str(dest[0]))
|
202 | 218 | options.append("toLongitude=" + str(dest[1]))
|
| 219 | + elif _station_sanity_check(dest): |
| 220 | + options.append("toStation=" + dest) |
203 | 221 | else:
|
204 | 222 | raise ValueError("A destination must be given;\
|
205 | 223 | either int station id or tuple latitude longitude")
|
@@ -256,11 +274,11 @@ def get_departures(station_id):
|
256 | 274 | `departureTimeMinutes`, the time left to the departure in minutes,
|
257 | 275 | is added to the response from the api for your convenience.
|
258 | 276 | """
|
259 |
| - if not isinstance(station_id, int): |
| 277 | + if not _station_sanity_check(station_id): |
260 | 278 | raise TypeError("Please give the int station_id of the station.\
|
261 | 279 | You can find it out by running \
|
262 | 280 | get_id_for_station('Station name')")
|
263 |
| - url = departure_url.format(id=str(station_id)) |
| 281 | + url = departure_url.format(id=station_id) |
264 | 282 | departures = _perform_api_request(url)['departures']
|
265 | 283 | for departure in departures:
|
266 | 284 | # For some reason, mvg gives you a Unix timestamp, but in milliseconds.
|
|
0 commit comments