diff --git a/app.py b/app.py new file mode 100644 index 0000000..80eb9fb --- /dev/null +++ b/app.py @@ -0,0 +1,50 @@ +from flask import Flask, jsonify, render_template, send_from_directory +import requests +import re +import json + +app = Flask(__name__) + +routes={} +txt=requests.get("https://www.ridetarc.org/getting-around/routes/").text +for r in re.findall(r'"rtNo">(.*)[\s]*(.*)[\s\S]*?href="([^"]*)',txt): + stops=json.loads(re.search(r'triangleCoords = (.*);',requests.get(r[2]).text).group(1)) + e=dict() + for i in stops: + code=i["stop_code"] + if code in e: + e[code].append(i["stop_sequence"]) + e[code]=e[code][:3]+sorted(e[code][3:]) + else: + e[code]=[i["lat"],i["lng"],i["stop_name"],i["stop_sequence"]] + # check for stops in txt, for some reason [4, 2, 6] are not there, and the sequence numbers are gone + routes[int(r[0])]={"name":r[1],"stops":e} + +@app.route("/") +def index(): + return render_template("index.html",routes=routes) + +@app.route("/routez") +def routez(): + return routes + +@app.route("/bus.svg") +def favicon(): + return send_from_directory(app.static_folder, "bus.svg") + +@app.route('/') +def map(route): + if route not in routes: return "not a valid route" + return render_template("map.html",route=route,stops=json.dumps(routes[route]["stops"],separators=(',',':'))) + +@app.route('/.csv') +def api(route): + if route not in routes: return "400" + u='https://www.ridetarc.org/wp-admin/admin-ajax.php?action=route_vehicle&route_id='+('0'+str(route))[-2:] + d=requests.get(u).json() + result=[] + for i in d["data"]: result.append(f"{i["vehicle"]["id"]},{i["timestamp"]},{i["position"]["latitude"]},{i["position"]["longitude"]}") + return "\n".join(result) + +if __name__ == '__main__': + app.run() diff --git a/static/bus.svg b/static/bus.svg new file mode 100644 index 0000000..7b8da44 --- /dev/null +++ b/static/bus.svg @@ -0,0 +1 @@ + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0df9ec2 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,27 @@ + + + + TARC Routes + + + + + + + + + Find news and alerts at ridetarc.org
+ Some busses don't provide location data.
+ This service is provided without warranty.
+ {%- for route in routes %} + {{'%02d'%route}} {{routes[route]["name"]}}
+ {%- endfor %} + + diff --git a/templates/map.html b/templates/map.html new file mode 100644 index 0000000..b5cc47a --- /dev/null +++ b/templates/map.html @@ -0,0 +1,67 @@ + + + + Route {{route}} + + + + + + + + + + +
+ + +