0

The python module python-gdata includes an pretty comprehensive example of using python to fetch google calendar events (calenderEvents.py)
No matter what I do, I only get 25 events returned. This is the default value of the maximum number of records returned.
The example script includes these comments

In reality, the server limits the result set intially returned.  You can 
    use the max_results query parameter to allow the server to send additional
    results back (see query parameter use in DateRangeQuery for more info).

I seem to have no idea how to use the max_results query parameter and I don't understand the documenation. Wondering if someone has cracked this.

This is one of my failed attempts (failed meaning it returns only 25 events)

def _PrintAllEventsOnDefaultCalendar(self):
    """Retrieves all events on the primary calendar for the authenticated user.
    In reality, the server limits the result set intially returned.  You can 
    use the max_results query parameter to allow the server to send additional
    results back (see query parameter use in DateRangeQuery for more info).
    Additionally, you can page through the results returned by using the 
    feed.GetNextLink().href value to get the location of the next set of
    results."""
    # original line:
    #feed = self.cal_client.GetCalendarEventFeed()
    # next line is one of my attempts
    feed = self.cal_client.GetCalendarEventFeed(max_results='99')
    print 'Events on Primary Calendar: %s' % (feed.title.text,)
    for i, an_event in zip(xrange(len(feed.entry)), feed.entry):
      print '\t%s. %s' % (i, an_event.title.text,)
      for p, a_participant in zip(xrange(len(an_event.who)), an_event.who):
        print '\t\t%s. %s' % (p, a_participant.email,)
        print '\t\t\t%s' % (a_participant.name,)
        if a_participant.attendee_status:
          print '\t\t\t%s' % (a_participant.attendee_status.value,)
1
Contributor
1
Reply
3
Views
6 Years
Discussion Span
Last Post by timrichardson
0

I added this function tothe CalendarExample class defined in calendarExample.py (a sample file installed along with the Google python modules)

def GetDateRangeQuery(self, start_date='2007-01-01', end_date='2007-07-01'):
    """Retrieves events from the server which occur during the specified date
    range.  This uses the CalendarEventQuery class to generate the URL which is
    used to retrieve the feed.  Returns an array of events. For more information on valid query parameters,
    see: http://code.google.com/apis/calendar/reference.html#Parameters"""

    print 'Date range query for events on Primary Calendar: %s to %s' % (
        start_date, end_date,)
    query = gdata.calendar.service.CalendarEventQuery('default', 'private', 
        'full')
   
    query.start_min = start_date
    query.start_max = end_date
    query.max_results='999'
   
    feed = self.cal_client.CalendarQuery(query)
    return feed.entry

and then I can batch delete calendar entries with the following python:

#!/usr/bin/python
#delete events from a google calendar

from calendarExample import CalendarExample
import time


calendar = CalendarExample('<your google email address>', '<your google password>')

allEvents = calendar._DateRangeQuery(start_date='2010-08-31',end_date='2099-07-31')
allEvents = calendar.GetDateRangeQuery(start_date='2010-08-31',end_date='2099-07-31')
print(len(allEvents))
x=0
for event in allEvents:
        calendar._DeleteEvent(event)
        time.sleep(1) #this seems to stop problems
        x+=1

There may be problems with gmail timeouts. Keep running the script until all desire events are deleted. The 1 second sleep avoids these timeout problems.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.