# Upload the appointments or events for a day to Google Calendar # based on the example from Jon Udell: # http://weblog.infoworld.com/udell/2006/06/13.html # # Replace USERNAME with your username (ie. testuser@gmail.com) # Replace PASSWORD with your password # Replace NAME with your name (ie. "Mike Rowehl") # import httplib, urllib, urlparse import calendar from time import * def GoogleAuth( u, p): params = urllib.urlencode({'Email':u,'Passwd':p,'service':'cl','source':'mikerowehl-s60gcal-0.1'}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPSConnection("www.google.com") conn.request("POST", "/accounts/ClientLogin", params, headers) response = conn.getresponse() body = response.read() conn.close() vals = dict([x.split('=') for x in body.splitlines()]) return vals['Auth'] def SessionUrl( auth, url ): headers = {"Accept": "text/plain", 'Authorization': 'GoogleLogin auth='+auth} conn = httplib.HTTPConnection("www.google.com") conn.request("GET", url, None, headers) response = conn.getresponse() sessionurl = response.getheader('Location') parts = urlparse.urlsplit(sessionurl) return parts[2] + '?' + parts[3] def dictFromS60Event(event): title = event.content descr = '' where = event.location start = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(event.start_time)) end = strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(event.end_time)) return {'title':title,'descr':descr,'where':where,'start':start,'end':end} def atomEntryFromDict(d): entry = """ %s %s NAME USERNAME """ % (d['title'],d['descr'],d['where'],d['start'],d['end']) return entry def insertGcal(atomEntry,sessionUrl,auth): headers = {'Content-Type': 'application/atom+xml','Authorization': 'GoogleLogin auth='+auth} conn = httplib.HTTPConnection("www.google.com") conn.request("POST", sessionUrl, atomEntry, headers) response = conn.getresponse() body = response.read() conn.close() u = 'USERNAME' p = 'PASSWORD' feedloc = '/calendar/feeds/'+u+'/private/full' cal = calendar.open() entries = cal.daily_instances(time(),appointments=1,events=1) auth = GoogleAuth( u, p ) sess = SessionUrl( auth, feedloc ) for entry in entries: event = cal[entry['id']] dict = dictFromS60Event(event) entry = atomEntryFromDict(dict) insertGcal(entry,sess,auth) print "Added : " + event.content