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.

  7. August 15th, 2019 at 04:42 | #7

    You will be surprised as to how men and women develop will purchase
    your useless junk e-mail. Study the tutorials or visit forums
    where existing writers can help clear your doubts. If you haven’t already just
    do it register. http://satone-phone.com/__media__/js/netsoltrademark.php?d=918kiss.bid%2Fdownloads%2F167-download-live22-apk-to-phone-for-android-and-ios

  8. October 6th, 2019 at 11:39 | #8

    If you lack the knowledge, then you can use tthe search engines
    to a person the indicates. Anyone cann start a
    directorey man or woman because it exists doesn’t mean which it is well ran or will drive
    you traffic. http://abog.hopto.org/phpinfo.php?a%5B%5D=%3Ca+href%3Dhttps%3A%2F%2F918kiss.bid%2Fdownloads%2F167-download-live22-apk-to-phone-for-android-and-ios%3Elive22+apk%3C%2Fa%3E

  9. October 20th, 2019 at 17:55 | #9

    Keflex Manufacturere Cialis Soft Tabs Online cialis 5mg best price Nimotop Viagra Rezeptfrei China

  10. October 22nd, 2019 at 01:03 | #10

    It is to keep the customer’s trust when you create an exhibit.
    Whereas reading involves both the physical as well as mental; however, reading remains primarily
    a mental assignment.

  11. October 24th, 2019 at 12:21 | #11

    The player can even talk with the dealer if you have already registered as an affiliate.
    In the united states Virgin Media Broadband is one of the hassle free connections at
    a low price.

  12. October 26th, 2019 at 12:56 | #12

    Cialis Buy Online Cheap cheapest cialis 20mg Propecia Shedding To Work Priligy Alcol

  13. November 4th, 2019 at 19:29 | #13

    Valuable information. Thanks a lot!
    interactions for modafinil
    Provigil Generic Online

  14. rabsbody
    November 15th, 2019 at 20:53 | #14

    Buy cialis online pharmacy buy cialis online viagra online prices cialis levitra

  15. Invergo
    November 16th, 2019 at 00:41 | #15

    Cialis generic reviews http://cialisdbrx.com non generic cialis

  16. ambutle
    November 16th, 2019 at 00:47 | #16

    Viagra black market cialis pills buy cheap cialis online shelf life cialis online pharmacy

  17. Beidasy
    November 16th, 2019 at 18:09 | #17

    Bad credit online payday loans payday loans borrow money now

  18. Donysoog
    November 16th, 2019 at 18:20 | #18

    Small loans no credit http://paydaymyonline.com apply for credit

  19. apafroni
    November 16th, 2019 at 21:42 | #19

    Payday loans el paso payday loans online fast personal loans

  1. September 4th, 2019 at 10:14 | #1
  2. September 5th, 2019 at 12:18 | #2
  3. September 6th, 2019 at 02:45 | #3
  4. September 7th, 2019 at 16:38 | #4
  5. September 11th, 2019 at 17:46 | #5
  6. October 6th, 2019 at 19:06 | #6
  7. October 15th, 2019 at 05:11 | #7
  8. October 29th, 2019 at 07:14 | #8
  9. November 13th, 2019 at 15:12 | #9
  10. November 16th, 2019 at 17:03 | #10

*