In this task, we will implement a module that will blink an LED to remind us about important events and appointments.
The Google Data Python client library is required for this task. You also have to save some tasks in your calendar to test the implementation.
calendars.py
:import gdata.calendar.service import gdata.service import gdata.calendar import time
calendar_service = gdata.calendar.service.CalendarService() calendar_service.email = '[email protected]' calendar_service.password = 'password' calendar_service.ProgrammaticLogin()
calendar_query
function performs a query for calendar events by defining a time frame as follows:query = gdata.calendar.service.CalendarEventQuery('default','private','full') query.start_min = '2014-01-01' query.start_max = '2013-05-31' feed = calendar_service.CalendarQuery(query) return feed
calendar.py
). We will begin by importing the previous definition as a module:import calendar feed = calendar.calendar_query()
for i, an_event in enumerate(feed.entry): print ' %s. %s' % (i, an_event.title.text,) for a_when in an_event.when: print ' Start time: %s' % (a_when.start_time,) print ' End time: %s' % (a_when.end_time,)
0. Sai Start time: 2013-05-12T11:30:00.000-05:00 End time: 2013-05-12T12:30:00.000-05:00 1. Event at Firegrill house Start time: 2013-04-24T18:30:00.000-05:00 End time: 2013-04-24T19:30:00.000-05:00 2. Chicago Hardware Startup Meetup - FIRST MEETING Start time: 2013-01-23T18:30:00.000-06:00 End time: 2013-01-23T21:00:00.000-06:00 3. Freescale SABRE Lite Board Start time: 2013-01-30T12:00:00.000-06:00 End time: 2013-01-30T13:00:00.000-06:00
RPi.GPIO
module in the same file:import calendar_special import time import datetime import RPi.GPIO as gpio
feed = calendar_special.calendar_query() # retrieve events from calendar for i, an_event in enumerate(feed.entry): for a_when in an_event.when: #We retrieve the start time and end time for each appointment/event try: start_time = datetime.datetime.strptime(a_when.start_time.split(".")[0], "%Y-%m-%dT%H:%M:%S") end_time = datetime.datetime.strptime(a_when.end_time.split(".")[0], "%Y-%m-%dT%H:%M:%S") except ValueError: print(ValueError) continue current_time = datetime.datetime.now() if end_time > current_time: #Has the event ended? print ' %s. %s' % (i, an_event.title.text,) print ' Start time: %s' % (a_when.start_time,) print ' End time: %s' % (a_when.end_time,)
for reminder in a_when.reminder: if reminder.method == "alert" and start_time - datetime.timedelta(0,60*int(reminder.minutes))<current_time: print ' %s. %s' % (i, an_event.title.text,) print ' Start time: %s' % (a_when.start_time,) print ' End time: %s' % (a_when.end_time,) count = 0 gpio.output(24,gpio.HIGH) time.sleep(1) gpio.output(24,gpio.LOW) time.sleep(1)
3.141.197.212