Home > English, Linux & Programming > rsslibnotify – My very first python script

rsslibnotify – My very first python script

UPDATE: I included tante’s comments and extended the program. The config-file now allows you to define the checking interval and will be automatically created if nonexisting.

I noticed that gwibber removed their rss support for some reason. However, it would be really nice to have the option for libnotify to …well… notify you on gnome when there is something new on rss/atom feeds you find important. Since I always wanted to learn python anyway, I hacked together a small script, which does exactly that. Just create a config file in ~/.rsslibnotify with a number of rss/atom feeds separated by newlines and you will be informed every ten minutes via libnotify if there’s something new. (see update above)

feedparser and pynotify need to be installed prior. Btw. feedparser is giving me errors while installing, however seems to be working without a problem.

Of course this script is far from perfect. Keep in mind it’s my first python script and I worked at most one to two hours on it ;).

Available for download here: rsslibnotify.py

#!/usr/bin/python
# coding=utf-8
 
#
#    Copyright 2010 by Daniel Süpke
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#
# rsslibnotify v0.3
#
 
import pynotify
import feedparser
import time
import os.path
import sys
 
config_file = os.path.expanduser('~/.rsslibnotify')
feeds = []
titles = []
 
try:
    file = open(config_file, 'r')
except:
    file = open(config_file, 'w')
    file.write('# amount of seconds to wait before checking for new items\n')
    file.write('interval 600\n\n')
    file.write('# feeds to check\n')
    file.write('feed https://suepke.net/feed/\n')
    file.write('feed http://rss.golem.de/rss.php?feed\x3dRSS1.0\x26ms\x3drss\n')
 
    print('Config file not found! Created example file at ' + config_file + '. Please edit.')
    sys.exit(0)
 
for line in file.readlines():
    config_line = line.rstrip().partition('#')[0] # Remove \n and comments
 
    if config_line.startswith('feed '):
        feeds.append(config_line.partition('feed ')[2])
    if config_line.startswith('interval '):
        interval = int(config_line.partition('interval ')[2])
 
    titles.append('')
 
file.close()
 
if not feeds:
    print('There were no feeds in your config file at ' + config_file + '. Exiting...')
    sys.exit(0)
 
if not interval:
    print('No check interval has been defined. Assuming ten minutes.')
    interval = 600
 
# Check for new feeds every interval seconds
while True:
    i = 0
 
    for feed in feeds:
        d = feedparser.parse(feed)
 
        # Assume new content if there is a new title
        if d.entries[0].title != titles[i]:
            titles[i] = d.entries[0].title
 
            if pynotify.init('rssnotify'):
                n = pynotify.Notification(d.feed.title, d.entries[0].title)
                n.show()
            else:
                print('there was a problem initializing the pynotify module')
        i += 1
 
    time.sleep(interval)
  1. October 28th, 2010 at 19:42 | #1

    Being the Python Nerd that I am, a few remarks 😉

    use “print” not as a statement (print ‘String’) but as a built-in function (print(‘String’)) because Python3 drops the statement, so it’s good to get used to not using it 😉

    An empty List ([]) evaluates as False so you can just write:
    if not feeds:
    print(“There were no feeds…”)

    And a final style thing: In Python land we live by the statement “It’s easier to ask for forgiveness than for permission” so to write “pythonic” code you wouldn’t check if the config file exists but just open it and read from it. If it goes well, fine. If an exception flies you catch it and write “Yeah no config file”.

    Sorry for smartassing 😉

  2. October 29th, 2010 at 11:07 | #2

    Thanks! I want to learn the language to see what all the fuss is about, since everyone working with python seems to be really excited about it. So especially that with the pythonic style is really helpful. I’ll change the script accordingly when I come to it. There’s always time for procrastinating 😉

  3. October 29th, 2010 at 12:48 | #3

    @Daniel
    That’s what you have a github account for, merge requests are the new contact request 😉

  4. October 29th, 2010 at 12:49 | #4

    @Daniel
    Also if you really want to get more into it, read the “Dive into Python” Book (preferably the new version) which you can download for free at diveintopython.org or which you can borrow from me (I have both versions in dead-tree form).

  5. October 29th, 2010 at 12:50 | #5

    @Daniel
    One final note:
    1. Start your python interpreter
    2. type “import this”
    3. Learn it by heart

  6. November 1st, 2010 at 12:27 | #6

    @tante
    “There should be one– and preferably only one –obvious way to do it.
    Although that way may not be obvious at first unless you’re Dutch.”
    😀

    Thanks for the hints! Updated the source code.

  1. No trackbacks yet.
*