From b8cb9881b38fe56bc8f16303aed2313230880492 Mon Sep 17 00:00:00 2001 From: steven Date: Sat, 10 May 2025 21:50:27 -0400 Subject: [PATCH 1/2] use httpx over requests --- app.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 67635ec..a3003ec 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,14 @@ from flask import Flask,render_template,send_from_directory from flask_caching import Cache -import requests +import httpx app=Flask(__name__) app.config['CACHE_TYPE']='SimpleCache' cache=Cache(app) -data=requests.get("https://tarc.rideralerts.com/InfoPoint/rest/Routes/GetVisibleRoutes").json() +data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Routes/GetVisibleRoutes").json() routes=",".join(str(r.get("RouteId")) for r in data) -data=requests.get("https://tarc.rideralerts.com/InfoPoint/rest/Stops/GetAllStops").json() +data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Stops/GetAllStops").json() stops={s.get("StopId"):[s.get("Latitude"),s.get("Longitude")] for s in data} @app.route("/") @@ -18,5 +18,5 @@ def index(): @app.route("/tarc.csv") @cache.cached(timeout=5) def tarc(): - d=requests.get("https://tarc.rideralerts.com/InfoPoint/rest/Vehicles/GetAllVehiclesForRoutes?routeIDs=0,"+routes).json() + d=httpx.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']},{v['LastUpdated'][6:16]}" for v in d) From 4025bc115b8d15c7d6e671761f54ea9d95bf6f8e Mon Sep 17 00:00:00 2001 From: steven Date: Sun, 11 May 2025 00:01:05 -0400 Subject: [PATCH 2/2] use custom iframes --- app.py | 23 ++++++++++++++++++++++- templates/map.html | 6 +++--- templates/stop.html | 10 ++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 templates/stop.html diff --git a/app.py b/app.py index a3003ec..fc99228 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ from flask import Flask,render_template,send_from_directory from flask_caching import Cache +from bs4 import BeautifulSoup import httpx app=Flask(__name__) @@ -9,7 +10,7 @@ cache=Cache(app) data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Routes/GetVisibleRoutes").json() routes=",".join(str(r.get("RouteId")) for r in data) data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Stops/GetAllStops").json() -stops={s.get("StopId"):[s.get("Latitude"),s.get("Longitude")] for s in data} +stops={s.get("StopId"):[[s.get("Latitude"),s.get("Longitude")],s.get("Name")] for s in data} @app.route("/") def index(): @@ -20,3 +21,23 @@ def index(): def tarc(): d=httpx.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']},{v['LastUpdated'][6:16]}" for v in d) + +@app.route('/') +@cache.cached(timeout=5) +def stop(stop): + if stop not in stops:return "not a stop" + response=httpx.post("https://tarc.rideralerts.com/InfoPoint/Stops/Detail",json={"StopID":stop}) + if response.status_code==200: + table=BeautifulSoup(response.content,"html.parser").find("table") + if table: + table.attrs={} + for element in table.find_all(True): + if element.name in ["div","a"]:element.unwrap() + else:element.attrs={} + for element in table.find_all(["th","td"]): + element.string=element.get_text(strip=True) + return render_template("stop.html",table=str(table)) + return render_template("stop.html",table="".join(str(table).split("\n"))) + else: + return "no stop information" + return "fetch failed" diff --git a/templates/map.html b/templates/map.html index cd48f02..aae2b55 100644 --- a/templates/map.html +++ b/templates/map.html @@ -9,15 +9,15 @@ - +