tarc/app.py

40 lines
1.6 KiB
Python
Raw Normal View History

from flask import Flask,render_template,send_from_directory
2025-03-22 23:00:30 -04:00
from flask_caching import Cache
2025-05-11 00:01:05 -04:00
from bs4 import BeautifulSoup
2025-05-10 21:50:27 -04:00
import httpx
2025-03-06 00:59:38 -05:00
app=Flask(__name__)
app.config['CACHE_TYPE']='SimpleCache'
cache=Cache(app)
2025-03-06 00:59:38 -05:00
2025-05-10 21:50:27 -04:00
data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Routes/GetVisibleRoutes").json()
2025-04-17 00:35:17 -04:00
routes=",".join(str(r.get("RouteId")) for r in data)
2025-05-10 21:50:27 -04:00
data=httpx.get("https://tarc.rideralerts.com/InfoPoint/rest/Stops/GetAllStops").json()
2025-05-11 00:01:05 -04:00
stops={s.get("StopId"):[[s.get("Latitude"),s.get("Longitude")],s.get("Name")] for s in data}
2025-04-17 00:35:17 -04:00
2025-03-06 00:59:38 -05:00
@app.route("/")
def index():
2025-04-17 00:35:17 -04:00
return render_template("map.html",stops=stops)
2025-03-06 00:59:38 -05:00
@app.route("/tarc.csv")
2025-03-22 23:00:30 -04:00
@cache.cached(timeout=5)
2025-03-22 20:12:49 -04:00
def tarc():
2025-05-10 21:50:27 -04:00
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['Destination']},{v['Heading']},{v['Speed']},{v['DirectionLong']},{v['LastUpdated'][6:16]}" for v in d)
2025-05-11 00:01:05 -04:00
2025-05-27 19:30:18 -04:00
@app.route('/<int:stop>.csv')
2025-05-11 00:01:05 -04:00
@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:
2025-05-27 19:30:18 -04:00
csv=[",".join([h.get_text(strip=True) for h in table.find_all("th")])]
for row in table.find_all("tr")[1:]:
csv.append(",".join([c.get_text(strip=True) for c in row.find_all("td")]))
return "\n".join(csv)
2025-05-11 00:01:05 -04:00
else:
return "no stop information"
return "fetch failed"