From 0db90601d9d189cf6ee5ff7aa9b4f51b9071142e Mon Sep 17 00:00:00 2001 From: steven Date: Sat, 22 Mar 2025 20:12:49 -0400 Subject: [PATCH] adapt to (tarc.)rideralerts.com api --- app.py | 43 ++++++++----------------------------------- templates/index.html | 27 --------------------------- templates/map.html | 41 +++++++++-------------------------------- 3 files changed, 17 insertions(+), 94 deletions(-) delete mode 100644 templates/index.html diff --git a/app.py b/app.py index 80eb9fb..a5369bc 100644 --- a/app.py +++ b/app.py @@ -1,50 +1,23 @@ 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} +data=requests.get("https://tarc.rideralerts.com/InfoPoint/rest/Routes/GetVisibleRoutes").json() +routes = ",".join(str(r.get('RouteId')) for r in data) @app.route("/") def index(): - return render_template("index.html",routes=routes) - -@app.route("/routez") -def routez(): - return routes + return render_template("map.html") @app.route("/bus.svg") def favicon(): - return send_from_directory(app.static_folder, "bus.svg") + 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) +@app.route('/tarc.csv') +def tarc(): + d=requests.get("https://tarc.rideralerts.com/InfoPoint/rest/Vehicles/GetAllVehiclesForRoutes?routeIDs=0,"+routes).json() + return "\n".join(f"{v['Latitude']},{v['Longitude']},{v['RouteId']},{v['Name']},{v['OnBoard']},{v['DirectionLong']},{v['Speed']}" for v in d) if __name__ == '__main__': app.run() diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index 0df9ec2..0000000 --- a/templates/index.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - 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 index b5cc47a..903019e 100644 --- a/templates/map.html +++ b/templates/map.html @@ -1,59 +1,36 @@ - Route {{route}} + TARC Routes - +