Setting up the parser for reminders and events

In this task, we will implement a module that will blink an LED to remind us about important events and appointments.

Prepare for lift off

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.

Engage thrusters

  1. Using the Python IDLE's editor, let's get started by creating a new file called calendars.py:
    import gdata.calendar.service
    import gdata.service
    import gdata.calendar
    import time
  2. We can get started by defining a Google Calendar object. We will define the username and password for our Google account:
    calendar_service = gdata.calendar.service.CalendarService()
    calendar_service.email = '[email protected]'
    calendar_service.password = 'password'
    calendar_service.ProgrammaticLogin()
  3. The 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
  4. Let's create another Python script and test the functionality of the calendar module (calendar.py). We will begin by importing the previous definition as a module:
    import calendar
      feed = calendar.calendar_query()
    • Now, we will print the details of the retrieved events:
      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,)
    • The preceding example was borrowed from the documentation of the Google Calendar API. When executed, the output would be something like the following:
      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
      
  5. Since we are able to retrieve and parse the events on the Google Calendar, we will try to blink an LED to remind us about the events and appointments.
    • We will get started with importing the RPi.GPIO module in the same file:
      import calendar_special
      import time
      import datetime
      import RPi.GPIO as gpio
    • Since we used pin 17 for the e-mail alerts, let's use pin 24 for reminders and events.
    • The events can be retrieved from Google Calendar as follows:
      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,)
    • We will filter out all upcoming events and list them with details. We will check whether we have set an alert for the event. If indeed there was an alert, we will blink an LED until the event has ended:
      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)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.197.212