Thursday, February 26, 2009

Google Calendar Hack

I have two Google calendars one for shared and one personal. The personal one is the default one. I was using python calendar api to fetch the events off the non default one. I had to query for events between start time and end time. There was no easy way to query non default calendar.

After finding no help on google groups. I said to myself I can fix it myself.. What the heck api code is available..


class CalendarEventQuery(gdata.service.Query):

def __init__(self, user='default', visibility='private', projection='full',
text_query=None, params=None, categories=None):
gdata.service.Query.__init__(self,
feed='http://www.google.com/calendar/feeds/%s/%s/%s' % (
urllib.quote(user),
urllib.quote(visibility),
urllib.quote(projection)),
text_query=text_query, params=params, categories=categories)

Here is the quick fix. very useful if you are using python api..I added MyCalendarEventQuery that inherits from gdata CalendarEventQuery but takes the calendar feed url as input.


class MyCalendarEventQuery(gdata.calendar.service.CalendarEventQuery):
def __init__(self,feed_url, text_query=None, params=None, categories=None):
gdata.service.Query.__init__(self,
feed= feed_url, text_query=text_query, params=params,
categories=categories)




Here is my code looks up the calendar by name. and the does range query

feed = self.cal_client.GetOwnCalendarsFeed()
for i, a_calendar in zip(xrange(len(feed.entry)), feed.entry):
if str(a_calendar.title.text).lower() == calendar_name.lower():
self.calendar_uri = a_calendar.link[0].href
self.calendar_feed = self.cal_client.GetCalendarEventFeed(
self.calendar_uri)
break


cq = MyCalendarEventQuery(feed.GetFeedLink().href)
cq.start_min = startTime
cq.start_max = endTime
cq.orderby = 'starttime'
feed = self.cal_client.CalendarQuery(cq)
while True:
for i, an_event in zip(xrange(len(feed.entry)), feed.entry):
for a_when in an_event.when:
start_time = a_when.start_time
print '\t%s. %s' % (i, an_event.title.text,)
print '\t\t%s. %s' % (i, an_event.content.text,)
print '\t\tStart time: %s' % (a_when.start_time,)
print '\t\tEnd time: %s' % (a_when.end_time,)
self.cal_client.DeleteEvent(an_event.GetEditLink().href)
if not feed.GetNextLink(): break
feed = self.cal_client.GetCalendarEventFeed(
feed.GetNextLink().href)

No comments: