diff --git a/server.py b/server.py
index ecaaf37..e24f18c 100644
--- a/server.py
+++ b/server.py
@@ -1,7 +1,7 @@
import os
import threading
-from flask import Flask, jsonify, render_template, request
+from flask import Flask, jsonify, render_template, request, abort
from mta_manager import MTA
from pprint import pprint
import pandas as pd
@@ -12,7 +12,6 @@ load_dotenv()
app = Flask(__name__)
app.secret_key = "SuperSecretDontEvenTryToGuessMeGGEZNoRe"
-app.debug = True
app._static_folder = os.path.abspath("templates/static/")
stops = pd.read_csv("stops.txt")
@@ -48,12 +47,12 @@ def link_to_station(data):
@app.route("/mta_data", methods=["POST"])
def get_mta_data():
station = request.json["station"]
- print(jsonify(
- subway_data[station]
- ))
- return jsonify(
- subway_data[station]
- )
+ if station in subway_data:
+ return jsonify(
+ subway_data[station]
+ )
+ else:
+ abort(404)
@app.route("/stops", methods=["GET"])
@@ -73,6 +72,7 @@ def get_stop_id():
if __name__ == "__main__":
api_key = os.getenv('MTA_API_KEY', '')
+
mtaController = MTA(
api_key,
["A", "C", "E", "1", "2", "3"],
@@ -83,6 +83,7 @@ if __name__ == "__main__":
async def mta_callback(routes):
global subway_data
subway_data = link_to_station(mtaController.convert_routes_to_station_first(routes))
+ app.logger.info(f"Updated Subway Data - {subway_data}")
class threadWrapper(threading.Thread):
@@ -105,7 +106,9 @@ if __name__ == "__main__":
for t in threads:
t.start()
- app.run(host="localhost", debug=True, port=5000)
+ debug = os.getenv("DEBUG", 'False').lower() in ('true', '1', 't')
+
+ app.run(host="localhost", debug= debug, port=5000)
# Wait for all threads to complete
for t in threads:
t.join()
diff --git a/templates/layouts/old.html b/templates/layouts/old.html
deleted file mode 100644
index 485c807..0000000
--- a/templates/layouts/old.html
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
-
-
-
-
- SUBWAY DISPLAY!
-
-
-
-
-
- PI MTA TRACKER
-
-
-
-
-
-
Train 1 Info
-
Train 2 Info
-
Train 3 Info
-
-
-
-
Train 1 Info
-
Train 2 Info
-
Train 3 Info
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/templates/static/js/DataRequests.js b/templates/static/js/DataRequests.js
index 5e68732..0201ea8 100644
--- a/templates/static/js/DataRequests.js
+++ b/templates/static/js/DataRequests.js
@@ -2,22 +2,21 @@ $(document).ready(function () {
const interval = setInterval(function () {
updateData($('#station_1'))
- updateData($('#station_2'))
}, 5000);
+ const interval2 = setInterval(function () {
+ updateData($('#station_2'))
+ }, 6000);
function updateData(station) {
$.ajax({
type: "POST",
- //the url where you want to sent the userName and password to
url: '/mta_data',
contentType: "application/json",
dataType: "json",
async: true,
- //json object to sent to the authentication url
data: JSON.stringify({"station": station.find('.station-name:first').get(0).innerText}, null, '\t'),
success: function (data, text) {
- // console.log(data)
updateStation(station, data)
},
error: function (request, status, error) {
@@ -27,32 +26,58 @@ $(document).ready(function () {
}
function updateStation(station, data) {
- //get first item
updateDirections(station, data, "North");
updateDirections(station, data, "South");
}
function updateDirections(station, data, direction) {
- // console.log(direction)
- // console.log(".card:".concat(direction === "North" ? "first" : "last"))
n = data[direction]
- // console.log(data[direction])
list_items = station.find(".card:".concat(direction === "North" ? "first" : "last")).find(".station-info")
var i = 0;
for (var train in n) {
- // console.log(train)
- // console.log(list_items)
updateLineItem(list_items.get(i), n[train], train)
i = i + 1
}
+ if (i < 3) {
+ console.log("Only 2 items updated")
+ for (let remainingIndex = i; remainingIndex < 3; remainingIndex++) {
+ updateLineItem(list_items.get(i), "No Trains Available", "N/A")
+ }
+ }
}
function updateLineItem(listItem, times, train) {
- // console.log(times)
- $(listItem).find("img").attr("src", "/static/images/lines/" + train + ".svg")
- $(listItem).find("h1").text(times.sort(function (a, b) {
- return a - b;
- }).join(", "));
+ var timeString;
+ if (typeof times === 'string' || times instanceof String) {
+ timeString = times;
+ } else {
+ timeString = times.sort(function (a, b) {
+ return a - b;
+ }).join(", ");
+ }
+ $(listItem).find("h1").text(timeString);
+ if (imageExists("/static/images/lines/" + train + ".svg")) {
+
+ $(listItem).show()
+ $(listItem).find("img").attr("src", "/static/images/lines/" + train + ".svg")
+ } else if (train === "N/A"){
+ console.log("Route Is NA - Disabling Route")
+ console.log($(listItem))
+ $(listItem).hide()
+ }
+
+
+ }
+
+ function imageExists(image_url) {
+
+ var http = new XMLHttpRequest();
+
+ http.open('HEAD', image_url, false);
+ http.send();
+
+ return http.status != 404;
+
}
});