Skip to content

Instantly share code, notes, and snippets.

@pjflanagan
Created August 25, 2021 18:53
Show Gist options
  • Save pjflanagan/b1bfb05c520496709ce447291fb84dff to your computer and use it in GitHub Desktop.
Save pjflanagan/b1bfb05c520496709ce447291fb84dff to your computer and use it in GitHub Desktop.
Python code to manually remove events from a calendar (best used for large birthday calendars)
CAL_START = """
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Birthdays
X-WR-TIMEZONE:America/New_York
X-WR-CALDESC:Imported from Facebook
BEGIN:VTIMEZONE
TZID:America/New_York
X-LIC-LOCATION:America/New_York
BEGIN:DAYLIGHT
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
END:STANDARD
END:VTIMEZONE
"""
CAL_END = """
END:VCALENDAR
"""
lines = []
with open("oldCalendar.ics", 'r') as fp:
lines = fp.readlines()
keepEvents = []
newCalendar = []
lineHold = []
recordEvent = False
for line in lines:
# if we see the begining of an event start recording
if "BEGIN:VEVENT" in line:
recordEvent = True
# if we are recording then record the line
if recordEvent:
lineHold.append(line)
# if we see the end of an event, add the lineHold and stop recording
if "END:VEVENT" in line:
newCalendar += lineHold
lineHold = []
recordEvent = False
# if we see the name, ask if we want to keep
if "SUMMARY:" in line:
friendName = line.replace("SUMMARY:","").replace("\n", "")
userIn = input("Type anything to remove [" + friendName + "] to the calendar: ")
# if the user enters anything but a blank line, remove the user
if userIn != "":
lineHold = []
recordEvent = False
else:
keepEvents.append(friendName)
with open("keepEvents.txt", "w") as fp:
fp.write("\n".join(keepEvents))
with open("newCalendar.ics", "w") as fp:
fp.write(CAL_START + "".join(newCalendar) + CAL_END)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment