forked from Ugric/cov-to-ics
add times to timetable filename
This commit is contained in:
24
main.py
24
main.py
@@ -7,6 +7,7 @@ from flask import Flask, request, send_file, render_template, Response
|
||||
import requests
|
||||
from requests_ntlm import HttpNtlmAuth
|
||||
import urllib
|
||||
import html
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Config
|
||||
@@ -58,7 +59,7 @@ def get_events_data(page_data):
|
||||
|
||||
for script in soup.head.find_all("script", {"type": "text/javascript"}):
|
||||
if not script.has_attr("src"):
|
||||
source = script.text
|
||||
source = html.unescape(script.text)
|
||||
break
|
||||
else:
|
||||
raise RuntimeError("Could not find inline timetable script")
|
||||
@@ -133,6 +134,19 @@ def build_calendar(page_data):
|
||||
|
||||
now = ics_time(datetime.now(timezone.utc))
|
||||
|
||||
# --- work out date range ---
|
||||
if new_events:
|
||||
start_dt = min(e["start"] for e in new_events)
|
||||
end_dt = max(e["end"] for e in new_events)
|
||||
|
||||
start_year = start_dt.year
|
||||
start_month = start_dt.strftime("%b")
|
||||
end_year = end_dt.year
|
||||
end_month = end_dt.strftime("%b")
|
||||
else:
|
||||
# sensible fallback if no events
|
||||
start_year = start_month = end_year = end_month = None
|
||||
|
||||
lines = [
|
||||
"BEGIN:VCALENDAR",
|
||||
"VERSION:2.0",
|
||||
@@ -167,7 +181,9 @@ def build_calendar(page_data):
|
||||
|
||||
lines.append("END:VCALENDAR")
|
||||
|
||||
return "\n".join(lines)
|
||||
data = "\n".join(lines)
|
||||
|
||||
return data, start_year, start_month, end_year, end_month
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
@@ -184,11 +200,11 @@ def login_and_download():
|
||||
username = data["username"]
|
||||
password = data["password"]
|
||||
try:
|
||||
data = build_calendar(fetch_timetable(f"COVENTRY\\{username}", password))
|
||||
data, start_year, start_month, end_year, end_month = build_calendar(fetch_timetable(f"COVENTRY\\{username}", password))
|
||||
return Response(
|
||||
data,
|
||||
mimetype="text/calendar",
|
||||
headers={"Content-Disposition": f'attachment; filename="{username}-timetable.ics"'},
|
||||
headers={"Content-Disposition": f'attachment; filename="{username}_timetable_{start_month}-{start_year}_to_{end_month}-{end_year}.ics"'},
|
||||
)
|
||||
except Exception as e:
|
||||
return render_template("login.jinja", error=str(e)), 401
|
||||
|
||||
Reference in New Issue
Block a user