Sep 10, 2008

While jetlagged last week and unable to sleep, I wrote a really shit twitter client in python. It's utterly incomplete and really long for what it does (not a whole lot). So earlier today I wrote a python library for the twitter API. I figured there had to be an easier way to do it rather than write a whole bunch of code to implement each and every method. And there was - __getattr__ to the rescue.

>>> import pytwitter
>>> twitter = pytwitter.pytwitter()
>>> twitter.help_test()
'<ok>true</ok>'


In the above example, 'help_test' is converted to http://twitter.com/help/test.xml using the power of __getattr__. Full code is here: http://svn.evil.ie/python/pytwitter. The whole thing is around 100 lines long.
Some methods are not working, like direct_messages, as pytwitter attempts to use this as http://twitter.com/direct/messages.xml instead of the real url of http://twitter.com/direct_messages.xml. I'll add exceptions as I get around to it. Thanks twitter. Thwitter.

0 comments:

Post a Comment