Fixing airline .ics files

For several years now, airlines have been providing iCalendar files to their customers so they can easily add their upcoming flights to their computer/PDA/phone calendars. Unfortunately, the information in these .ics files is often not organized in a way that’s compact and easy to read at a glance. Today, after scheduling a couple of flights, I got tired of hand-editing the entries in my calendar program and wrote a script that’ll do it for me before I import them.

I usually fly Southwest, which lets you download .ics files through links on its website.

Southwest calendar popup

The problem with Southwest’s calendar entries is that they hide the information I want to see front and center. The summary field is the one that appears most prominently in calendar programs; unfortunately, Southwest fills it with crap:

Southwest Airlines Confirmation F5309V

I like having the confirmation number in the summary (it’s handy when you need to call to change a reservation), but the most important thing to have there is the flight number. Southwest puts that in the location field.

MDW to JNY on Flight(s) 867

There’s some logic to having the airport codes in the location field, but many calendar programs show that only in the detailed view of an entry. I don’t want to have to scroll and click and tap just to see the flight number.

When I do look at the detailed view, Southwest’s description field dominates the screen with a lot of useless marketing.

Thank you for purchasing travel on Southwest Airlines! Below are your itinerary details and helpful links to online checkin, flight status messaging, and more. To obtain your boarding pass, check in online starting 24 hours before your scheduled departure time. For your convenience, we already set a reminder for this flight.

NEED TO RENT A CAR OR BOOK A HOTEL? Be sure to visit http://travel.southwest.com/specialoffers to see our latest

It goes on like that until your eyes glaze over and you miss the tiny tidbits of actual usable information. I’d rather not be distracted by it.

One thing I do like about the Southwest calendar entries is that they include an alarm set to go off 24 hours before your flight. This reminds you to check in soon as you can to help you get a good seat.1 I wish, though, that it gave just a little bit more notice.

The iCalendar format is just a text file with items arranged in a particular way. It’s easy to build scripts that write to the format, but because the format has lots of options—the RFC is 40,000 words long—I wasn’t about to try to parse it without help.

Help came in the form of the Python icalendar library, which has methods for reading, parsing, and writing iCalendar files. Its documentation stinks, but after some interactive experimentation in IPython, I understood enough to write the following script, named swicsfix:

python:
 1:  #!/usr/bin/python
 2:  
 3:  from icalendar import Calendar
 4:  import sys
 5:  from datetime import timedelta
 6:  
 7:  for ics in sys.argv[1:]:
 8:    # Open the ics file and extract the event and alarm.
 9:    cal = Calendar.from_ical(open(ics).read())
10:    event = cal.walk('vevent')[0]
11:    alarm = event.walk('valarm')[0]
12:  
13:    # The last word in the location is the flight number.
14:    # The last word in the summary is the confirmation number.
15:    flight = event['location'].split()[-1]
16:    confirmation = event['summary'].split()[-1]
17:  
18:    # Erase the event's verbose description and rewrite its summary.
19:    event['description'] = ''
20:    event['summary'] = 'SW %s (%s)' % (flight, confirmation)
21:  
22:    # Set the alarm to 24 hours, 5 minutes before the flight and
23:    # rewrite its description.
24:    alarm['trigger'].dt = timedelta(days=-1, minutes=-5)
25:    alarm['description'] = 'Check in SW %s (%s)' % (flight, confirmation)
26:  
27:    # Write the changes back to the original file.
28:    f = open(ics, 'w')
29:    f.write(cal.to_ical())
30:    f.close()

The script expects a series of .ics file names on the command line. It goes through each in turn and changes certain fields to match my tastes. The long description field is erased, and the summary field becomes

SW 867 (F5309V)

which has everything I need to know and nothing I don’t. The alarm is set to 24 hours, 5 minutes before flight time. The extra 5 minutes gives me a chance to prepare myself so I can check in as soon as the 24-hour window opens. This is not a big deal, but I figured as long as I was changing the entry, I might as well change the alarm, too. Similarly, I changed the alert text from the verbose

It’s time to check in for your Southwest Airlines Flight - Confirmation #F5309V

to the more punchy

Check in SW 867 (F5309V)

I like to think the comments are good enough to explain the overall flow of the script, but a couple of lines could probably use a little more detail. When the icalendar library parses an .ics file, it puts it into a data structure that’s recursive. Digging through the layers of the data structure is not straightforward, even for simple entries. That’s why I used the walk method in Lines 10 and 11: it’s the easiest way to get the event (vevent) and alarm (valarm) components. The walk method returns a list of all the components of the given type. For Southwest’s .ics files, there’s only one event and one alarm, so I know that what I want is the first item from each list.

As you can see from Lines 27–30, swicsfix overwrites the original file with the new items. This is a little dangerous, but it’s not the end of the world if the summary field of an .ics file gets screwed up. It’s certainly more convenient not to have two versions of each file on my computer.

Although swicsfix can be used from the command line, I’ll probably prefer to convert Southwest’s .ics files directly from the Finder. For that I made a Service in Automator:

SW ics fix service in Automator

The one-line script in the box is

~/Dropbox/bin/swicsfix "$@"

where ~/Dropbox/bin is the folder where I keep swicsfix and most of my other scripts. With this Service, I can right-click on the .ics files in the Finder and convert them with Services‣SW ics fix.

Other airlines have their own idiosyncrasies and will need their own scripts. Because I fly Southwest far more than any other airline, I doubt it’s worth my time to adapt swicsfix to other airlines, but it should be fairly easy to do.

And swicsfix itself might need to be altered. I’ve only had direct flights to test it on; multi-legged flights have more than one flight number, and I have no idea how the location field is formatted for that situation. While I do my best to avoid connections, eventually I’ll run into one and will edit swicsfix accordingly.


  1. If you’re a Southwest flier, you understand what this means. If you’re not, I don’t want to try to explain it here.