Create a Desktop Notifier in Python

Want to create a simple yet functional Python project? Desktop informant could be the perfect fit! This exercise is not only a great way to use Python, but also shows how to integrate libraries and templates. In this guide, we’ll walk you through creating a Desktop Notifier that will display the day’s top news stories. Let’s get started!

What is a Desktop Notifier?

Desktop Notifier is an application that sends notification messages to your desktop. Think of it as a pop-up alert that can provide you with important information like news headlines, weather updates, or even reminders.

Project Overview

In this project we will create a Python script that will:

  • Feed high-quality news from RSS feeds.
  • Parses headers using XML parsing.
  • Each title appears as desktop text.

Getting Started: Install Required Libraries

Before diving into the code, let’s install the necessary Python libraries. We will use queries to retrieve data from the internet and report2 to display reports. You can install pip under these libraries:

Python
pip install requests notify2

Step 1: Fetching News Headlines

The first step is to bring the latest news. We will use a query library to get an RSS feed from a news website and then parse the XML data.

Here is the Python code to fetch and parse the RSS feed:

Python
import requests 
import xml.etree.ElementTree as ET 

RSS_FEED_URL = "http://www.hindustantimes.com/rss/topnews/rssfeed.xml"    

def loadRSS(): 
    resp = requests.get(RSS_FEED_URL) 
    return resp.content 

def parseXML(rss): 
    root = ET.fromstring(rss) 
    newsitems = [] 

    for item in root.findall('./channel/item'): 
        news = {} 

        for child in item: 
            if child.tag == '{http://search.yahoo.com/mrss/}content': 
                news['media'] = child.attrib['url'] 
            else: 
                news[child.tag] = child.text.encode('utf8') 
        newsitems.append(news) 

    return newsitems 

def topStories(): 
    rss = loadRSS() 
    newsitems = parseXML(rss) 
    return newsitems 

This script takes the RSS feed, parses the XML data, and extracts the news titles.

Step 2: Configure Desktop Notifier

Now, let’s use notify2 as a Desktop Notifier. This library allows us to send desktop reports quickly.

Here is the Python code for the Desktop Notifier:

Python
import time 
import notify2 
from topnews import topStories 

ICON_PATH = "put full path to icon image here"
  
newsitems = topStories() 

notify2.init("News Notifier") 

n = notify2.Notification(None, icon=ICON_PATH) 

n.set_urgency(notify2.URGENCY_NORMAL) 
n.set_timeout(10000) 

for newsitem in newsitems: 
    n.update(newsitem['title'], newsitem['description']) 
    n.show() 
    time.sleep(15) 

Step 3: Running the Script

Configure the notifier to run the script. Your desktop will start displaying notifications with the latest news. You can adjust the report frequency by changing the value of time.sleep(15).

Project highlights:

  • What you will learn XML Parsing: Learn how to extract data from an XML file with Python.
  • HTTP requests: Use a request library to retrieve data from a web browser.
  • Desktop notifications: Understand how to create and display notifications on your desktop.
  • Python Scripting: Enhance your scripting skills by working on a real-world project.

Final Thoughts

This Desktop Notifier project is a great way to improve your Python skills while building something useful. You can extend this functionality by adding other features, such as submitting weather updates or creating reminders. Happy coding!

Share This Post:

Leave a Reply

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

Scroll to Top