Thursday, April 03, 2008

Pidgin and Twitter

I have been searching far and wide for something to turn my Pidgin status messages into Twitter tweets. My search, as well as your, is over:

http://arstechnica.com/reviews/apps/pidgin-2-0.ars/4

Scroll down, and you'll see this block of code:

#!/usr/bin/env python

import gtk, dbus, gobject, dbus.glib
import base64, httplib, urllib

TWITTER_USERNAME = "segphault"
TWITTER_PASSWORD = ""

PIDGIN_ACCOUNT = "segphault"
PIDGIN_PROTOCOL = "AIM"

def updateTwitter(username, passwd, statusmsg):
# Generate Twitter authentication header string
auth = {"Authorization": "Basic %s" %
base64.encodestring("%s:%s" % (username, passwd)).strip()}

# Create a connection to the Twitter web site

connection = httplib.HTTPConnection("twitter.com", 80)
# Use Twitter's REST API to post a status update
connection.request("POST", "/statuses/update.xml",
urllib.urlencode({"status":statusmsg}), auth)

# Initiate a connection to the Session Bus
bus = dbus.SessionBus()


# Associate Pidgin's D-Bus interface with Python objects
obj = bus.get_object(
"im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")

def onStatusChanged(acctID, old, new):
# Only update the Twitter status for one specific Pidgin account
if purple.PurpleAccountGetUsername(acctID) == PIDGIN_ACCOUNT and \
purple.PurpleAccountGetProtocolName(acctID) == PIDGIN_PROTOCOL:
# Retrieve the current Pidgin status message
message = purple.PurpleSavedstatusGetMessage(
purple.PurpleSavedstatusGetCurrent())

# Update Twitter status with current Pidgin status message
if message: updateTwitter(TWITTER_USERNAME, TWITTER_PASSWORD, message)

# Bind the onStatusChanged function to Pidgin's AccountStatusChanged event
bus.add_signal_receiver(onStatusChanged,
dbus_interface="im.pidgin.purple.PurpleInterface",
signal_name="AccountStatusChanged")


# Start the main loop
gobject.MainLoop().run()

Just paste that into a twitter.py file (for God sakes make sure you don't mess up the white space, this is Python!), set the Twitter/Pidgin configuration info at the top, and run it. Now, whenever your status changes, your Twitter is updated. Oh the magic of Dbus and REST

3 comments:

Daniele Muscetta said...

I actually thought of writing a plugin for Live Writer that would update twitter and/or facebook long time ago.
Dare also suggested it at one stage.
But it seems that the Live Messenger Plugin APIs have never made it out of beta and they have disappeared, at least that has been my experience when I actually wanted to TRY and write such a thing (read the comments).
But probably is just me not being an enough expert coder, as I see that there ARE plugins for MSN/Live messenger available on the net. Just wonder if they are written with supported or Unsupported methods/APIs.
But I see that you resorted to using Pidgin...

Dave Lowe said...

Yes, but can you go the other way? I want my latest tweet to be my Pidgin status.

Leonetix said...
This post has been removed by the author.