better stops (still WIP)

This commit is contained in:
steven 2025-05-27 19:30:18 -04:00
parent 0f5f878485
commit b060960392
2 changed files with 37 additions and 23 deletions

14
app.py
View file

@ -22,7 +22,7 @@ def tarc():
d=httpx.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['Destination']},{v['Heading']},{v['Speed']},{v['DirectionLong']},{v['LastUpdated'][6:16]}" for v in d) 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)
@app.route('/<int:stop>') @app.route('/<int:stop>.csv')
@cache.cached(timeout=5) @cache.cached(timeout=5)
def stop(stop): def stop(stop):
if stop not in stops:return "not a stop" if stop not in stops:return "not a stop"
@ -30,14 +30,10 @@ def stop(stop):
if response.status_code==200: if response.status_code==200:
table=BeautifulSoup(response.content,"html.parser").find("table") table=BeautifulSoup(response.content,"html.parser").find("table")
if table: if table:
table.attrs={} csv=[",".join([h.get_text(strip=True) for h in table.find_all("th")])]
for element in table.find_all(True): for row in table.find_all("tr")[1:]:
if element.name in ["div","a"]:element.unwrap() csv.append(",".join([c.get_text(strip=True) for c in row.find_all("td")]))
else:element.attrs={} return "\n".join(csv)
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: else:
return "no stop information" return "no stop information"
return "fetch failed" return "fetch failed"

View file

@ -9,27 +9,48 @@
<script src="/static/leaflet/leaflet.js"></script> <script src="/static/leaflet/leaflet.js"></script>
<link rel="stylesheet" href="/static/leaflet/leaflet.css"> <link rel="stylesheet" href="/static/leaflet/leaflet.css">
<base target="_blank"> <base target="_blank">
<style>html,body{height:100%;margin:0;}#map{height:100vh;}iframe{border:none;width:444px;}</style> <style>html,body{height:100%;margin:0;}#map{height:100vh;}table{white-space:nowrap;}.leaflet-popup-content{width:auto !important;}</style>
</head> </head>
<body> <body>
<div id="map"></div> <div id="map"></div>
<script> <script>
let map=L.map("map",{center:[38.22,-85.7],zoom:12,layers:[L.tileLayer("https://tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=cf957cde4a7b40bbb49479c8fa4d60f7")]}); let map=L.map("map",{center:[38.22,-85.7],zoom:12,layers:[L.tileLayer("https://tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=cf957cde4a7b40bbb49479c8fa4d60f7")]});
let stops={{stops|safe}}; let stops={{stops|safe}};
for (stop in stops){ for(let stop in stops){
L.circle(stops[stop][0]).bindPopup(`${stops[stop][1]} (<a href=${stop}>#${stop}</a>)<iframe src="${stop}"></iframe>`).addTo(map) stops[stop][0]=L.circle(stops[stop][0]).bindPopup(`${stops[stop][1]} (<a href=${stop}.csv>#${stop}</a>)`).on('click',()=>fetchStop(stop)).addTo(map)
} }
let busIcon=L.icon({iconUrl:'static/bus.ico',iconSize:[32,32]}) let busIcon=L.icon({iconUrl:"static/bus.ico",iconSize:[32,32]})
let layerControl=L.control.layers().addTo(map); let layerControl=L.control.layers().addTo(map);
let busLayers={},busMarkers={}; let busLayers={},busMarkers={};
function filterRoutes(wanted){ function filterRoutes(wanted){
for(r in busLayers){ for(r in busLayers){
if(!wanted||wanted==r){ !wanted||wanted==r?map.addLayer(busLayers[r]):map.removeLayer(busLayers[r])
map.addLayer(busLayers[r])
} else{
map.removeLayer(busLayers[r])
} }
} }
function fetchStop(s){
fetch(s+".csv").then(response=>response.text()).then(csv=>{
const table=document.createElement('table');
const rows=csv.split('\n');
const headerRow=document.createElement('tr');
const header=rows[0].split(',');
header.forEach(header=>{
const th=document.createElement('th');
th.textContent=header;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
for(let i=1;i<rows.length;i++){
const row=rows[i].split(',');
const tr=document.createElement('tr');
row.forEach(value=>{
const td=document.createElement('td');
td.textContent=value;
tr.appendChild(td);
});
table.appendChild(tr);
}
stops[s][0].setPopupContent(`${stops[s][1]} (<a href=${s}.csv>#${s}</a>)${table.outerHTML}`).addTo(map)
})
} }
function updateMap(){ function updateMap(){
let routes={}; let routes={};
@ -47,13 +68,10 @@ function updateMap(){
for(let r in routes){ for(let r in routes){
overlayLayers[`${r.padStart(2,0)} | ${String(routes[r]).padStart(2,0)} vehicle(s)`]=busLayers[r] overlayLayers[`${r.padStart(2,0)} | ${String(routes[r]).padStart(2,0)} vehicle(s)`]=busLayers[r]
} }
layerControl.remove(); layerControl.remove();layerControl=L.control.layers(null,overlayLayers).addTo(map)
layerControl=L.control.layers(null,overlayLayers).addTo(map);
let iframe=document.querySelector("iframe");if(iframe)iframe.src=iframe.src
}) })
} }
updateMap(); updateMap();setInterval(updateMap,5000);
setInterval(updateMap,5000);
map.on('click',()=>filterRoutes()) map.on('click',()=>filterRoutes())
let locationMarker=L.marker([0,0]).addTo(map); let locationMarker=L.marker([0,0]).addTo(map);