Skip to content

Instantly share code, notes, and snippets.

@johnfosborneiii
Last active April 29, 2024 15:34
Show Gist options
  • Save johnfosborneiii/3558b70a7c2ba2d0008d82facb3d418a to your computer and use it in GitHub Desktop.
Save johnfosborneiii/3558b70a7c2ba2d0008d82facb3d418a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# This library requires requests and icalendar
# pip install icalendar
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from pytz import timezone, utc
# Prefix for event summaries
event_prefix = "CG: "
# Load your calendar
with open('[email protected]', 'rb') as f:
cal = Calendar.from_ical(f.read())
# Get today's date in EST
est = timezone('US/Eastern')
today = datetime.now(est).date()
# Determine the end date (next Sunday)
if today.weekday() == 6: # Sunday
end_date = today
else:
end_date = today + timedelta(days=(6 - today.weekday()))
# Function to convert datetime to EST
def to_est(dt):
return dt.astimezone(est)
# Iterate over each day from today to Sunday
current_day = today
while current_day <= end_date:
# Collect events for the current day
events = []
for component in cal.walk():
if component.name == "VEVENT":
start_dt = component.get('dtstart').dt
if type(start_dt) is datetime:
start_dt = to_est(start_dt)
if start_dt.date() == current_day:
summary = component.get('summary')
events.append((start_dt, summary))
# Sort events by start time in reverse order
events.sort(key=lambda x: x[0], reverse=True)
# Print a newline for visual separation
print()
# Print the day of the week
print(current_day.strftime("%A"))
# Print sorted events for the current day
for event in events:
time_str = event[0].strftime('%I%M%p').lstrip('0')
if time_str[1:4] == ":00": # Checks if it ends with ":00"
time_str = time_str[0] + time_str[4:] # Removes the ":00"
print(f"- {time_str}: {event_prefix}{event[1]}")
# Move to the next day
current_day += timedelta(days=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment