I’ve started looking into means of connecting external google calendars into keeners.org, and found it took longer than anticipated just to get the appropriate tools in place to get started. Importing google calendars, which I had assumed to be well documented is really lacking. The goal was to not only capture occurrences of events but to capture the recurrence rrules and datetimes that define them. I downloaded the python gdata package (gdata + atom) and began hacking at a public calendar. It turns out there’s no apparent mechanism in gdata to parse the iCalendar data itself. After considering parsing out the ical data with regular expressions, I thought better and started looking around. There’s a vobject package that seems to do the trick.
So.. Here’s some basic noodling with the two libraries, I’ve found that there’s plenty of documentation on connecting to and collecting google calendars, but little on the actual import of the data – so here’s the missing link, it requires the gdata and vobject python packages listed above.
from gdata.calendar import service
import vobject
cs = service.CalendarService()
cs.email = 'anygoogleaccount@example.com'
cs.password = 'google_password'
cs.ProgrammaticLogin()
calendar_uri = '/calendar/feeds/5hqt8fv2s3smdn4rnua5pd6hbc%40group.calendar.google.com/public/full'
feed = cs.GetCalendarEventFeed(uri=calendar_uri)
print feed.entry
# [<gdata.calendar.CalendarEventEntry object at 0x03390550>,
# <gdata.calendar.CalendarEventEntry object at 0x03390590>,
# <gdata.calendar.CalendarEventEntry object at 0x033909B0>]
recurrence_text = feed.entry[0].recurrence.text
recurrence = vobject.readOne(recurrence_text)
recurrence.prettyPrint()
# DTEND: 20091213T210000
# params for DTEND:
# TZID [u'America/New_York']
# DTSTART: 20091213T200000
# params for DTSTART:
# TZID [u'America/New_York']
# RRULE: FREQ=DAILY;WKST=SU
# VTIMEZONE
# TZID: America/New_York
# DAYLIGHT
# DTSTART: 19700308T020000
# TZOFFSETFROM: -0500
# TZNAME: EDT
# TZOFFSETTO: -0400
# RRULE: FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
#
# STANDARD
# DTSTART: 19701101T020000
# TZOFFSETFROM: -0400
# TZNAME: EST
# TZOFFSETTO: -0500
# RRULE: FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
#
# X-LIC-LOCATION: America/New_York
dt_start = recurrence.contents['dtstart'][0]
print dt_start.prettyPrint()
# DTSTART: 20091213T200000
# params for DTSTART:
# TZID [u'America/New_York']
print dt_start.params
# {u'TZID': [u'America/New_York']}
print dt_start.params['TZID'][0]
# u'America/New_York'
print recurrence.contents['rrule'][0].value
# u'FREQ=DAILY;WKST=SU'
print recurrence.contents['dtstart'][0].value
# u'20091213T200000'
