Build desktop notifier using python and notify2.

You must have heard about desktop notifier, today we are going to build one and see how it works. We are going to do it using python and we will do it on ubuntu for now. So, lets start to build desktop notifier using python.

build desktop notifier using python

Lets build desktop notifier using python

For building this we will be using notify2 and using requests module, we will get latest news from hackers news and show it in notification. Lets start with getting news from hacker news.

How I created a 100 twitter bots and made them interact using python, aws and selenium..

For getting new from hacker news we are going to use their apis. You can see the apis here.

We make an api call https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty to get all the latest story ids then make another get call to https://hacker-news.firebaseio.com/v0/item/’+str(a[0])+’.json?print=pretty to get its details.

Below code will do this

import requests
import json

a = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
a = json.loads(a.content)
b = requests.get('https://hacker-news.firebaseio.com/v0/item/'+str(a[0])+'.json?print=pretty')
b = json.loads(b.content)
ICON = "Path of icon image"
newsitem = [{"title":b['url'],"description":b['title']}]

In newsitem you will get the title and description of the news.

Now we will make a notifier and pass this data to that. Look at the below code. It gets the news and shows notification on the screen.

import time
import notify2
import requests
import json

a = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
a = json.loads(a.content)
b = requests.get('https://hacker-news.firebaseio.com/v0/item/'+str(a[0])+'.json?print=pretty')
b = json.loads(b.content)
ICON = "Path of icon image"
newsitem = [{"title":b['url'],"description":b['title']}]

# initialize the d-bus connection
notify2.init("Hacker News Notifier")

# create Notification object
n = notify2.Notification(None, icon = ICON_PATH)

# set urgency level
n.set_urgency(notify2.URGENCY_NORMAL)

# set timeout for a notification
n.set_timeout(10000)

n.update(newsitem['title'], newsitem['description'])
n.show()
time.sleep(5)

Python:

Suggested books for Algorithms in Python

Now you can see the notification like below.

build desktop notifier using python

You can also modify it for different content and actions on notifications. What you need to do is just explore.

Building desktop notifier is pretty easy task and may be we can take on some bigger task for the next time. If you have any idea that we can build together please do not hesitate to drop in the comments. I will get back to you so that we can make things up and running as early as possible. You can read more articles here.

Also please ignore the bad coding that I used here its just for giving hint how you can do it. So i didn’t bother about creating classes or stuff cause I don’t need it more than once. 🙂

Thanks for reading if you like the stuff, Please share, subscribe and spread the words. We really need these type of inspirations.

Level order traversal of a binary tree in python.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.