Thursday, March 19, 2009

IPY with Google Data Calendar API

Here's a code that I've done while learning the language. In this python code I explored the Google Calendar through its API. Creating an instance of the service,ClientLogin and retrieving entries were pretty much straightforward. But creating,updating and deleting calendar's event(or entries) really took me sometime to make it work. On creating entries, I had a hard time coding the this
Insert(TEntry)(Uri, TEntry)
to python. It took a help from IPY mailing list to get on with my programming. On updating and deleting entries, I thought it would be straightforward but it's not. But, anyway I figured it out how to delete or update entries.

Below is the class that explore the Google Calendar.

class googleCal:

def __init__(self,googleacct,googlepasswd):
"Login to google"
self.calsvc = GDCal.CalendarService("myapp")
self.calsvc.setUserCredentials(googleacct,googlepasswd)
return self.calsvc
def doentries(self,myURL,date2post,myTitle,myContent):
# define srch range for retrieval
postdate = DateTime.Parse(date2post)
myQuery = GDCal.EventQuery(myURL)
myQuery.StartTime = postdate
myQuery.EndTime = postdate.AddMinutes(1438)
myevFeed = self.calsvc.Query(myQuery)

if myevFeed.Entries.Count > 0:
for myentry in myevFeed.Entries:
# first entry and only entry need to update or delete
print "Old Entry: " + myentry.Title.Text
###########################################################
# to delete entry
#myentry.Delete <--- not working
###########################################################
self.calsvc.Delete(myentry)
print "Succesfully deleted\n"
############################################################
# to update entry
#myentry.Title.Text = myTitle
#myentry.Content.Content = myContent
#myentry.Update <-- not working
#self.mycalsvc.Update[type(myentry)](myentry) <-- working
############################################################
break
else:
pass

# delete and create that's the idea not update
myentry = GDCal.EventEntry()
myUri = Uri(myURL)
myentry.Title.Text = myTitle
myentry.Content.Content = myContent
if DateTime.Compare(postdate,DateTime.Today) == 0:
evWhento = DateTime.Now
else:
evWhento = postdate.AddHours(22)
evTime = GDxtn.When(evWhento,evWhento.AddMinutes(60))
myentry.Times.Add(evTime)
self.calsvc.Insert[type(myentry)](myUri,myentry)


and the rest of the code that uses the class

if len(sys.argv) < 2:
dtofs = 0
else:
dtofs = int(sys.argv[1])

mydate = CDRdate(dtofs)
print "Date: " + mydate.rtndate()
CDR = getCDR(mydate.rtndate())
sfields = CDR.rtnCDR()
eTitle = "Dumb Bill: $%3.2f" % sfields[2]
eContent = "CallCount: %d\nTotalLength: %d:%d\nBill: $%3.2f\n" % (sfields[0],(sfields[1]/60),(sfields[1]%60),sfields[2])
feedURL = "http://www.google.com/calendar/feeds/sometext here%40group.calendar.google.com/private/full"
print eTitle
print eContent
gcalsvc = googleCal(username here,password here)
gcalsvc.doentries(feedURL,mydate.rtndate(),eTitle,eContent)
print "New Entry: " + eTitle
print "Successfully Inserted\n"

No comments:

Post a Comment

Followers

About Me

An underrated techie. With great interest in digital electronics, photography(minus photoshop and others stuff) and audio-video engineering. With more than 10 years of IT working experience ranging from systems and network administration, programming and Voice-Over-IP engineering.