Created
November 13, 2010 05:11
-
-
Save egradman/675103 to your computer and use it in GitHub Desktop.
calendar code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import atom | |
import gdata | |
import gdata.calendar | |
import gdata.calendar.service | |
def GetCalendarByTitle(calendar_service, title): | |
feed = calendar_service.GetAllCalendarsFeed() | |
calendar = [entry for entry in feed.entry if entry.title.text == title][0] | |
return calendar | |
def InsertEvent(calendar_service, calendar, title, | |
content, start_time=None, end_time=None): | |
event = gdata.calendar.CalendarEventEntry() | |
event.title = atom.Title(text=title) | |
event.content = atom.Content(text=content) | |
if start_time is None: | |
# Use current time for the start_time and have the event last 1 hour | |
start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime()) | |
end_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 3600)) | |
event.when.append(gdata.calendar.When(start_time=start_time, end_time=end_time)) | |
calendar_id = calendar.id.text.split("/")[-1] | |
uri = 'http://www.google.com/calendar/feeds/%(calendar_id)s/private/full' % locals() | |
tries = 0 | |
while tries < 2: | |
try: | |
new_event = calendar_service.InsertEvent(event, uri) | |
break | |
except gdata.service.RequestError: | |
tries += 1 | |
print 'New single event inserted: %s' % (new_event.id.text,) | |
print '\tEvent edit URL: %s' % (new_event.GetEditLink().href,) | |
print '\tEvent HTML URL: %s' % (new_event.GetHtmlLink().href,) | |
return new_event | |
if __name__=='__main__': | |
calendar_service = gdata.calendar.service.CalendarService() | |
calendar_service.email = '[email protected]' | |
calendar_service.password = 'orenoren' | |
calendar_service.source = 'Google-Calendar_Python_Sample-1.0' | |
calendar_service.ProgrammaticLogin() | |
calendar = GetCalendarByTitle(calendar_service, "test") | |
InsertEvent(calendar_service, calendar, "go to the movies", "with a monkey") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment