g.raphaelli's weblog

Archive for April 2009

libyahoo2 python bindings

written by g, on Apr 3, 2009 9:23:00 AM.

I have wanted Python bindings for libyahoo2 for a long time and finally sat down with Ned Batchedler's Pycon '09 slides, A Whirlwind Excursion through Python C Extensions, and wrote some basic ones available at:

Thanks Ned for the great presentation. He's right that it's just an introduction as things like calling python from C couldn't even be covered. I would recommend that aspiring module writers review Ned's presentation, move on to Extending and Embedding the Python Interpreter, and then bookmark the crap out of the Python/C API Reference Manual.

Back to the bindings - a simple bot that will parrot your words back to you looks like this:

import YmsgrClient

class ParrotBot(YmsgrClient.YmsgrClient):
    def got_im(self, who, msg, timestamp):
        self.send_im(who, msg)

b = ParrotBot(username, password)
b.login(YmsgrClient.STATUS_AVAILABLE)

There are a number of rough edges and simply bad ideas in the code but it is sufficient for writing a single account client. We've been using this for our bot at work for a few days without problems [or segfaults]. What we have had is an increase in bot functionality, as the barrier to writing few lines of Python and restarting the bot is much lower than writing a bunch of C and recompiling repeatedly until it works [or doesn't, giving up, and rolling it all back].

Some things I'd like to do immediately:

  • Convert the data structures and functions lifted directly from the libyahoo2 sample client to Python objects and expose them
  • Redo the login poll loop or at least allow callbacks to be scheduled somehow
  • Add Documentation
  • Throw better exceptions and use the logging module properly
Some things that someone should do eventually:
  • Add Webcam support and sample usage documentation
  • Test functionality on Windows

Contributions are welcome. Constructive criticism is greatly appreciated.