Open In App

Python Desktop News Notifier in 20 lines

To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2. 
Give following command to to install feedparser: 

sudo pip3 install feedparser

For installing notify2 in your terminal:



sudo pip3 install notify2

Feedparser will parse the feed that we will get from the URL. We will use notify2 for the desktop notification purpose. Other than these two libraries, we will use OS and time lib. Once you are done with the installation import both libraries in the program. Here, in this example I have parsed the news from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program: 
 




# Python program to illustrate 
# desktop news notifier
import feedparser
import notify2
import os
import time
def parseFeed():
    f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
    ICON_PATH = os.getcwd() + "/icon.ico"
    notify2.init('News Notify')
    for newsitem in f['items']: 
        n = notify2.Notification(newsitem['title'], 
                                 newsitem['summary'], 
                                 icon=ICON_PATH 
                                 )
    n.set_urgency(notify2.URGENCY_NORMAL)
    n.show()
    n.set_timeout(15000)
    time.sleep(1200)
      
if __name__ == '__main__':
    parseFeed()

Screenshot of the news notification popup



Step by step Explanation of Code: 

f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
ICON_PATH = os.getcwd() + "/icon.ico"
notify2.init('News Notify')
 for newsitem in f['items']: 
        n = notify2.Notification(newsitem['title'], 
                                 newsitem['summary'], 
                                 icon=ICON_PATH 
                                 )
n.set_urgency(notify2.URGENCY_NORMAL)
n.show()
n.set_timeout(15000)
time.sleep(1200)

Article Tags :