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

Commit f0ec386

Browse files
committed
fix interruptions url
use format strings for urls
1 parent 85c50f2 commit f0ec386

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

mvg_api/__init__.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
from time import mktime
77

88
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}"
1413
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"
1715

1816

1917
def _perform_api_request(url):
@@ -89,7 +87,7 @@ def get_nearby_stations(lat, lon):
8987
if not (isinstance(lat, float) and isinstance(lon, float)):
9088
raise TypeError()
9189

92-
url = nearby_url + "?latitude=%f&longitude=%f" % (lat, lon)
90+
url = nearby_url.format(lat=lat, lon=lon)
9391

9492
results = _perform_api_request(url)
9593
return results['locations']
@@ -141,9 +139,9 @@ def get_locations(query):
141139
try:
142140
query = int(query) # converts station ids to int if thay aren't already
143141
except(ValueError): # happens if it is a station name
144-
url = query_url_name + query
142+
url = query_url_name.format(name=query)
145143
else: # happens if it is a station id
146-
url = query_url_id + str(query)
144+
url = query_url_id.format(id=str(query))
147145

148146
results = _perform_api_request(url)
149147
return results["locations"]
@@ -252,7 +250,7 @@ def get_departures(station_id):
252250
raise TypeError("Please give the int station_id of the station.\
253251
You can find it out by running \
254252
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))
256254
departures = _perform_api_request(url)['departures']
257255
for departure in departures:
258256
# For some reason, mvg gives you a Unix timestamp, but in milliseconds.
@@ -295,7 +293,7 @@ def get_departures(self):
295293
"""Gets the departures for the station object.
296294
Pretty much the same like module-level-:func:`get_departures`
297295
"""
298-
url = departure_url + str(self.station_id) + departure_url_postfix
296+
url = departure_url.format(id=str(self.station_id))
299297
departures = _perform_api_request(url)['departures']
300298
for departure in departures:
301299
# For some reason, mvg gives you a Unix timestamp in milliseconds.

0 commit comments

Comments
 (0)