• Alexander

Automate Inspirational Quotes Notifications with Python on MacOS

"Remember that failure is an event, not a person"

Simple quotes like this, can really inspire you to be the best version of yourself. Imagine receiving such motivational boosts every hour? Great, right?

In this post, we are going to learn how to code a basic Python script that sends notifications to your Mac every hour.

What do you need?

  • Access to an inspirational quotes API. We will use this free API.

  • pync module to send notifications in our Mac.

  • Cron to automate the execution of our Python script.

Access the API

In order to access the API, we need to use requests library and use the get() method:

import requests

data = requests.get("https://type.fit/api/quotes")

Now, in the variable data we have the response of the API.

We can check the status of the request:


Also, we can obtain the JSON response from the request:

json_data = data.json()

At this point, we already have all the information sent in JSON format by the API.

This variable json_data is in fact an array of dictionaries. Each dictionary has a key to access each field of the JSON. In our case, we have two keys:

  • author

  • text

quote = json_data[0]
text = quote["text"]
author = quote["author"]

In the second and third line we have accessed to the values related with these keys and save them in text and author variables, respectively.

However, we are accessing always to the same JSON object, which means that we will obtain the same quote at every execution of the script. To solve this, we can use the python module random to select a different one at each execution of the script.

import random
import requests

data = requests.get("https://type.fit/api/quotes")
json_data = data.json()

# Create a seed to take a random quote
data_size = len(json_data)
seed = random.randint(0, data_size-1)

# Select the quote randomly
quote = json_data[seed]
text = quote["text"]
author = quote["author"]

Creating the notification

Now, we can use pync to display the notifications. We can do it in many ways, as explained in the documentation, but I prefer the following one:

from pync import Notifier
Notifier.notify(text, title = author + " once said:")

As you can see above, the first argument of notify() is the body of the notification and the second argument is the title of the notification. You will see a notification as the one below:

You can use different flags to control other parameters. If you are interested, you can check the documentation of pync.

Automating using Cron

Finally, we can automate this task by using cron. Cron is a time-based job scheduler that works in Unix-like systems.

To configure it, we need to use crontab. Crontab has a specific format to set a cron job:

minute hour day_of_month month day_of_week command 

The parameter command refers to any terminal command we want to execute. The five first parameters refer to the time of the execution of the cron job. They accept the following inputs:

  • minute: 0 - 59

  • hour: 0 - 23

  • day_of_month: 1 - 31

  • month: 1 - 12

  • day_of_week: 0 - 6, where 0 is Sunday

Also, you can use * to tell that you want to execute at every minute/hour/day of the month/month/day of week. For example, if you want to execute a cron job every day at 10am, you can write:

0 10 * * * command

In any case, if you want to check if your time configuration is correctly set up, you can check it at Crontab.guru.

To access crontab configuration, open the terminal and write the following command:

crontab -e

Press i key to be able to start typing.

In our case, we would like to execute our Python script every day at every hour. To do so, we will write the following command:

0 * * * * cd path_dir && /path_to_python/python inspirationalQuotes.py

Let's understand the command:

  • 0 * * * * means that the command is executed every hour everyday.

  • cd path_dir is used to access to the directory where our script is located.

  • && concatenates terminal commands.

  • /path_to_python/python is where our python compiler is placed. If you have anaconda, probably this path looks like /anaconda3/bin/python.

  • inspirationalQuotes.py is the name of our Python script.

Then, hit ESC key and write ":wq" to save and close the crontab configuration. A pop up may be shown from MacOS system asking to enable the terminal to modify your system. Simply accept it.

Finally, you can list all the cron jobs you have set up by using:

crontab -l 

We are done! We hope you enjoy being notified with awesome inspirational quotes and get inspired to work hard.