Events by Day

Subscribe to Tech With Tim!

Up until this point we have only been able to get events the next upcoming events from our google calendar. In this tutorial I will show you how you can retrieve a list of events that occur in one day.

Using Pyttsx3

In the previous tutorials we used gTTS (Google Text-to-Speech) to output audio and speak to the user. Since I've noticed this module has a lot of problems I've decided to change to pyttsx3. This module is much easier to use and does not need an internet connection to produce voice.

We will start by installing it with pip:

pip install pyttsx3

Next we will import it into our python script.

import pyttsx3

And finally, replace our previous speak() function with the following:

def speak(text):
    engine = pyttsx3.init()

Events By Day

Now we will modify our get_events() function to take in a date and print out the events that occur on that specific date. Unfortunatly the google calendar API is very picky when it comes to how it accepts date information. This means we need to do some work to convert our date object into the correct form.

Here is what our new get_events() function looks like:

def get_events(day, service):
    # Call the Calendar API
    date = datetime.datetime.combine(day, datetime.datetime.min.time())
    end = datetime.datetime.combine(day, datetime.datetime.max.time())
    utc = pytz.UTC
    date = date.astimezone(utc)
    end = end.astimezone(utc)
    events_result ='primary', timeMin=date.isoformat(), timeMax=end.isoformat(),
    events = events_result.get('items', [])

    if not events:
        print('No upcoming events found.')
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])

Now if we call get_events() and pass a date we got from the get_date() function we will see a list of events on that specific day.


Now we can test out our new function by doing writing the following into the mainline of our program.

SERVICE = authenticate_google()
text = get_audio()
get_events(get_date(text), SERVICE)

In the next tutorial we will make the assistant speak the events out to us.