A Simple Technorati Client

May 13, 2003 | Fredrik Lundh

The following is a minimal technorati client, using the generic HTTPClient class and the ElementTree library.

 
from HTTPClient import HTTPClient
from elementtree.HTMLTreeBuilder import TreeBuilder
from urllib import urlencode

class TechnoratiClient(HTTPClient):

    def __init__(self, apikey):
        self.apikey = apikey
        HTTPClient.__init__(self, "http://api.technorati.com")

    def __getattr__(self, method):
        def method(_self=self, _method=method, **params):
            return _self.do_request("", path="/%s?%s&key=%s" % (
                _method, urlencode(params), _self.apikey
                ), method="GET", parser=TreeBuilder())
        return method

Note that I’m using the HTMLTreeBuilder instead of the default XML parser. The reason for this is that the XML generated by the technorati service can contain HTML character entities (defined by an external DTD). If you pass the technorati data to the standard parser, it will most likely complain about unknown entities.

Here’s how to use this class to get the technorati cosmos for online.effbot.org:

>>> client = TechnoratiClient("...your api key...")

>>> tree = client.cosmos(url="online.effbot.org")

>>> import sys
>>> tree.write(sys.stdout)
<tapi version="0.9">
<document>
<result>
  <url>http://online.effbot.org</url>
  <weblog>
    <name>online.effbot.org</name>
    <url>http://online.effbot.org</url>
    <rssurl>http://online.effbot.org/rss.xml</rssurl>
    <inboundblogs>8</inboundblogs>
    <inboundlinks>13</inboundlinks>
    <lastupdate>1970-01-01 00:33:23 GMT</lastupdate>
  </weblog>
  <inboundblogs>8</inboundblogs>
  <inboundlinks>13</inboundlinks>
  <rankingstart>1</rankingstart>
</result>
<item>
  <weblog>
    <name>PyZine RE: Bryan</name>
    <url>http://www.pyzine.com</url>
    <rssurl />
...
>>> for item in tree.getiterator("item"):
...     print item.find("weblog").findtext("name")
...
PyZine RE: Bryan
PyZine RE: Bryan
PyZine RE: Bryan
PyZine RE: Bryan
Zope Notes
Zope Notes
Small Values of Cool
Joao Prado Maia's weblog
Python owns us
...

You can find a full list of Technorati methods here. Just call the corresponding client method, and pass in the parameters as keyword arguments.

 

A Django site. rendered by a django application. hosted by webfaction.