Python and Pidgin’s status on Ubuntu (Linux)

Posted on August 26th, 2009 at 18:01, in .com, Code, How To, Linux, Python, Ubuntu.

If you remember Fortune and Pidgin’s status on Ubuntu (Linux), Tudor has found a better solution to accomplish this using Python. I did argue with him a bit regarding the number of requests made (you can see that in the comments on his blog post) but finally I would agree that his solution was better than mine, only that I felt the need to improve it a bit. This still needs to set DBUS session parameters at log in time, and thanks to Earl Ruby this can be done easily too.

The script is pretty simple and I made it write a log to stdout, which with the help of cron can be piped to any file you want. Knock yourself out:

#!/usr/bin/python
# pidgin_status_change.py
"""
Handy script for changing Pidgin's status with a post from a feed passed as argument.
Usage:
        python pidgin_status_change.py your_feed_link                                

Authors:
        Tudor Barbu,    http://motane.lu
        Radu Cotescu,   http://radu.cotescu.com
"""                                            

import commands
import feedparser
import random
import os
import sys
import time

# feed url
FEED = []

def setFeed():
        if sys.argv is not None and len( sys.argv ) < 3:
                global FEED
                FEED = sys.argv[1]

def changeStatus():
        if len( FEED ) is not None:
                if commands.getoutput('pidof pidgin') is not None:
                        feed = feedparser.parse( FEED )
                        index = random.randint( 0, len( feed['items'] ) - 1 )
                        print time.strftime("%d-%m-%Y, %H:%M:%S", time.localtime())
                        print """\tStatus:\n\t%s\n\t%s""" % ( feed['items'][index].title.encode("utf-8"), feed['items'][index].link.encode("utf-8") )
                        status = 'purple-remote "setstatus?status=available&message=%s %s"' % ( feed['items'][index].title, feed['items'][index].link )
                        os.system(status.encode("utf-8"))

setFeed()
changeStatus()

# EOF

Download it from here.

Setting your DBUS session parameters requires exporting some variables to your environment. Take the script below and add it via System – Preferences – Startup Applications to the list of programs that start at log in. This way you’ll always be sure the needed variables are available.

#!/bin/bash
#Export the dbus session address on startup so it can be used by cron
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

Download this one from here.

Then you only need to set cron to do the job:

SHELL=/bin/bash
DISPLAY=:0.0
*/5 * * * * source ~/.Xdbus; /home/radu/scripts/pidgin_python/pidgin_status_change.py http://feeds2.feedburner.com/raducotescu >> /home/radu/crontab.log

Happy blogging!

Similar Posts:


One Comment

[...] the same principle as the script I wrote for updating Pidgin’s status with a random post from your blog’s feed, this new script uses as well a feed (the one provided by Twitter to every user), only that this [...]

Think you've got something to say?

For leaving comments including source code or terminal output, please use the the following tags like in the next example:

[language]
code lines
[/language]
where you substitute language with the programming laguage used throught the code example (for terminal output that would be bash), e.g:
[bash]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
[/bash]

To see a list of all the supported languages, please check this page.

If you want to include code bits inline, please use the code tags like in the following example:

The <code>$USER</code> variable holds the current logged in username.

Subscribe without commenting


Bear