Python DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


XML-RPC in Python
Pages: 1, 2

Getting a list of channels from Meerkat

Meerkat groups RSS channels into topics, called categories. For example, channels related to XML are in category 23. If you want to get a list of channels for a specific topic, you can query Meerkat this way:



>>> pprint(meerkatsvr.meerkat.getChannelsByCategory(23))
[{'title': '<XML>fr', 'id': 2991},
{'title': '4xt', 'id': 2559},
{'title': 'Eclectic', 'id': 555},
{'title': 'eXploringXML', 'id': 4471},
{'title': 'eXploringXML Channel', 'id': 1105},
{'title': 'finetuning com', 'id': 107},
{'title': 'finetuning.com', 'id': 4628},
{'title': 'Free XML tools', 'id': 906},
{'title': 'JabberCentral Recent Jabber News', 'id': 4655},
{'title': 'Moreover XML and metadata news', 'id': 2243},
{'title': 'moreover... XML and metadata news', 'id': 683},
{'title': 'My Userland', 'id': 1945},
{'title': "O'Reilly Network XML FAQs", 'id': 2365},
{'title': 'oreillynet.xml', 'id': 989},
{'title': 'oreillynet.xmldev', 'id': 990},
{'title': 'SOAP Webservices Resource Center', 'id': 2022},
{'title': 'XML About com', 'id': 2435},
{'title': 'XML News from PerlXML.com', 'id': 718},
{'title': 'XML XSL Portal', 'id': 4460},
{'title': 'XML.com', 'id': 47},
{'title': 'XML.com Resource Guide', 'id': 4637},
{'title': 'xmlhack', 'id': 724},
{'title': 'xmlTree Newsletter', 'id': 413}]

Deeper into XML-RPC and Meerkat

In my web client programming article, I demonstrated how we could easily download a listing of all Meerkat articles related to Linux, using Python's powerful URLLIB and a very simple script. Let's revisit that task using xmlrpclib. Employing an even simpler script, we can eliminate some of the ambiguities and awkwardness of the former approach and make a more robust application.

Fire up a new interactive session in Python, import xmlrpclib, and create a server instance for interacting with the Meerkat server:

>>> import xmlrpclib
>>> meerkatURI = http://www.oreillynet.com/meerkat/xml-rpc/server.php
>>> meerkatsvr = xmlrpclib.Server(meerkatURI)

Before we begin coding our updated solution, let's explore some features Rael Dornfest has built into the Meerkat XML-RPC server to document its capabilities. We've seen system.listMethods(). It returns an up-to-date list of the methods available on the server. You can obtain the method signature of any of these methods by calling methodSignature()

Related:

Java and XML

Java and XML
By Brett McLaughlin


>>> meerkatsvr.system.methodSignature(methodname)

This returns a two-element array containing the return value and parameter(s) of the method named. Note that you should not include enclosing parentheses after the method name.

Dornfest also provides a means of obtaining help about any method on the server


>>> meerkatsvr.system.methodHelp(meerkat.getItems)

To see these and all the other methods available to XML-RPC clients of Meerkat, go to the Meerkat XML-RPC Interface Test page. Selecting introspection from the drop-down list will give you all methods available, with their documentation. The URI, as its name suggests, also tests the Meerkat XML-RPC Server, returning both the method call and the server response. Nice.

Obtaining a list of Linux articles from Meerkat

As you may recall, the objective of the program in my web client programming article was to obtain links to Linux articles from Meerkat over the past hour. After building the framework to access the server through URLLIB, we constructed the URI:


http://www.oreillynet.com/meerkat/?p=5&t=1HOUR&_fl=minimal&_de=0&_ca=0&_ch=0&_da=0.

Imagine having to maintain the above line in an application! As a refresher, the URI tells Meerkat to give us all stories in the Linux category that have been posted in the last hour, using the minimal flavor. We want to further restrict the information coming back by eliminating the description, category, channel, and date fields. Let's replicate this functionality using xmlrpclib.

Declare a parameter list by constructing a Python dictionary (the same as a structure in XML-RPC):

>>> params = {
... 'category' : 7,
... 'time_period' : '1HOUR',
... 'descriptions' : 0,
... 'categories' : 0,
... 'channels' : 0,
... 'dates' : 0
    }

The first two parameters define the scope of the information we desire. The last four act as switches, turning off the various components returned. Next, we'll call a method on the server to obtain the desired information


>>> results = meerkatsvr.meerkat.getItems(params)

This returns a list of dictionaries containing both the title and an HTML link for all Linux articles that have appeared on Meerkat over the last hour. If your request returns an empty list, increase the time period (e.g. 2HOUR or 1DAY).

Now, let's loop through this list and construct full-fledged HTML links to the articles:

>>> for d in results:
...     print <A HREF=%s>%s</A> % (d[link],d[title])

What we have gained

This approach has given us several benefits. We have enhanced readability. We have less code. Presentation has been separated from data, allowing you to choose alternatives easily. Best of all, this approach has a distinctly Pythonic feel to it. Thanks to the functionality built into XML-RPC and xmlrpclib, the response from Meerkat is now Python data, with all the attendant flexibility: Pickle it, store it in a database, show it in a wxPython or Tkinter widget. Or even send it to another client via XML-RPC!

In the next article, I will dig deeper into the internals of xmlrpclib to show how Python objects are converted into XML-RPC requests and how return values are unmarshalled. We will also build an XML-RPC server that includes a testing framework.

Dave Warner is a senior programmer and DBA at Federal Data Corporation. He specializes in accessing relational databases with languages that begin with the letter P: Python, Perl, and PowerBuilder.


Discuss this article in the O'Reilly Network Python Forum.

Return to the Python DevCenter.

 





Sponsored by: