7 Sep 2009 oubiwann   » (Journeyer)

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()



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!



Syndicated 2009-09-07 22:20:00 (Updated 2009-09-07 22:45:33) from Duncan McGreggor

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!