School-Calendar-Import/parse_csv.py

33 lines
No EOL
1.1 KiB
Python

from csv import reader
from datetime import date, time, datetime
from ical.calendar import Calendar
from ical.event import Event
from ical.calendar_stream import IcsCalendarStream
from re import sub
def cleanup(text):
return sub(r' {2,}', " ", text.replace("\t", " ")).strip().replace("Quiz", "Quiz +")
calendar = Calendar()
CLASS_START = time(hour=12, minute=7)
CLASS_END = time(hour=1+12, minute=26)
math = list(reader(open('math calendar.csv', newline='')))
dates = []
for i in range(0, len(math), 2):
if i+1 >= len(math):
break
for k in range(len(math[i])):
event = cleanup(math[i+1][k])
if event.lower() == "pa day" or event.lower() == "pd day" or "break" in event.lower():
continue
# year = "2025 " if "January" in math[i][k] else "2024 "
year = "2025 "
day = datetime.strptime(year + math[i][k], "%Y %B %d").date()
calendar.events.append(
Event(summary=event, start=datetime.combine(day, CLASS_START), end=datetime.combine(day, CLASS_END))
)
with open("math.ics", "w") as ics_file:
ics_file.write(IcsCalendarStream.calendar_to_ics(calendar))