Monday, September 07, 2009

Twisted Ping.fm Client


I just merged async (Twisted) support into the Python ping.fm library today and have just taken it for a test drive. I do love Twisted :-) The Twisted pyngfm API usage is identical to the synchronous API, with the usual exception of using deferreds and callbacks.

Here's some example usage, the client I now use for command-line updates to Twitter, Identi.ca, Tumblr, Facebook, LinkedIn, Jaiku, and even Flickr (note that the keys are stored in an .ini-style config file):

#!/usr/bin/python
import sys
from ConfigParser import SafeConfigParser
from twisted.internet import reactor
from pyngfm.client import PingFMAsyncClient
def checkMessage(message):
if len(message) > 140:
print "Message is too long! (%s chars)" % len(message)
sys.exit(1)
def getKeys():
cred_file = "/etc/ping.fm.creds"
config = SafeConfigParser()
config.read([cred_file])
api_key = config.get("default", "api-key")
user_app_key = config.get("default", "user-app-key")
return api_key, user_app_key
def pingIt(message):
def check_result(status):
print status
def check_error(error):
print error.getErrorMessage()
def finish(ignored):
reactor.stop()
pinger = PingFMAsyncClient(*getKeys())
deferred = pinger.user_post("status", body=message)
deferred.addErrback(check_error)
deferred.addCallback(check_result)
deferred.addErrback(check_error)
deferred.addCallback(finish)
if __name__ == "__main__":
message = " ".join(sys.argv[1:])
checkMessage(message)
pingIt(message)
reactor.run()
view raw pingfm.py hosted with ❤ by GitHub

There's another example in the README that iterates through the recents posts made to ping.fm. If you do manage to use it and come across any issues, be sure to file a ticket.

Enjoy!



1 comment:

Снежный человек said...

Cool library, Mr. McGreggor!

Any chance to see a simple PyGtk app that just sits in GNOME panel and when clicked, allows to type in a status message and pings it via this library? That's be a KILLER app!