Files
cov-to-ics/main.py
2026-01-23 01:39:54 +00:00

194 lines
5.2 KiB
Python

from bs4 import BeautifulSoup
from datetime import datetime, timezone
import pytz
import hashlib
import os
from flask import Flask, request, send_file, render_template
import requests
from requests_ntlm import HttpNtlmAuth
# ------------------------------------------------------------
# Config
# ------------------------------------------------------------
RESPONSE_FILE = "response.txt"
OLD_CALENDAR = "old_calendar.ics"
NEW_CALENDAR = "calendar.ics"
LOCAL_TZ = pytz.timezone("Europe/London")
URL = "https://webapp.coventry.ac.uk/Timetable-main/Timetable/Current"
app = Flask(__name__)
def parse_events(events_data):
events_data = events_data.replace("new Date", "")
cleaned_data = ""
for line in events_data.split("\n"):
comment_pos = line.find("//")
if comment_pos != -1:
line = line[:comment_pos]
if ":" in line:
key, val = line.split(":", 1)
line = f"'{key}': {val}"
cleaned_data += line + "\n"
parsed_data = eval(cleaned_data)
for event in parsed_data:
if "start" in event:
s = list(event["start"])
s[1] += 1
s.append(0)
event["start"] = LOCAL_TZ.localize(datetime(*s))
if "end" in event:
e = list(event["end"])
e[1] += 1
e.append(0)
event["end"] = LOCAL_TZ.localize(datetime(*e))
return parsed_data
def get_events_data_from_file(path):
with open(path, "r", encoding="utf-8") as f:
page_data = f.read()
soup = BeautifulSoup(page_data, features="html.parser")
for script in soup.head.find_all("script", {"type": "text/javascript"}):
if not script.has_attr("src"):
source = script.text
break
else:
raise RuntimeError("Could not find inline timetable script")
return source.split("events:")[1].split("]")[0] + "]"
def ics_time(dt):
return dt.astimezone(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
def make_uid(event):
key = f"{event['moduleDesc']}|{event['title']}|{event['start'].isoformat()}"
return hashlib.sha1(key.encode()).hexdigest() + "@timetable"
def create_ics_event(event):
return {
"uid": make_uid(event),
"summary": f"{event['moduleDesc']} - {event['title']}",
"description": f"{event['lecturer']} - {event['room']}",
"start": event["start"],
"end": event["end"],
}
def load_existing_uids(path):
uids = set()
if not os.path.exists(path):
return uids
with open(path, "r", encoding="utf-8") as f:
current_uid = None
cancelled = False
for line in f:
line = line.strip()
if line == "BEGIN:VEVENT":
current_uid = None
cancelled = False
elif line.startswith("UID:"):
current_uid = line[4:]
elif line == "STATUS:CANCELLED":
cancelled = True
elif line == "END:VEVENT" and current_uid and not cancelled:
uids.add(current_uid)
return uids
def fetch_timetable(username, password):
session = requests.Session()
session.auth = HttpNtlmAuth(username, password)
r = session.get(URL)
r.raise_for_status()
with open("response.txt", "w", encoding="utf-8") as f:
f.write(r.text)
def build_calendar():
events_js = get_events_data_from_file(RESPONSE_FILE)
parsed_events = parse_events(events_js)
new_events = [create_ics_event(e) for e in parsed_events if e]
old_uids = load_existing_uids(OLD_CALENDAR)
new_uids = {e["uid"] for e in new_events}
now = ics_time(datetime.now(timezone.utc))
lines = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
"PRODID:-//Timetable Sync//EN",
"CALSCALE:GREGORIAN",
]
for ev in new_events:
lines.extend([
"BEGIN:VEVENT",
f"UID:{ev['uid']}",
f"DTSTAMP:{now}",
f"DTSTART:{ics_time(ev['start'])}",
f"DTEND:{ics_time(ev['end'])}",
f"SUMMARY:{ev['summary']}",
f"DESCRIPTION:{ev['description']}",
"END:VEVENT",
])
for uid in old_uids - new_uids:
lines.extend([
"BEGIN:VEVENT",
f"UID:{uid}",
f"DTSTAMP:{now}",
"STATUS:CANCELLED",
"END:VEVENT",
])
lines.append("END:VCALENDAR")
with open(NEW_CALENDAR, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
# ------------------------------------------------------------
# Flask
# ------------------------------------------------------------
@app.route("/")
def index():
return render_template("index.html")
@app.route("/login-and-download", methods=["POST"])
def login_and_download():
data = request.get_json()
username = data["username"]
password = data["password"]
username = f'COVENTRY\\{username}'
try:
fetch_timetable(username, password) #request
build_calendar()
return send_file(
NEW_CALENDAR,
as_attachment=True,
download_name="timetable.ics",
mimetype="text/calendar"
)
except Exception as e:
return str(e), 400
if __name__ == "__main__":
app.run(debug=True)