Skip to content

Instantly share code, notes, and snippets.

@justinrporter
Last active January 19, 2022 22:18
Show Gist options
  • Save justinrporter/92f88712934a9fa85f63274e2b989473 to your computer and use it in GitHub Desktop.
Save justinrporter/92f88712934a9fa85f63274e2b989473 to your computer and use it in GitHub Desktop.
Make my mangled, Keystone-generated calendars fit for human consumption.
import datetime
# you can get this with 'pip install icalendar'
from icalendar import Calendar, vDate
# load a file called 'capstone.ics'
# you can get this by opening your calendar feed link in your browser
with open('capstone.ics','rb') as f:
cal = Calendar.from_ical(f.read())
cal_filtered = Calendar()
# get rid of all the events after 2/25/2022, but keep if
# it has the urlhttps://keystone.wustl.edu/events?id=4760
for sc in cal.subcomponents:
if sc.name == 'VEVENT':
dt = sc['DTSTART'].dt
if dt < datetime.datetime(year=2022, month=2, day=25, tzinfo=dt.tzinfo):
cal_filtered.add_component(sc)
elif sc['URL'] == 'https://keystone.wustl.edu/events?id=4760':
cal_filtered.add_component(sc)
else:
cal_filtered.add_component(sc)
# rewrite all the mangled all-day events as proper
# all day events using the vDate property (rather
# than the vDatetime property)
cal_normalized = Calendar()
for sc in cal_filtered.subcomponents:
if sc.name == 'VEVENT':
dt_start = sc['DTSTART'].dt
dt_end = sc['DTEND'].dt
if dt_start + datetime.timedelta(days=1) == dt_end:
sc['DTSTART'] = vDate(dt_start.date())
sc['DTEND'] = vDate(dt_start.date())
cal_normalized.add_component(sc)
else:
cal_normalized.add_component(sc)
# write the output file to disk as 'cal_filtered.ics'
with open('cal_filtered.ics', 'wb') as f:
f.write(cal_normalized.to_ical())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment