views:

363

answers:

4

I'm struggling with writing a python script that automatically grabs the zip fail containing all my google calendars and stores it (as a backup) on my harddisk.

I'm using ClientLogin to get an authentication token (and successfully can obtain the token).

Unfortunately, i'm unable to retrieve the file at https://www.google.com/calendar/exporticalzip

It always asks me for the login credentials again by returning a login page as html (instead of the zip).

Here's the critical code:

post_data = post_data = urllib.urlencode({ 'auth': token, 'continue': zip_url})
request = urllib2.Request('https://www.google.com/calendar', post_data, header)
try:
  f = urllib2.urlopen(request)
  result = f.read()
except:
  print "Error"

Anyone any ideas or done that before? Or an alternative idea how to backup all my calendars (automatically!)

A: 

Calendars have an URL which allows you to download it without authentication (see Google Calendar's UI, Calendar settings, under Private URL). You should be able to download the iCal or XML file using urllib without problems.

Wim
if possible i'd like to use the url i provided above. there i always get ALL my calendar in one zip file without the need of knowing the private urls of every single calendar.
Michael
+1  A: 

You could write a script with mechanize to walk through google login process before downloading Calendar from your preferred url.

So try with:

import mechanize
br=mechanize.Browser()
br.open('https://www.google.com/calendar/exporticalzip')
br.select_form(nr=0)
br['Email']='[email protected]'
br['Passwd']='Password'
br.submit()
br.retrieve('https://www.google.com/calendar/exporticalzip','exportical.zip')

It worked for me, i downloaded my zipped ical calendar succesfully.

systempuntoout
A: 

Great solution systempuntoout! For those of you using Google Apps, just needs a tweak to the URLs and the Email.

import mechanize
br=mechanize.Browser()
br.open('https://www.google.com/calendar/hosted/yourdomain.com/exporticalzip')
br.select_form(nr=0)
br['Email']='Username'
br['Passwd']='Password'
br.submit()
br.retrieve('https://www.google.com/calendar/hosted/yourdomain.com/exporticalzip','exportical.zip')

would have commented on the other answer, but not enough rep yet. :(

Skit
A: 

Could you post the entire code, please?