Friday Fun: Get Notifications On Your Desktop

Bit of a personal project this week, created to meet a specific need, but it contains some useful Python libraries for creating notifications and parsing RSS feeds.

So I spend a lot of time researching, reading and looking for the latest news, mainly Raspberry Pi news because we all know that Les loves Pi(e)!

So for this weeks Friday Fun I built myself a notifications application in Python3.

This application is used by calling the application with two arguments, the arguments being the URL for the RSS feed that I wish to keep an eye on and the second being the delay between repeating.

This project works with Ubuntu, and it should work with other Linux distributions, but sadly not the Raspberry Pi...I tried..I promise!

Code Download

All of the code for this project can be found on my Github page it is all Creative Commons CC-BY-SA 3.0 so use it and make it your own.

Installing the Python libraries

Before I write any code I need to install the Python libraries that power the application. These are feedparser used to parse RSS feeds in Python, and notify2 which generates desktop notifications in Python.

So in a terminal I installed the libraries using pip3

sudo pip3 install feedparser notify2

Writing the Python Code

Using your favourite text editor, I quite like Geany right now, create a new blank document called executable.py

Where is Python3?

I want this application to run without using IDLE or by using the Python interpreter in the terminal, so I add a line that tells the application where to find the Python3 interpreter for my operating system.

#!/usr/bin/python3

Libraries

We now carry on by importing the libraries we shall later be using.

We use sys to handle arguments from the terminal, feedparser to parse RSS feeds, notify2 will create our desktop notifications and finally time is used to create a delay in our code.

import sys
import feedparser
import notify2
import time

Objects

So now we move on and create a variable called feed that will store the URL for the RSS feed that is provided by the argument that we pass on the command line. A second variable delay will take the second argument from the command line and convert it to an integer which will be used to set the delay in seconds. Then we initialise the notify2 instance with a title that will provide information as to what feed we are looking at.

feed = feedparser.parse(sys.argv[1])
delay = int(sys.argv[2])
notify2.init("Latest News from "+str(sys.argv[1]))

Forever and ever...

In order for the code to continuously run we need to use a while True: loop, all of the code for the project will run inside it.

while True:

Iterating a Loop

So...How many news articles do I want to see?

Well using a for loop I will set that value as 3. I want to see the latest three items in the RSS feed.

    for i in range(3):

Each time the loop goes round we use the feed object and we inspect the feed to look inside [entries] for the [i] item in the list, and then show us the [title] which is the title of the news item/blog post.

        info = feed["entries"][i]["title"]

And we shall do the same for the URL of the blog post.

        URL = feed.entries[i]["link"]

I love a good icon, so I've borrowed the Raspberry Pi logo (don't sue me) to add to my notifications. I create a variable called icon and in the variable I store the full path to the picture. Obviously you need to have a picture in the location that use as the path, otherwise you will see nothing.

        icon = "/home/les/Documents/blog/RSS-Notif/Pi.png"

We're getting near to the end now, so lets create an object called n and in there we store an instruction to notify2 that it should create a notification using the information stored in our info, URL and icon variables.

        n = notify2.Notification(info,URL,icon=icon)

Then we tell notify2 to show the notification.

        n.show()

Breaking out of the for loop, but still inside the main while True: loop we now tell the application to time.sleep (wait) for the number of seconds stored in our delay variable.

    time.sleep(delay)

That's all of the code, now go ahead and save your work and close the editor.

Make it executable

Open a terminal and navigate to where I have saved the executable.py file. I want to be able to call the application from the terminal without calling a Python interpreter, which was the first line of code that I added to the project. But in order to make the file executable I need to tell Ubuntu that it is an executable file. So in the terminal type this command to do so

chmod +x executable.py

Stay in the terminal as now it is time to test!

Test it!

Let's take it for a test drive. In the terminal I tested my version by typing

./executable.py https://www.raspberrypi.org/feed/

and it worked!

Create a system wide application.

So it works, but right now it is still just a Python project in a folder somewhere, why not turn it into a system wide application? To do that we need to copy the file to the /usr/bin/ directory, and also rename it as executable.py is a bit unwieldy but NewsPi is cool.

So in a terminal type the following (in the same directory as the executable.py file.

sudo cp executable.py /usr/bin/NewsPi

Test it!

Get the latest Raspberry Pi news and repeat every 60 seconds.

NewsPi https://www.raspberrypi.org/feed/ 60

To stop the test press CTRL + C

Ubuntu auto start

I have the app, it works and I am happy, but now I want it to run when I start Ubuntu. and to do that I need to use the Startup Applications tool.

Click on Add to create a new application and then enter the details as so.

Click Save and then Close.

Now reboot, and marvel as the application is auto run once Ubuntu starts, and then every five minutes (300 seconds) it repeats the loop.

Feel free to adapt the delay to suit your needs, for my version I went with 3600 seconds as it provides enough information without being too distracting.