Syncing iCal with Google Calendar

EDIT: This exercise isn’t necessary any more because Google Calendar now supports CalDAV. Check this out for more info.

A major caveat with Google Calendar is the absence of any CalDAV support, meaning you can distribute calendars through subscription to a feed. This is fine but still doesn’t let you edit events in a client app like Mozilla Sunbird or iCal. However it’s possible to manipulate your calendars through the GCalendar API; the Google GData talk on YouTube is particularly worth watching.

Anyway, I digress; after exhausting my SpanningSync trial and not being particularly impressed with the way it blindly syncs both sources at a predefined interval I decided to hack out my own ruby script which I could daemonize later.

The approach I took was to instantiate a WEBrick server to act as a go-between so you could ‘publish’ the calendar to localhost. As soon as a PUT comes through from a client with the iCal user agent, the request body is compared to the previous file (if it exists).

I then use the icalendar and googlecalendar gems to parse the body if a change has occurred, and push the changes to Google. The only problem here is that there doesn’t seem to be any sensible way of performing a diff on the local and remote data (and even if there were it’d be pretty bandwidth-intensive) so icalendar just iterates through every event in the old file, deletes it remotely, then adds the new one. Rinse and repeat. Or is that bleach and repeat?

I will update this soon, but this is the fruit of an hours’ labour:

http://gist.github.com/40274