2023-10-02 20:35:30 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
"""Web page to show upcoming events."""
|
|
|
|
|
2023-10-06 23:29:22 +01:00
|
|
|
import inspect
|
2023-12-04 21:53:33 +00:00
|
|
|
import operator
|
2023-11-20 19:48:42 +00:00
|
|
|
import os.path
|
2023-10-06 23:29:22 +01:00
|
|
|
import sys
|
|
|
|
import traceback
|
2023-11-20 19:48:42 +00:00
|
|
|
from datetime import date, datetime
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-10-06 23:29:22 +01:00
|
|
|
import flask
|
|
|
|
import werkzeug
|
|
|
|
import werkzeug.debug.tbtools
|
2023-11-20 19:48:42 +00:00
|
|
|
import yaml
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-11-07 15:55:05 +00:00
|
|
|
import agenda.data
|
2023-12-07 17:15:58 +00:00
|
|
|
import agenda.error_mail
|
2024-01-03 09:13:58 +00:00
|
|
|
import agenda.thespacedevs
|
2024-01-04 22:55:19 +00:00
|
|
|
from agenda import format_list_with_ampersand, travel
|
|
|
|
from agenda.types import StrDict, Trip
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-10-06 23:29:22 +01:00
|
|
|
app = flask.Flask(__name__)
|
2023-11-07 15:55:05 +00:00
|
|
|
app.debug = False
|
2023-12-07 15:52:48 +00:00
|
|
|
app.config.from_object("config.default")
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-12-07 17:15:58 +00:00
|
|
|
agenda.error_mail.setup_error_mail(app)
|
|
|
|
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-10-06 23:29:22 +01:00
|
|
|
@app.errorhandler(werkzeug.exceptions.InternalServerError)
|
|
|
|
def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]:
|
|
|
|
"""Handle exception."""
|
|
|
|
exec_type, exc_value, current_traceback = sys.exc_info()
|
|
|
|
assert exc_value
|
|
|
|
tb = werkzeug.debug.tbtools.DebugTraceback(exc_value)
|
|
|
|
|
|
|
|
summary = tb.render_traceback_html(include_title=False)
|
|
|
|
exc_lines = "".join(tb._te.format_exception_only())
|
|
|
|
|
|
|
|
last_frame = list(traceback.walk_tb(current_traceback))[-1][0]
|
|
|
|
last_frame_args = inspect.getargs(last_frame.f_code)
|
|
|
|
|
|
|
|
return (
|
|
|
|
flask.render_template(
|
|
|
|
"show_error.html",
|
|
|
|
plaintext=tb.render_traceback_text(),
|
|
|
|
exception=exc_lines,
|
|
|
|
exception_type=tb._te.exc_type.__name__,
|
|
|
|
summary=summary,
|
|
|
|
last_frame=last_frame,
|
|
|
|
last_frame_args=last_frame_args,
|
|
|
|
),
|
|
|
|
500,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-10-02 20:35:30 +01:00
|
|
|
@app.route("/")
|
2023-11-05 14:40:02 +00:00
|
|
|
async def index() -> str:
|
2023-10-02 20:35:30 +01:00
|
|
|
"""Index page."""
|
|
|
|
now = datetime.now()
|
2023-12-07 15:52:48 +00:00
|
|
|
data = await agenda.data.get_data(now, app.config)
|
2023-10-02 20:35:30 +01:00
|
|
|
|
2023-10-21 22:58:44 +01:00
|
|
|
return flask.render_template("index.html", today=now.date(), **data)
|
2023-10-02 20:35:30 +01:00
|
|
|
|
|
|
|
|
2024-01-03 09:13:58 +00:00
|
|
|
@app.route("/launches")
|
|
|
|
async def launch_list() -> str:
|
|
|
|
"""Web page showing List of space launches."""
|
|
|
|
now = datetime.now()
|
|
|
|
data_dir = app.config["DATA_DIR"]
|
|
|
|
rocket_dir = os.path.join(data_dir, "thespacedevs")
|
|
|
|
rockets = await agenda.thespacedevs.get_launches(rocket_dir, limit=100)
|
|
|
|
|
|
|
|
return flask.render_template("launches.html", rockets=rockets, now=now)
|
|
|
|
|
|
|
|
|
2023-12-27 08:22:53 +00:00
|
|
|
@app.route("/gaps")
|
|
|
|
async def gaps_page() -> str:
|
|
|
|
"""List of available gaps."""
|
|
|
|
now = datetime.now()
|
|
|
|
data = await agenda.data.get_data(now, app.config)
|
|
|
|
return flask.render_template("gaps.html", today=now.date(), gaps=data["gaps"])
|
|
|
|
|
|
|
|
|
2023-11-19 15:22:12 +00:00
|
|
|
@app.route("/travel")
|
2023-11-20 19:48:42 +00:00
|
|
|
def travel_list() -> str:
|
2023-11-19 15:22:12 +00:00
|
|
|
"""Page showing a list of upcoming travel."""
|
2023-12-07 15:52:48 +00:00
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
2024-01-04 22:55:19 +00:00
|
|
|
flights = travel.parse_yaml("flights", data_dir)
|
|
|
|
trains = travel.parse_yaml("trains", data_dir)
|
2023-11-19 15:22:12 +00:00
|
|
|
|
2023-11-20 19:48:42 +00:00
|
|
|
return flask.render_template("travel.html", flights=flights, trains=trains)
|
|
|
|
|
|
|
|
|
|
|
|
def as_date(d: date | datetime) -> date:
|
|
|
|
"""Date of event."""
|
|
|
|
return d.date() if isinstance(d, datetime) else d
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/conference")
|
|
|
|
def conference_list() -> str:
|
|
|
|
"""Page showing a list of conferences."""
|
2023-12-07 15:52:48 +00:00
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
2023-11-20 19:48:42 +00:00
|
|
|
filepath = os.path.join(data_dir, "conferences.yaml")
|
2024-01-04 15:08:12 +00:00
|
|
|
item_list = yaml.safe_load(open(filepath))
|
2023-12-04 21:53:33 +00:00
|
|
|
today = date.today()
|
|
|
|
for conf in item_list:
|
2023-12-04 23:01:14 +00:00
|
|
|
conf["start_date"] = as_date(conf["start"])
|
|
|
|
conf["end_date"] = as_date(conf["end"])
|
2023-11-20 19:48:42 +00:00
|
|
|
|
2023-12-04 23:01:14 +00:00
|
|
|
item_list.sort(key=operator.itemgetter("start_date"))
|
2023-11-20 19:48:42 +00:00
|
|
|
|
2023-12-04 23:01:14 +00:00
|
|
|
current = [
|
|
|
|
conf
|
|
|
|
for conf in item_list
|
|
|
|
if conf["start_date"] <= today and conf["end_date"] >= today
|
|
|
|
]
|
|
|
|
|
|
|
|
past = [conf for conf in item_list if conf["end_date"] < today]
|
|
|
|
future = [conf for conf in item_list if conf["start_date"] > today]
|
2023-12-04 21:53:33 +00:00
|
|
|
|
|
|
|
return flask.render_template(
|
2024-01-03 15:52:24 +00:00
|
|
|
"conference_list.html",
|
|
|
|
current=current,
|
|
|
|
past=past,
|
|
|
|
future=future,
|
|
|
|
today=today,
|
2024-01-04 22:55:19 +00:00
|
|
|
get_country=agenda.get_country,
|
2023-12-04 21:53:33 +00:00
|
|
|
)
|
2023-11-19 15:22:12 +00:00
|
|
|
|
|
|
|
|
2024-01-01 21:26:39 +00:00
|
|
|
@app.route("/accommodation")
|
|
|
|
def accommodation_list() -> str:
|
|
|
|
"""Page showing a list of past, present and future accommodation."""
|
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
2024-01-04 22:55:19 +00:00
|
|
|
items = travel.parse_yaml("accommodation", data_dir)
|
2024-01-01 21:26:39 +00:00
|
|
|
|
|
|
|
stays_in_2024 = [item for item in items if item["from"].year == 2024]
|
|
|
|
total_nights_2024 = sum(
|
|
|
|
(stay["to"].date() - stay["from"].date()).days for stay in stays_in_2024
|
|
|
|
)
|
|
|
|
|
|
|
|
nights_abroad_2024 = sum(
|
|
|
|
(stay["to"].date() - stay["from"].date()).days
|
|
|
|
for stay in stays_in_2024
|
|
|
|
if stay["country"] != "gb"
|
|
|
|
)
|
|
|
|
|
|
|
|
return flask.render_template(
|
|
|
|
"accommodation.html",
|
|
|
|
items=items,
|
|
|
|
total_nights_2024=total_nights_2024,
|
|
|
|
nights_abroad_2024=nights_abroad_2024,
|
2024-01-04 22:55:19 +00:00
|
|
|
get_country=agenda.get_country,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def load_travel(travel_type: str) -> list[StrDict]:
|
|
|
|
"""Read flight and train journeys."""
|
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
|
|
|
items = travel.parse_yaml(travel_type + "s", data_dir)
|
|
|
|
for item in items:
|
|
|
|
item["type"] = travel_type
|
|
|
|
return items
|
|
|
|
|
|
|
|
|
2024-01-12 16:20:36 +00:00
|
|
|
def load_trains() -> list[StrDict]:
|
|
|
|
"""Load trains."""
|
2024-01-04 22:55:19 +00:00
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
|
|
|
|
2024-01-12 16:20:36 +00:00
|
|
|
trains = load_travel("train")
|
2024-01-12 14:04:06 +00:00
|
|
|
stations = travel.parse_yaml("stations", data_dir)
|
|
|
|
by_name = {station["name"]: station for station in stations}
|
|
|
|
|
|
|
|
for train in trains:
|
|
|
|
assert train["from"] in by_name
|
|
|
|
assert train["to"] in by_name
|
|
|
|
train["from_station"] = by_name[train["from"]]
|
|
|
|
train["to_station"] = by_name[train["to"]]
|
|
|
|
|
|
|
|
for leg in train["legs"]:
|
|
|
|
assert leg["from"] in by_name
|
|
|
|
assert leg["to"] in by_name
|
2024-01-12 16:54:52 +00:00
|
|
|
leg["from_station"] = by_name[leg["from"]]
|
|
|
|
leg["to_station"] = by_name[leg["to"]]
|
2024-01-12 14:04:06 +00:00
|
|
|
|
2024-01-12 16:20:36 +00:00
|
|
|
return trains
|
|
|
|
|
|
|
|
|
|
|
|
def load_flights() -> list[StrDict]:
|
|
|
|
"""Load flights."""
|
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
|
|
|
flights = load_travel("flight")
|
|
|
|
airports = travel.parse_yaml("airports", data_dir)
|
|
|
|
for flight in flights:
|
|
|
|
if flight["from"] in airports:
|
|
|
|
flight["from_airport"] = airports[flight["from"]]
|
|
|
|
if flight["to"] in airports:
|
|
|
|
flight["to_airport"] = airports[flight["to"]]
|
|
|
|
return flights
|
|
|
|
|
|
|
|
|
|
|
|
def build_trip_list() -> list[Trip]:
|
|
|
|
"""Generate list of trips."""
|
|
|
|
trips: dict[date, Trip] = {}
|
|
|
|
|
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
|
|
|
|
2024-01-04 22:55:19 +00:00
|
|
|
travel_items = sorted(
|
2024-01-12 16:20:36 +00:00
|
|
|
load_flights() + load_trains(), key=operator.itemgetter("depart")
|
2024-01-04 22:55:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"travel": travel_items,
|
|
|
|
"accommodation": travel.parse_yaml("accommodation", data_dir),
|
|
|
|
"conferences": travel.parse_yaml("conferences", data_dir),
|
2024-01-10 13:27:25 +00:00
|
|
|
"events": travel.parse_yaml("events", data_dir),
|
2024-01-04 22:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, item_list in data.items():
|
|
|
|
assert isinstance(item_list, list)
|
|
|
|
for item in item_list:
|
2024-01-05 09:35:56 +00:00
|
|
|
if not (start := item.get("trip")):
|
2024-01-04 22:55:19 +00:00
|
|
|
continue
|
2024-01-05 09:35:56 +00:00
|
|
|
if start not in trips:
|
|
|
|
trips[start] = Trip(start=start)
|
|
|
|
getattr(trips[start], key).append(item)
|
2024-01-04 22:55:19 +00:00
|
|
|
|
2024-01-06 09:17:34 +00:00
|
|
|
return [trip for _, trip in sorted(trips.items())]
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/trip")
|
|
|
|
def trip_list() -> str:
|
|
|
|
"""Page showing a list of trips."""
|
|
|
|
trip_list = build_trip_list()
|
|
|
|
|
|
|
|
today = date.today()
|
|
|
|
current = [
|
|
|
|
item
|
|
|
|
for item in trip_list
|
|
|
|
if item.start <= today and (item.end or item.start) >= today
|
|
|
|
]
|
|
|
|
|
|
|
|
past = [item for item in trip_list if (item.end or item.start) < today]
|
|
|
|
future = [item for item in trip_list if item.start > today]
|
2024-01-04 22:55:19 +00:00
|
|
|
|
|
|
|
return flask.render_template(
|
|
|
|
"trips.html",
|
2024-01-06 09:17:34 +00:00
|
|
|
current=current,
|
|
|
|
past=past,
|
|
|
|
future=future,
|
|
|
|
today=today,
|
2024-01-04 22:55:19 +00:00
|
|
|
get_country=agenda.get_country,
|
|
|
|
format_list_with_ampersand=format_list_with_ampersand,
|
2024-01-01 21:26:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-12 16:54:52 +00:00
|
|
|
def collect_trip_coordinates(trip: Trip) -> list[StrDict]:
|
|
|
|
"""Extract and deduplicate airport and station coordinates from trip."""
|
2024-01-12 15:04:08 +00:00
|
|
|
stations = {}
|
|
|
|
station_list = []
|
2024-01-12 16:20:36 +00:00
|
|
|
airports = {}
|
2024-01-12 15:04:08 +00:00
|
|
|
for t in trip.travel:
|
2024-01-12 16:20:36 +00:00
|
|
|
if t["type"] == "train":
|
|
|
|
station_list += [t["from_station"], t["to_station"]]
|
|
|
|
for leg in t["legs"]:
|
|
|
|
station_list.append(leg["from_station"])
|
|
|
|
station_list.append(leg["to_station"])
|
|
|
|
else:
|
|
|
|
assert t["type"] == "flight"
|
|
|
|
for field in "from_airport", "to_airport":
|
|
|
|
if field in t:
|
|
|
|
airports[t[field]["iata"]] = t[field]
|
2024-01-12 15:04:08 +00:00
|
|
|
|
|
|
|
for s in station_list:
|
|
|
|
if s["uic"] in stations:
|
|
|
|
continue
|
|
|
|
stations[s["uic"]] = s
|
|
|
|
|
2024-01-12 16:20:36 +00:00
|
|
|
return [
|
2024-01-12 16:54:52 +00:00
|
|
|
{
|
|
|
|
"name": s["name"],
|
|
|
|
"type": "station",
|
|
|
|
"latitude": s["latitude"],
|
|
|
|
"longitude": s["longitude"],
|
|
|
|
}
|
|
|
|
for s in stations.values()
|
|
|
|
] + [
|
|
|
|
{
|
|
|
|
"name": s["name"],
|
|
|
|
"type": "airport",
|
|
|
|
"latitude": s["latitude"],
|
|
|
|
"longitude": s["longitude"],
|
|
|
|
}
|
|
|
|
for s in airports.values()
|
2024-01-12 16:20:36 +00:00
|
|
|
]
|
2024-01-12 15:04:08 +00:00
|
|
|
|
|
|
|
|
2024-01-12 17:17:12 +00:00
|
|
|
def latlon_tuple(stop: StrDict) -> tuple[float, float]:
|
|
|
|
"""Given a transport stop return the lat/lon as a tuple."""
|
|
|
|
return (stop["latitude"], stop["longitude"])
|
|
|
|
|
|
|
|
|
2024-01-12 22:29:10 +00:00
|
|
|
def read_geojson(filename: str) -> str:
|
|
|
|
data_dir = app.config["PERSONAL_DATA"]
|
|
|
|
return open(os.path.join(data_dir, "train_routes", filename + ".geojson")).read()
|
|
|
|
|
|
|
|
|
|
|
|
def get_trip_routes(trip: Trip) -> list[StrDict]:
|
2024-01-12 17:17:12 +00:00
|
|
|
routes = []
|
2024-01-12 22:29:10 +00:00
|
|
|
seen_geojson = set()
|
2024-01-12 17:17:12 +00:00
|
|
|
for t in trip.travel:
|
|
|
|
if t["type"] == "flight":
|
|
|
|
if "from_airport" not in t or "to_airport" not in t:
|
|
|
|
continue
|
|
|
|
fly_from, fly_to = t["from_airport"], t["to_airport"]
|
2024-01-12 22:29:10 +00:00
|
|
|
routes.append(
|
|
|
|
{
|
|
|
|
"type": "flight",
|
|
|
|
"from": latlon_tuple(fly_from),
|
|
|
|
"to": latlon_tuple(fly_to),
|
|
|
|
}
|
|
|
|
)
|
2024-01-12 17:17:12 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
assert t["type"] == "train"
|
|
|
|
for leg in t["legs"]:
|
|
|
|
train_from, train_to = leg["from_station"], leg["to_station"]
|
2024-01-12 22:29:10 +00:00
|
|
|
geojson_filename = train_from.get("routes", {}).get(train_to["uic"])
|
|
|
|
if not geojson_filename:
|
|
|
|
routes.append(
|
|
|
|
{
|
|
|
|
"type": "train",
|
|
|
|
"from": latlon_tuple(train_from),
|
|
|
|
"to": latlon_tuple(train_to),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if geojson_filename in seen_geojson:
|
|
|
|
continue
|
|
|
|
seen_geojson.add(geojson_filename)
|
|
|
|
|
2024-01-12 19:52:00 +00:00
|
|
|
routes.append(
|
2024-01-12 22:29:10 +00:00
|
|
|
{
|
|
|
|
"type": "train",
|
|
|
|
"geojson": read_geojson(geojson_filename),
|
|
|
|
}
|
2024-01-12 19:52:00 +00:00
|
|
|
)
|
2024-01-12 17:17:12 +00:00
|
|
|
|
|
|
|
return routes
|
|
|
|
|
|
|
|
|
2024-01-12 14:04:06 +00:00
|
|
|
@app.route("/trip/<start>")
|
|
|
|
def trip_page(start: str) -> str:
|
2024-01-12 14:08:36 +00:00
|
|
|
"""Individual trip page."""
|
2024-01-12 14:04:06 +00:00
|
|
|
trip_list = build_trip_list()
|
|
|
|
today = date.today()
|
|
|
|
|
|
|
|
trip = next((trip for trip in trip_list if trip.start.isoformat() == start), None)
|
2024-01-12 14:08:36 +00:00
|
|
|
|
|
|
|
if not trip:
|
|
|
|
flask.abort(404)
|
|
|
|
|
2024-01-12 16:54:52 +00:00
|
|
|
coordinates = collect_trip_coordinates(trip)
|
2024-01-12 17:17:12 +00:00
|
|
|
routes = get_trip_routes(trip)
|
2024-01-12 15:04:08 +00:00
|
|
|
|
2024-01-12 14:04:06 +00:00
|
|
|
return flask.render_template(
|
|
|
|
"trip_page.html",
|
|
|
|
trip=trip,
|
|
|
|
today=today,
|
2024-01-12 16:54:52 +00:00
|
|
|
coordinates=coordinates,
|
2024-01-12 17:17:12 +00:00
|
|
|
routes=routes,
|
2024-01-12 14:04:06 +00:00
|
|
|
get_country=agenda.get_country,
|
|
|
|
format_list_with_ampersand=format_list_with_ampersand,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-10-02 20:35:30 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0")
|