First public release

This commit is contained in:
Steven 2025-03-06 00:59:38 -05:00
parent 16691e8211
commit 2f7d67aa5a
4 changed files with 145 additions and 0 deletions

50
app.py Normal file
View file

@ -0,0 +1,50 @@
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">(.*)</span>[\s]*(.*)</h5>[\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}
@app.route("/")
def index():
return render_template("index.html",routes=routes)
@app.route("/routez")
def routez():
return routes
@app.route("/bus.svg")
def favicon():
return send_from_directory(app.static_folder, "bus.svg")
@app.route('/<int: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('/<int: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)
if __name__ == '__main__':
app.run()

1
static/bus.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="#e51837" d="M288 0C422.4 0 512 35.2 512 80l0 16 0 32c17.7 0 32 14.3 32 32l0 64c0 17.7-14.3 32-32 32l0 160c0 17.7-14.3 32-32 32l0 32c0 17.7-14.3 32-32 32l-32 0c-17.7 0-32-14.3-32-32l0-32-192 0 0 32c0 17.7-14.3 32-32 32l-32 0c-17.7 0-32-14.3-32-32l0-32c-17.7 0-32-14.3-32-32l0-160c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32c0 0 0 0 0 0l0-32s0 0 0 0l0-16C64 35.2 153.6 0 288 0zM128 160l0 96c0 17.7 14.3 32 32 32l112 0 0-160-112 0c-17.7 0-32 14.3-32 32zM304 288l112 0c17.7 0 32-14.3 32-32l0-96c0-17.7-14.3-32-32-32l-112 0 0 160zM144 400a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm288 0a32 32 0 1 0 0-64 32 32 0 1 0 0 64zM384 80c0-8.8-7.2-16-16-16L208 64c-8.8 0-16 7.2-16 16s7.2 16 16 16l160 0c8.8 0 16-7.2 16-16z"/></svg>

After

Width:  |  Height:  |  Size: 789 B

27
templates/index.html Normal file
View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>TARC Routes</title>
<meta charset="UTF-8">
<meta content="Realtime TARC bus maps" property="og:title">
<meta content='https://tarc.stevenalexander.org/bus.svg' property='og:image'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="/bus.svg">
<style>
body {
font-family: monospace;
font-size: 1cm;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
Find <a href="https://www.ridetarc.org/about-us/news/">news</a> and <a href="https://www.ridetarc.org/alerts/">alerts</a> at ridetarc.org<br>
Some busses don't provide location data.<br>
This service is provided without warranty.<br>
{%- for route in routes %}
<a href="{{route}}">{{'%02d'%route}} {{routes[route]["name"]}}</a><br>
{%- endfor %}
</body>
</html>

67
templates/map.html Normal file
View file

@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Route {{route}}</title>
<meta charset="UTF-8">
<meta content="Route {{route}}" property="og:title">
<meta content='https://tarc.stevenalexander.org/bus.svg' property='og:image'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="/bus.svg">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<style>
html, body {
height: 100%;
margin: 0;
}
#map {
height: 100vh;
}
#clock {
background-color: #000;
color: #FFF;
font: 16px monospace;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
let map=L.map("map",{center:[38.21,-85.75],zoom:13,layers:[L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")]});
L.control.scale({"position":"topright"}).addTo(map);
document.querySelector(".leaflet-top.leaflet-right").addEventListener("click",function() {
map.getContainer().requestFullscreen()
})
let s={{stops|safe}};
for (i in s) {
L.circle([s[i][0],s[i][1]],{radius:20,color:"#0092da"}).bindPopup(`<b>${s[i][2]} (#${i})</b><br>Sequence #${s[i].slice(3)}`).addTo(map)
}
let busMarkers=L.layerGroup().addTo(map);
function updateMap() {
fetch("{{route}}.csv").then(response=>response.text()).then(data=>{
if (data == "") return;
busMarkers.clearLayers();
data.split("\n").forEach(line=>{
var [n,t,lat,lng]=line.split(",");
L.marker([lat,lng]).bindPopup("<b>"+n+"</b> at<br>"+new Date(t*1000).toLocaleTimeString()).addTo(busMarkers)
})
})
}
document.querySelector(".leaflet-bottom.leaflet-right").innerHTML='<div id="clock"></div>';
let clockElement=document.querySelector("#clock");
function updateClock() {
clockElement.textContent=busMarkers.getLayers().length+" TARC(s) - "+new Date().toLocaleTimeString()
}
setInterval(updateMap,5000); // use websocket?
setInterval(updateClock,1000);
let locationMarker=L.marker([0,0]),locationRadius=L.circle([0,0]);
let locationLayer=L.layerGroup([locationMarker,locationRadius]).addTo(map);
map.locate({watch:true});
map.on("locationfound",e=>{
locationMarker.setLatLng(e.latlng).bindPopup("<b>You</b> at<br>"+new Date().toLocaleTimeString());
locationRadius.setLatLng(e.latlng).setRadius(e.accuracy/2).bringToBack()
});
</script>
</body>
</html>