Archive

Posts Tagged ‘Python’

rsslibnotify – My very first python script

October 28th, 2010 6 comments

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)

See also: Lucid Dreaming the Python Way

March 17th, 2009 1 comment

Coming back from Cuba, a dear colleague surprised me with his “side”-studies: supporting lucid dreaming via his mobile. He has done some very interesting studies on how to use the motion detection of his mobile to identify the REM-phases, and thus using an alert system to enable lucid dreaming. In my opinion, this is not only a fascinating idea, but he has come up with some remarkable effort and well-thought ideas, culminating in a python-software he wrote for his mobile.

I recommend reading his blog for anyone interested in his progress, or the idea of lucid dreaming in general. And, for giving him his first trackback and to show appreciation for mentioning me 😉 a link to his first article.


Oh yes, I haven’t yet put up the Cuba pictures, so sue me 😛