from flask import Flask,render_template,send_from_directory from flask_caching import Cache from bs4 import BeautifulSoup import httpx app=Flask(__name__) app.config['CACHE_TYPE']='SimpleCache' 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")],s.get("Name")] for s in data} @app.route("/") def index(): return render_template("map.html",stops=stops) @app.route("/tarc.csv") @cache.cached(timeout=5) 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['Heading']},{v['DirectionLong']},{v['Destination']},{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"