Created
December 10, 2017 19:39
-
-
Save drdrang/739f862035ff35119fcd634444a432b5 to your computer and use it in GitHub Desktop.
Clean up Southwest Airlines ICS files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from icalendar import Calendar | |
import sys | |
import copy | |
from datetime import timedelta | |
for ics in sys.argv[1:]: | |
# Open the ics file and extract the event and alarm. | |
cal = Calendar.from_ical(open(ics).read()) | |
event = cal.walk('vevent')[0] | |
alarm = event.walk('valarm')[0] | |
# Make no changes if summary doesn't contain "Southwest Airlines". | |
if 'Southwest Airlines' not in event['summary']: | |
continue | |
# The last word in the location is the flight number. | |
# The last word in the summary is the confirmation number. | |
flight = event['location'].split()[-1] | |
confirmation = event['summary'].split()[-1] | |
# Erase the event's verbose description and rewrite its summary. | |
event['description'] = '' | |
event['summary'] = 'SW {} ({})'.format(flight, confirmation) | |
# Get rid of the mistaken MS fields. | |
for e in ('x-microsoft-cdo-alldayevent', 'x-microsoft-cdo-busystatus'): | |
try: | |
del event[e] | |
except KeyError: | |
continue | |
# Set the alarm to 24 hours, 15 minutes before the flight, rewrite its | |
# description, and make it audible. | |
alarm['trigger'].dt = timedelta(days=-1, minutes=-15) | |
alarm['description'] = 'Check in SW {} ({})'.format(flight, confirmation) | |
alarm['action'] = 'AUDIO' | |
alarm.add('ATTACH;VALUE=URI', 'Basso') | |
# Delete any UIDs before copying. | |
for u in ('uid', 'x-wr-alarmuid'): | |
try: | |
del alarm[u] | |
except KeyError: | |
continue | |
# Add a new alarm 24 hours, 5 minutes before the flight by copying | |
# the previous alarm and changing its trigger time. | |
newalarm = copy.deepcopy(alarm) | |
newalarm['trigger'].dt = timedelta(days=-1, minutes=-5) | |
event.add_component(newalarm) | |
# Write the changes back to the original file. | |
f = open(ics, 'w') | |
f.write(cal.to_ical()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment