# calendar.py # Sends message to your nabaztag to read out your remaining events for the current day from your Google Calendar # Created by Mike McGurrin # November 14, 2009 # May be freely used for non-commercial use # Contact author for commercial use # To use, replace [paste your username here] with your calendar username, replace [paste your private key here] # with your private key from Google Calendar, and replace 'Mike' in text strings with your name (unless you're named Mike!) # Also requires a file in the same directory called wxBunnyFile.txt # 1st line is nabaztag serial number (mac), 2nd line is token, 3rd line is voice to use """ Accesses a Google calendar event feed. Sends remaining events for the day as msg. to Nabaztag""" import gdata.calendar.service import gdata.service import atom.service import gdata.calendar import atom import getopt import sys import string import time import datetime from Nabaztag import * # Function that makes a query with a date range, with the events sorted by start time def DateRangeQuery(calendar_service, start_date, end_date): """ Make a date ranged query to calendar service. Return the feed result""" query = gdata.calendar.service.CalendarEventQuery(username, visibility, projection) query.start_min = start_date query.start_max = end_date query.orderby = "starttime" query.sortorder = "ascending" feed = calendar_service.CalendarQuery(query) return feed # Set basic parameters for the query username = '[paste your username here]' visibility = 'private-[paste your private key here]' projection = 'full' # Set start and end dates for events returned, in proper format # In this case, current day and next day (exclusive) today=datetime.datetime.today() todaysDate = datetime.datetime.isoformat(today) tomorrowsDate = datetime.datetime.isoformat(today+datetime.timedelta(days=1)) calendar_service = gdata.calendar.service.CalendarService() # Make the query feed = DateRangeQuery(calendar_service, todaysDate, tomorrowsDate) # Process results and build output strings # Currently inefficient naive concatenation. Use list of strings and join at the end (can use ''.join conclusion = 'Have a great day!' eventString = '' if feed.entry: announce = "events on Mike's calendar for " + today.strftime('%A %B %d.\t') for i, an_event in enumerate(feed.entry): eventString=eventString+'\n'+str(an_event.title.text) for a_location in an_event.where: if a_location.value_string is not None: eventString=eventString+'. at '+str(a_location.value_string) if an_event.text is not None: eventString=eventString+'. '+str(an_event.text) for a_when in an_event.when: start = a_when.start_time end = a_when.end_time # Convert start time to appropriate strings to be read aloud splitStart = start.split('T') splitStart = splitStart[1].split(':') # Convert from 24 hour to 12 hour clock for the hour startHour = int(splitStart[0]) % 12 if startHour == 0: startHour = 12 startHourString = str(startHour) startMinuteString = splitStart[1] if startMinuteString == '00': startMinuteString = ' ' startString = startHourString+' '+startMinuteString # Convert end time to appropriate strings to be read aloud splitEnd = end.split('T') splitEnd = splitEnd[1].split(':') # Convert from 24 hour to 12 hour clock for the hour endHour = int(splitEnd[0]) % 12 if endHour == 0: endHour = 12 endHourString = str(endHour) endMinuteString = splitStart[1] if endMinuteString == '00': endMinuteString = ' ' endString = endHourString+' '+endMinuteString # Now add to the string eventString=eventString+'. Starting at '+startString+' and ending at '+endString+'.' else: # There are no events for the day announce = "Mike has no events scheduled for today. " # Read the bunny info, desired voice, and zip code bunnyFile = open('wxBunnyFile.txt', "r") data = bunnyFile.readlines() # Strip of the new line characters sn=data[0][:-1] token=data[1][:-1] voice=data[2][:-1] #Last row may not have a new line character, depending on edits if data[3][-1] == "/n": zip=data[3][:-1] zip=data[3] bunnyFile.close() # Create bunny object myNab = Nabaztag(sn, token) # Send text to speech using Ryan voice msg = "%s%s%s" % (announce, eventString, conclusion) myNab.say(msg,voice)