Welcome to bytebang » The blog about all and nothing » Fetching google calendar events with python

Fetching google calendar events with python

Mar 08 2015

The problem

Google calendar is a wonderful tool. In the last year my glider club managed it to switch away from our old paper based calentar towards a google calendar which is also integrated into our homepage. While this setup is working very well we discovered that we need a prinout of todays events for the daily briefing of our pilots and for daily event management. The previous version of this daily summary was printed automatically via a reciept printer in the morning and looked like this:


In the past i was fetching the calendar events with a tool called googlecl which aims to be a commandline client for a few google products. In November 2014 google switched off its v2 Calendar API and since then the googlecl tool was unable to fetch data from your calendars. Till now it is not possible to get data out of the google service with the tool that you get with the googlecl package from the ubuntu repositories.

$ google calendar list

Failed to get entries: {'status': 403, 'body': '<HTML>
<H2>Error 403</H2>
</HTML>', 'reason': 'Forbidden'}

This problem is already known to the google-cl developers, but the solution did not make it into the ubuntu package yet. However - i had to find another solution how to fetch the data.

The solution

After an hour of googling i discovered that the google calendar can be queried with a plain curl request:

$ curl https://loginname:password@www.google.com/calendar/dav/g4mbr6al674vrp46uund9stsh4@group.calendar.google.com/events
PRODID:-//Google Inc//Google Calendar 70.9054//EN

This works only with https requests. The loginname and the username have to be replaced with the google account login credentials who has access to the calendar, and the g4mbr6al674vrp46uund9stsh4@group.calendar.google.com is the calendar id.

So far so good. The data can be parsed with any library that understands the caldav format. Fortunately i found such a library for python: caldav. It is not available via the ubuntu repositories, so i had to install it by hand:

$sudo apt-get install build-essential python-dev python-setuptools libxml2 libxslt1-dev
$wget https://pypi.python.org/packages/source/c/caldav/caldav-0.2.2.tar.gz
$tar -xzvf ./caldav-0.2.2.tar.gz
$cd caldav-0.2.2
$sudo python setup.py install 

From there on you can use it in your python scripts like this:


import datetime
import caldav
from caldav.elements import dav, cdav

username = "myusername"
password = "mypassword"
calendarname = "Maintainance"
calenderid = "something@group.calendar.google.com"

# OK, lets go
url = "https://" + username + ":" + password + "@www.google.com/calendar/dav/" + calenderid + "/events"
# Setup Client
client = caldav.DAVClient(url)

# Fetch calendar
cal = client.principal().calendar(name=calenderid)

# Fetch todays events
events = cal.date_search(datetime.date.today(), datetime.date.today() + datetime.timedelta(days=1))

# Get the events and push them to stdout
for event in events:
    e = event.instance.vevent
   print "(" + e.dtstart.value.strftime("%H:%M") + ") " + e.summary.value

Super simple stuff! This snipplet doesnt show the full capabilities of the library, but it fits my needs perfectly.

Get Social

(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen