Hello Danilo,

I've tweaked your scripts a little bit to use the latest version for the 
Client Library (you might need to download the source directly, see link at 
the end of this post) and to use batch requests to send the delete requests.

[SCRIPT]
try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.data
import gdata.calendar.client
import gdata.acl.data
import atom.data
import getopt
import sys
import string
import time


def DeleteRequest(calendar_client, feed):
  """Execute a batch request to delete entries."""

  success = 0
  failure = 0

  print 'Executing batch request to delete %d entries.' % len(feed.entry)
  # feed that holds all the batch request entries
  delete_feed = gdata.calendar.data.CalendarEventFeed()

  for an_event in feed.entry:
    an_event.batch_id = gdata.data.BatchId(text='delete-request-%s' % 
an_event.id.text)
    delete_feed.AddDelete(entry=an_event)

  try:
    response_feed = calendar_client.ExecuteBatch(delete_feed, 
gdata.calendar.client.DEFAULT_BATCH_URL)
  except gdata.client.RedirectError:
    print 'Delete request failure: too many redirect'
    return (0, len(feed.entry))

  # iterate the response feed to get the operation status
  for entry in response_feed.entry:
    if entry.batch_status.code != '200':
      print '\tbatch id: %s' % (entry.batch_id.text,)
      print '\t-> status: %s' % (entry.batch_status.code,)
      print '\t-> reason: %s' % (entry.batch_status.reason,)
      failure += 1
    else:
      success += 1
  return (success, failure)


def GetNextFeed(calendar_client, feed, delete, query, calendar):
  """Get the next link."""
  if delete:
    # Since we are deleting entries, just send a fresh request.
    feed = 
calendar_client.GetCalendarEventFeed(calendar_client.GetCalendarEventFeedUri(calendar=calendar),
 
q=query)
    if len(feed.entry) == 0:
      feed = None  
  else:
    if feed.GetNextLink():
      try:
        feed = calendar_client.GetCalendarEventFeed(feed.GetNextLink().href)
      except gdata.client.RedirectError:
        print 'Request failure: too many redirect'
        feed = None
    else:
      feed = None
  return feed


# The function we'll use to delete the relevant calendar entries
def DeleteEventEntries(calendar_client, calendar='default', 
start_date='2010-01-01', end_date='2010-12-31', pattern="", delete=False):
  print 'Date range query for events on %s Calendar: %s to %s' % (calendar, 
start_date, end_date,)
  query = gdata.calendar.client.CalendarEventQuery(text_query=pattern, 
start_min=start_date, start_max=end_date, max_results=25)
  query.start_min = start_date
  query.start_max = end_date

  try:
    feed = 
calendar_client.GetCalendarEventFeed(calendar_client.GetCalendarEventFeedUri(calendar=calendar),
 
q=query)
  except gdata.client.RedirectError:
    print 'Request failure: too many redirect'
    return

  total_results = feed.total_results.text.strip()
  print "Total results: %s" % (total_results,)
  i = 1
  success = 0
  failure = 0
  while True:
    for an_event in feed.entry:
      print '\t%s/%s. %s (%s)' % (i, total_results, 
an_event.title.text,an_event.id.text.strip(),)
      i += 1
    if delete:
      result = DeleteRequest(calendar_client, feed)
      success += result[0]
      failure += result[1]
    feed = GetNextFeed(calendar_client=calendar_client, feed=feed, 
delete=delete, query=query, calendar=calendar) 
    if feed is None:
      print 'Done'
      break;
  print 'Total entries: %s - Proceesed: %i - Success: %i - Failure %i' % 
(total_results, i - 1, success, failure)


def main():
  # parse command line options
  try:
    opts, args = getopt.getopt(sys.argv[1:], "", ["user=", "pw=", "delete=", 
"calendar=", "pattern=", "start=", "end="])
  except getopt.error, msg:
    print ('python calendarDelete.py --user [username] --pw [password] ' +
        '--delete [true|false] --calendar [calendar] --pattern [pattern] ' +
        '--start [start_date] --end [end_date]')
    sys.exit(2)

  user = ''
  pw = ''
  delete = False
  calendar = 'default'
  start_date = '2010-01-01'
  end_date = '2010-12-31'
  pattern = ''

  # Process options
  for o, a in opts:
    if o == '--user':
      user = a
    elif o == '--pw':
      pw = a
    elif o == '--delete':
      delete = a == 'true'
    elif o == '--calendar':
      calendar = a
    elif o == '--pattern':
      pattern = a
    elif o == '--start':
      start_date = a
    elif o == '--end':
      end_date = a

  if user == '' or pw == '':
    print ('python calendarDelete.py --user [username] --pw [password] ' +
        '--delete [true|false] --calendar [calendar] --pattern [pattern]' +
        '--start [start_date] --end [end_date]')
    sys.exit(2)

  calendar_client = 
gdata.calendar.client.CalendarClient(source='Event-Deletion-1.0')
  calendar_client.ClientLogin(user, pw, calendar_client.source);

  DeleteEventEntries(calendar_client=calendar_client, calendar=calendar, 
start_date=start_date, end_date=end_date, pattern=pattern, delete=delete);

if __name__ == '__main__':
  main()
[/SCRIPT]

You can run the script as follow:

[COMMAND]
python delete_entries.py --user <username> --pw <password> --start 
2010-01-01 --end 2010-12-31 --pattern <pattern> --delete [true|false]
[/COMMAND]

