|
6 | 6 | from time import mktime |
7 | 7 |
|
8 | 8 | api_key = "5af1beca494712ed38d313714d4caff6" |
9 | | -query_url_name = "https://www.mvg.de/fahrinfo/api/location/queryWeb?q=" #for station names |
10 | | -query_url_id = "https://www.mvg.de/fahrinfo/api/location/query?q=" #for station ids |
11 | | -departure_url = "https://www.mvg.de/fahrinfo/api/departure/" |
12 | | -departure_url_postfix = "?footway=0" |
13 | | -nearby_url = "https://www.mvg.de/fahrinfo/api/location/nearby" |
| 9 | +query_url_name = "https://www.mvg.de/fahrinfo/api/location/queryWeb?q={name}" #for station names |
| 10 | +query_url_id = "https://www.mvg.de/fahrinfo/api/location/query?q={id}" #for station ids |
| 11 | +departure_url = "https://www.mvg.de/fahrinfo/api/departure/{id}?footway=0" |
| 12 | +nearby_url = "https://www.mvg.de/fahrinfo/api/location/nearby?latitude={lat}&longitude={lon}" |
14 | 13 | routing_url = "https://www.mvg.de/fahrinfo/api/routing/?" |
15 | | -interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen\ |
16 | | - /api/interruptions" |
| 14 | +interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions" |
17 | 15 |
|
18 | 16 |
|
19 | 17 | def _perform_api_request(url): |
@@ -89,7 +87,7 @@ def get_nearby_stations(lat, lon): |
89 | 87 | if not (isinstance(lat, float) and isinstance(lon, float)): |
90 | 88 | raise TypeError() |
91 | 89 |
|
92 | | - url = nearby_url + "?latitude=%f&longitude=%f" % (lat, lon) |
| 90 | + url = nearby_url.format(lat=lat, lon=lon) |
93 | 91 |
|
94 | 92 | results = _perform_api_request(url) |
95 | 93 | return results['locations'] |
@@ -141,9 +139,9 @@ def get_locations(query): |
141 | 139 | try: |
142 | 140 | query = int(query) # converts station ids to int if thay aren't already |
143 | 141 | except(ValueError): # happens if it is a station name |
144 | | - url = query_url_name + query |
| 142 | + url = query_url_name.format(name=query) |
145 | 143 | else: # happens if it is a station id |
146 | | - url = query_url_id + str(query) |
| 144 | + url = query_url_id.format(id=str(query)) |
147 | 145 |
|
148 | 146 | results = _perform_api_request(url) |
149 | 147 | return results["locations"] |
@@ -252,7 +250,7 @@ def get_departures(station_id): |
252 | 250 | raise TypeError("Please give the int station_id of the station.\ |
253 | 251 | You can find it out by running \ |
254 | 252 | get_id_for_station('Station name')") |
255 | | - url = departure_url + str(station_id) + departure_url_postfix |
| 253 | + url = departure_url.format(id=str(station_id)) |
256 | 254 | departures = _perform_api_request(url)['departures'] |
257 | 255 | for departure in departures: |
258 | 256 | # For some reason, mvg gives you a Unix timestamp, but in milliseconds. |
@@ -295,7 +293,7 @@ def get_departures(self): |
295 | 293 | """Gets the departures for the station object. |
296 | 294 | Pretty much the same like module-level-:func:`get_departures` |
297 | 295 | """ |
298 | | - url = departure_url + str(self.station_id) + departure_url_postfix |
| 296 | + url = departure_url.format(id=str(self.station_id)) |
299 | 297 | departures = _perform_api_request(url)['departures'] |
300 | 298 | for departure in departures: |
301 | 299 | # For some reason, mvg gives you a Unix timestamp in milliseconds. |
|
0 commit comments