|
@@ -10,21 +10,18 @@ metric_map = {
|
|
|
"location": "coordinates"
|
|
|
}
|
|
|
url = "https://vancouver-ca.smoove.pro/api-public/stations"
|
|
|
-# use old dump if no live data
|
|
|
-#with open('live_dump.json', 'w') as f:
|
|
|
-# json.dump(old_data, f)
|
|
|
|
|
|
-with open('live_dump.json') as f:
|
|
|
- old_data = json.load(f)
|
|
|
- data = old_data
|
|
|
+#with open('live_dump.json') as f:
|
|
|
+# old_data = json.load(f)
|
|
|
+# data = old_data
|
|
|
|
|
|
def refresh_data():
|
|
|
# try to get new data
|
|
|
try:
|
|
|
new = requests.get(url)
|
|
|
except requests.exceptions.RequestException:
|
|
|
- print("using old data")
|
|
|
- data = old_data
|
|
|
+ print("failed to get data")
|
|
|
+ data = None
|
|
|
else:
|
|
|
data = new.json()["result"]
|
|
|
|
|
@@ -35,6 +32,10 @@ app = Flask(__name__)
|
|
|
def get_bikes(station, metric):
|
|
|
refresh_data()
|
|
|
|
|
|
+ # if refresh failed, return 404
|
|
|
+ if not data:
|
|
|
+ return 502
|
|
|
+
|
|
|
# find the station
|
|
|
match = False
|
|
|
for each in data:
|
|
@@ -45,9 +46,9 @@ def get_bikes(station, metric):
|
|
|
match = each
|
|
|
break
|
|
|
|
|
|
- # return 400 if no match
|
|
|
+ # return 404 if no match
|
|
|
if not match:
|
|
|
- return None
|
|
|
+ return 404
|
|
|
|
|
|
if metric == "both":
|
|
|
result = "Spots: " + str(match["avl_bikes"]) + '<br/>'
|
|
@@ -64,8 +65,10 @@ def get_metric_info(station, metric):
|
|
|
except KeyError:
|
|
|
abort(404, description="Sorry, that metric does not exist")
|
|
|
result = get_bikes(station, metric_map[metric])
|
|
|
- if not result:
|
|
|
+ if result == 404:
|
|
|
abort(404, description="Sorry, that station does not exist")
|
|
|
+ elif result == 502:
|
|
|
+ abort(502, description="Sorry, the API server isn't responding right now")
|
|
|
else:
|
|
|
return result
|
|
|
|
|
@@ -74,7 +77,9 @@ def get_metric_info(station, metric):
|
|
|
@app.route('/api/<string:station>/', methods=['GET'])
|
|
|
def get_station_info(station):
|
|
|
result = get_bikes(station, "both")
|
|
|
- if not result:
|
|
|
+ if result == 404:
|
|
|
abort(404, description="Sorry, that station does not exist")
|
|
|
+ elif result == 502:
|
|
|
+ abort(502, description="Sorry, the API server isn't responding right now")
|
|
|
else:
|
|
|
return result
|