Compare commits
2 commits
0b8017cbc8
...
4025bc115b
Author | SHA1 | Date | |
---|---|---|---|
4025bc115b | |||
b8cb9881b3 |
3 changed files with 39 additions and 8 deletions
31
app.py
31
app.py
|
@ -1,15 +1,16 @@
|
||||||
from flask import Flask,render_template,send_from_directory
|
from flask import Flask,render_template,send_from_directory
|
||||||
from flask_caching import Cache
|
from flask_caching import Cache
|
||||||
import requests
|
from bs4 import BeautifulSoup
|
||||||
|
import httpx
|
||||||
|
|
||||||
app=Flask(__name__)
|
app=Flask(__name__)
|
||||||
app.config['CACHE_TYPE']='SimpleCache'
|
app.config['CACHE_TYPE']='SimpleCache'
|
||||||
cache=Cache(app)
|
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)
|
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}
|
stops={s.get("StopId"):[[s.get("Latitude"),s.get("Longitude")],s.get("Name")] for s in data}
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
|
@ -18,5 +19,25 @@ def index():
|
||||||
@app.route("/tarc.csv")
|
@app.route("/tarc.csv")
|
||||||
@cache.cached(timeout=5)
|
@cache.cached(timeout=5)
|
||||||
def tarc():
|
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)
|
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('/<int:stop>')
|
||||||
|
@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"
|
||||||
|
|
|
@ -9,15 +9,15 @@
|
||||||
<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;}</style>
|
<style>html,body{height:100%;margin:0;}#map{height:100vh;}iframe{border:none;width:400px;}.leaflet-popup-content{min-width:400px}</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}};
|
let stops={{stops|safe}};
|
||||||
for (stop in stops){
|
for (stop in stops){
|
||||||
L.circle(stops[stop]).bindPopup(`<iframe src="https://tarc.rideralerts.com/InfoPoint/Minimal/Departures/ForStop?stopId=${stop}">Stop #${stop}</iframe>`).addTo(map)
|
L.circle(stops[stop][0]).bindPopup(`${stops[stop][1]} (#${stop})<iframe src="${stop}"></iframe>`).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);
|
||||||
|
|
10
templates/stop.html
Normal file
10
templates/stop.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<style>html,body{margin:0;font-size:12px}</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{table|safe}}
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue