Accessing Google Calendar with Python

From PeformIQ Upgrade
Jump to navigation Jump to search

Overview

Google provides a Python module to interact with Google Calendar

A sample script is provided below

Script

#!/usr/bin/env python

from GoogleCalendar import *

gCalMNG = GoogleCalendarMng()
gCalMNG.connect ("some_login@gmail.com", "some_password")

calendar = gCalMNG.getCalendar ("myCalendarForTest")

events = calendar.getEvents()

for event in events:
    print event.getTitle()
    print event.getContent()

    print time.strftime("%Y-%m-%dT%H:%M:%S" ,
                         time.localtime(event.getStartTime()))

    print time.strftime("%Y-%m-%dT%H:%M:%S" ,
                         time.localtime(event.getEndTime()))

#----- Adding new events -----------------------------------------------------------------------
# There are two mechanisms:

#  - Creating a single event, and adding this one into calendar.

ev = newEvent("Lluis", "lluis.gesa@gmail.org",
              "Meeting", "With Aleix to talk about work", "La Garriga",
               time.mktime((2007,03,27,19,30,00)),
               time.mktime((2007,03,27,21,30,00)))

calendar.addEvent (ev)

#  - Creating directly the event into calendar:

calendar.newEvent ("Meeting", "With Aleix to talk about work", "La Garriga",
                    time.mktime((2007,03,27,19,30,00)),
                    time.mktime((2007,03,27,21,30,00)))

# with this second mechanisms, the user and email used are the same of owner of calendar

#----- Updating events -----------------------------------------------------------

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
        event.setContent (event.getContent() + "Changes on location")
        event.setLocation ("Barcelona")
        event.update()

#----- Deleting events -----------------------------------------------------------

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
        event.delete()

#----- Adding comments ------------------------------------------------------------
#  Like events, there are two methods:
#  -  Directly over event

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
         event.newComment ("comment test")

#  - Using  a global method for create.

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
         cmt = newComment ("some_user_mail", "some_email",  "Coment test2")
         event.addComment (cmt)

#----- Updating comments -----------------------------------------------------

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
       comments = event.getComments()
       for comment in comments:
           if comment.getContent() == "comment test":
               comment.setContent("Updated")
               comment.update()

#----- Deleting comments -----------------------------------------------------

events = calendar.getEvents()

for event in events:
    if event.getTitle() == "Meeting":
       comments = event.getComments()
       for comment in comments:
           if comment.getContent() == "Updated":
               comment.delete()