Adding in full MTA api.
* Added in support for station search, route specific search, and several time limiting functions Added in functional backend in flask * starts flask app * starts MTA app on another thread * serves basic webpage which pull subway data from flask backend on button press.
This commit is contained in:
69
server.py
Normal file
69
server.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import os
|
||||
import threading
|
||||
from flask import Flask, jsonify, render_template, request
|
||||
from mta import MTA
|
||||
from pprint import pprint
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "SuperSecretDontEvenTryToGuessMeGGEZNoRe"
|
||||
app.debug = True
|
||||
app._static_folder = os.path.abspath("templates/static/")
|
||||
|
||||
subway_data = {}
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def index():
|
||||
title = "Create the input image"
|
||||
return render_template("layouts/index.html", title=title)
|
||||
|
||||
|
||||
@app.route("/mta_data", methods=["POST"])
|
||||
def get_mta_data():
|
||||
content = request.json
|
||||
return jsonify(
|
||||
subway_data
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
api_key = os.getenv('MTA_API_KEY', '')
|
||||
mtaController = MTA(
|
||||
api_key,
|
||||
["A", "C", "E", "1", "2", "3"],
|
||||
["127S", "127N", "A27N", "A27S"]
|
||||
)
|
||||
|
||||
|
||||
async def mta_callback(routes):
|
||||
global subway_data
|
||||
print("We are inside of the call back now")
|
||||
print(len(routes))
|
||||
subway_data = mtaController.convert_routes_to_station_first(routes)
|
||||
pprint(subway_data)
|
||||
|
||||
|
||||
class threadWrapper(threading.Thread):
|
||||
def __init__(self, run):
|
||||
threading.Thread.__init__(self)
|
||||
self.run = run
|
||||
|
||||
def run(self):
|
||||
self.run()
|
||||
|
||||
|
||||
def start_mta():
|
||||
mtaController.add_callback(mta_callback)
|
||||
mtaController.start_updates()
|
||||
|
||||
|
||||
threadLock = threading.Lock()
|
||||
threads = [threadWrapper(start_mta)]
|
||||
|
||||
for t in threads:
|
||||
t.start()
|
||||
|
||||
app.run("0.0.0.0", port=5000)
|
||||
# Wait for all threads to complete
|
||||
for t in threads:
|
||||
t.join()
|
||||
print("Exiting Main Thread")
|
||||
Reference in New Issue
Block a user