from icalendar import Calendar, Event
from datetime import datetime, timedelta
import pytz
# Define the start and end dates for the schedule
start_date = datetime(2024, 9, 25)
end_date = datetime(2024, 12, 10)
current_date = start_date
# Define the timezone
timezone = pytz.timezone("America/Vancouver")
# Create a calendar
cal = Calendar()
# Function to add an event to the calendar
def add_event(cal, summary, start_time, end_time, recurrence_rule=None):
event = Event()
event.add('summary', summary)
event.add('dtstart', start_time)
event.add('dtend', end_time)
event.add('dtstamp', datetime.now(timezone))
if recurrence_rule:
event.add('rrule', recurrence_rule)
cal.add_component(event)
# Monday to Thursday schedule
monday_to_thursday = [
{"summary": "Study/Review Course Materials", "start": timedelta(hours=16, minutes=30), "end": timedelta(hours=17, minutes=30)},
{"summary": "Online Class", "start": timedelta(hours=17, minutes=30), "end": timedelta(hours=19, minutes=0), "days": ["MO", "TU"]},
{"summary": "Dinner and Unwind", "start": timedelta(hours=18, minutes=30), "end": timedelta(hours=19, minutes=30), "days": ["MO", "TU"]},
{"summary": "Study/Work on Assignments", "start": timedelta(hours=19, minutes=30), "end": timedelta(hours=21, minutes=0)},
{"summary": "Leisure Time", "start": timedelta(hours=21, minutes=0), "end": timedelta(hours=22, minutes=0)},
{"summary": "Wind Down and Prepare for Bed", "start": timedelta(hours=22, minutes=0), "end": timedelta(hours=22, minutes=30)}
]
# Friday schedule
friday = [
{"summary": "Study/Work on Assignments", "start": timedelta(hours=16, minutes=30), "end": timedelta(hours=18, minutes=0)},
{"summary": "Free Time", "start": timedelta(hours=18, minutes=0), "end": timedelta(hours=23, minutes=59)}
]
# Saturday schedule
saturday = [
{"summary": "Study/Work on Assignments", "start": timedelta(hours=8, minutes=0), "end": timedelta(hours=12, minutes=0)},
{"summary": "Study/Work on Assignments or Leisure", "start": timedelta(hours=12, minutes=30), "end": timedelta(hours=18, minutes=0)},
{"summary": "Free Time", "start": timedelta(hours=18, minutes=0), "end": timedelta(hours=23, minutes=59)}
]
# Sunday schedule
sunday = [
{"summary": "Study/Work on Assignments", "start": timedelta(hours=8, minutes=0), "end": timedelta(hours=12, minutes=0)},
{"summary": "Study/Work on Assignments", "start": timedelta(hours=12, minutes=30), "end": timedelta(hours=16, minutes=0)},
{"summary": "Leisure Time", "start": timedelta(hours=16, minutes=0), "end": timedelta(hours=18, minutes=0)},
{"summary": "Meal Prep for the Week", "start": timedelta(hours=18, minutes=0), "end": timedelta(hours=20, minutes=0)},
{"summary": "Leisure Time", "start": timedelta(hours=20, minutes=0), "end": timedelta(hours=22, minutes=0)},
{"summary": "Wind Down and Prepare for Bed", "start": timedelta(hours=22, minutes=0), "end": timedelta(hours=22, minutes=30)}
]
# Recurrence rules
weekly_recurrence = {"freq": "weekly", "until": end_date}
# Create events for Monday to Thursday
for day in ["MO", "TU", "WE", "TH"]:
for activity in monday_to_thursday:
recurrence_rule = weekly_recurrence if "days" not in activity or day in activity["days"] else None
start_time = current_date + activity["start"]
end_time = current_date + activity["end"]
add_event(cal, activity["summary"], timezone.localize(start_time), timezone.localize(end_time), recurrence_rule)
# Create events for Friday
current_date = start_date + timedelta(days=(4 - start_date.weekday()) % 7) # Move to the next Friday
for activity in friday:
start_time = current_date + activity["start"]
end_time = current_date + activity["end"]
add_event(cal, activity["summary"], timezone.localize(start_time), timezone.localize(end_time), weekly_recurrence)
# Create events for Saturday
current_date = start_date + timedelta(days=(5 - start_date.weekday()) % 7) # Move to the next Saturday
for activity in saturday:
start_time = current_date + activity["start"]
end_time = current_date + activity["end"]
add_event(cal, activity["summary"], timezone.localize(start_time), timezone.localize(end_time), weekly_recurrence)
# Create events for Sunday
current_date = start_date + timedelta(days=(6 - start_date.weekday()) % 7) # Move to the next Sunday
for activity in sunday:
start_time = current_date + activity["start"]
end_time = current_date + activity["end"]
add_event(cal, activity["summary"], timezone.localize(start_time), timezone.localize(end_time), weekly_recurrence)
# Save the calendar to a file
with open('schedule_sept25_to_dec10.ics', 'wb') as f:
f.write(cal.to_ical())
print("iCal file 'schedule_sept25_to_dec10.ics' created successfully!")