If you run it with "--delete true", this will delete the entries, otherwise, 
it will just list the entries that has been returned by the API.

As I mentioned, you might need to download the missing dependencies here:
  - get the whole 
project: http://code.google.com/p/gdata-python-client/source/checkout
  - just get the missing files (calendar/client.py and 
calendar/data.py): 
http://code.google.com/p/gdata-python-client/source/browse/#hg%2Fsrc%2Fgdata%2Fcalendar

I hope this helped!

Best,
Alain

-- 
You received this message because you are subscribed to the Google
Groups "Google Calendar Data API" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://code.google.com/apis/calendar/community/forum.html
try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.data
import gdata.calendar.client
import gdata.acl.data
import atom.data
import getopt
import sys
import string
import time


def DeleteRequest(calendar_client, feed):
  """Execute a batch request to delete entries."""

  success = 0
  failure = 0

  print 'Executing batch request to delete %d entries.' % len(feed.entry)
  # feed that holds all the batch request entries
  delete_feed = gdata.calendar.data.CalendarEventFeed()

  for an_event in feed.entry:
    an_event.batch_id = gdata.data.BatchId(text='delete-request-%s' % an_event.id.text)
    delete_feed.AddDelete(entry=an_event)

  try:
    response_feed = calendar_client.ExecuteBatch(delete_feed, gdata.calendar.client.DEFAULT_BATCH_URL)
  except gdata.client.RedirectError:
    print 'Delete request failure: too many redirect'
    return (0, len(feed.entry))

  # iterate the response feed to get the operation status
  for entry in response_feed.entry:
    if entry.batch_status.code != '200':
      print '\tbatch id: %s' % (entry.batch_id.text,)
      print '\t-> status: %s' % (entry.batch_status.code,)
      print '\t-> reason: %s' % (entry.batch_status.reason,)
      failure += 1
    else:
      success += 1
  return (success, failure)


def GetNextFeed(calendar_client, feed, delete, query, calendar):
  if delete:
    # Since we are deleting entries, just send a fresh request.
    feed = calendar_client.GetCalendarEventFeed(calendar_client.GetCalendarEventFeedUri(calendar=calendar), q=query)
    if len(feed.entry) == 0:
      feed = None  
  else:
    if feed.GetNextLink():
      try:
        feed = calendar_client.GetCalendarEventFeed(feed.GetNextLink().href)
      except gdata.client.RedirectError:
        print 'Request failure: too many redirect'
        feed = None
    else:
      feed = None
  return feed


# The function we'll use to delete the relevant calendar entries
def DeleteEventEntries(calendar_client, calendar='default', start_date='2010-01-01', end_date='2010-12-31', pattern="", delete=False):
  print 'Date range query for events on %s Calendar: %s to %s' % (calendar, start_date, end_date,)
  query = gdata.calendar.client.CalendarEventQuery(text_query=pattern, start_min=start_date, start_max=end_date, max_results=25)
  query.start_min = start_date
  query.start_max = end_date

  try:
    feed = calendar_client.GetCalendarEventFeed(calendar_client.GetCalendarEventFeedUri(calendar=calendar), q=query)
  except gdata.client.RedirectError:
    print 'Request failure: too many redirect'
    return

  total_results = feed.total_results.text.strip()
  print "Total results: %s" % (total_results,)
  i = 1
  success = 0
  failure = 0
  while True:
    for an_event in feed.entry:
      print '\t%s/%s. %s (%s)' % (i, total_results, an_event.title.text,an_event.id.text.strip(),)
      i += 1
    if delete:
      result = DeleteRequest(calendar_client, feed)
      success += result[0]
      failure += result[1]
    feed = GetNextFeed(calendar_client=calendar_client, feed=feed, delete=delete, query=query, calendar=calendar) 
    if feed is None:
      print 'Done'
      break;
  print 'Total entries: %s - Proceesed: %i - Success: %i - Failure %i' % (total_results, i - 1, success, failure)


def main():
  # parse command line options
  try:
    opts, args = getopt.getopt(sys.argv[1:], "", ["user=", "pw=", "delete=", "calendar=", "pattern=", "start=", "end="])
  except getopt.error, msg:
    print ('python calendarDelete.py --user [username] --pw [password] ' +
        '--delete [true|false] --calendar [calendar] --pattern [pattern] ' +
        '--start [start_date] --end [end_date]')
    sys.exit(2)

  user = ''
  pw = ''
  delete = False
  calendar = 'default'
  start_date = '2010-01-01'
  end_date = '2010-12-31'
  pattern = ''

  # Process options
  for o, a in opts:
    if o == '--user':
      user = a
    elif o == '--pw':
      pw = a
    elif o == '--delete':
      delete = a == 'true'
    elif o == '--calendar':
      calendar = a
    elif o == '--pattern':
      pattern = a
    elif o == '--start':
      start_date = a
    elif o == '--end':
      end_date = a

  if user == '' or pw == '':
    print ('python calendarDelete.py --user [username] --pw [password] ' +
        '--delete [true|false] --calendar [calendar] --pattern [pattern]' +
        '--start [start_date] --end [end_date]')
    sys.exit(2)

  calendar_client = gdata.calendar.client.CalendarClient(source='Event-Deletion-1.0')
  calendar_client.ClientLogin(user, pw, calendar_client.source);

  DeleteEventEntries(calendar_client=calendar_client, calendar=calendar, start_date=start_date, end_date=end_date, pattern=pattern, delete=delete);

if __name__ == '__main__':
  main()



Reply via email to