50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
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()
|