Radu Cotescu's professional blog

g33k w17h pa45510n

Python and Pidgin's Status on Ubuntu (Linux)

Tudor has found a better solution to advertise one’s blog posts with the help of Pidgin and a little bit of 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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

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.

1
2
3
4
5
6
7
8
9
#!/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

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

1
2
3
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

You can download everything from here.

Happy blogging!

.com, Code, How To, Linux, Python, Ubuntu

« Resetting Windows passwords for every user Windows 7 review from a Linux user »

Comments