School-Calendar-Import/parse_csv.py

33 lines
1.1 KiB
Python
Raw Normal View History

2025-01-20 11:15:14 -05:00
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()
2025-02-12 13:49:12 -05:00
CLASS_START = time(hour=12, minute=7)
CLASS_END = time(hour=1+12, minute=26)
2025-01-20 11:15:14 -05:00
2025-02-12 13:49:12 -05:00
math = list(reader(open('math calendar.csv', newline='')))
2025-01-20 11:15:14 -05:00
dates = []
for i in range(0, len(math), 2):
2025-02-12 13:49:12 -05:00
if i+1 >= len(math):
break
2025-01-20 11:15:14 -05:00
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
2025-02-12 13:49:12 -05:00
# year = "2025 " if "January" in math[i][k] else "2024 "
year = "2025 "
2025-01-20 11:15:14 -05:00
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))