School-Calendar-Import/parse_txt.py

36 lines
1 KiB
Python
Raw Permalink Normal View History

from json import load
from datetime import time, datetime, timedelta
from ical.calendar import Calendar
from ical.event import Event
from ical.calendar_stream import IcsCalendarStream
from pathlib import Path
calendar = Calendar()
CLASS = "compsci"
CLASS_START = time(hour=1+12, minute=30)
CLASS_END = time(hour=2+12, minute=45)
this_monday = datetime.today()
if datetime.now().weekday == 4: # is a friday, go to next week
this_monday += timedelta(days=-this_monday.weekday(), weeks=1) # black magic
else: # not a friday, go to this week
this_monday -= timedelta(days=this_monday.weekday()) # black magic
print(this_monday)
weekday = 0
for title in Path("compsci.txt").read_text().splitlines():
day = this_monday + timedelta(days=weekday)
weekday += 1
if title.strip == "":
continue
calendar.events.append(
Event(summary=title, start=datetime.combine(day, CLASS_START), end=datetime.combine(day, CLASS_END))
)
with open(f"{CLASS}.ics", "w") as ics_file:
ics_file.write(IcsCalendarStream.calendar_to_ics(calendar))