I am currently redeveloping a website and a new feature is being able to connects to Technorati to return a list of all the blog posts tagged with a particular term and display them on the site.
Technorati is a very simple API to connect to using a REST-ful interface. It returns an XML response containing information about the result set and then a collection of items which correspond to the tagged blog posts.
The response looks like this:
xml version="1.0" encoding="utf-8"?> DOCTYPE tapi PUBLIC "-//Technorati, Inc.//DTD TAPI 0.02//EN" "http://api.technorati.com/dtd/tapi-002.xml"> <tapi version="1.0"> <document> <result> <query>[tag]query> <postsmatched>[number of posts that match the tag]postsmatched> <blogsmatched>[number of blogs that match the tag]blogsmatched> <start>[value of the start parameter]start> <limit>[value of limit parameter]limit> <querytime>[duration of query]querytime> result> <item> <weblog> <name>[name of blog containing match]name> <url>[blog URL]url> <rssurl>[blog RSS URL]rssurl> <atomurl>[blog Atom URL]atomurl> <inboundblogs>[inbound blogs]inboundblogs> <inboundlinks>[inbound links]inboundlinks> <lastupdate>[date blog last updated]lastupdate> weblog> <title>[entry title]title> <excerpt>[entry blurb with term highlighting]excerpt> <created>[date entry created]created> <postupdate>[date post last updated]postupdate> <permalink>[blog entry URL]permalink> item> document> tapi>
You make a request to the API using either HTTP GET or HTTP POST requests, this makes it really easy to use within C#, as discussed in a moment. To make a request, you hit the URL http://api.technorati.com/tag?key=[apikey]&tag=[tag] replacing the [apikey] for the API which Technorati provides you, and replace [tag] with the term you want for all the posts to return. There is also some optional fields which can be included to allow you to customize the response, for example limiting how many responses are actually returned (default is 20).
Onto the code, the first task is to construct a string for the URL setting any parameters which you wish to use as the request.
string url = string.Format("http://api.technorati.com/tag?key={0}&tag={1}&limit={2}excerptsize={3}", technoratiAPI, technoratiTerm, limit, excerptsize);
I then use the URL together with the XmlDocument and XmlTextReader to make the request and load the document into memory for processing. The XmlTextReader will deal with making the request to the URL and processing the response, we just need to provide it with the correct URL. I then use a XPath query to select all of the items/blogs returned from the http request and store them in a collection.
1 XmlDocument xmlResultsSet = new XmlDocument(); 2 xmlResultsSet.Load(new XmlTextReader(url)); 3 XmlNodeList xmlResponse = xmlResultsSet.SelectNodes("/tapi/document/item");
We then use a foreach loop to cycle over each blog post in the collection.
foreach (XmlNode post in xmlResponse)
I have created an object Blog to allow me to store the information, this is a set of properties relating to the XML file. Within the foreach body, I create a new Blog object and populate the new object with the information I want from the XML response, if you download the full code method I have also processed the related Weblog.
1 Blog blog = new Blog(); 2 blog.Title = post["title"].InnerText; 3 blog.Description = post["excerpt"].InnerText; 4 blog.Created = Convert.ToDateTime(post["created"].InnerText); 5 blog.Url = post["permalink"].InnerText; 6 taggedPosts.Add(blog);
I then have a generic collection to store all of the Blog items to allow it to be access in the UI, in my case I have bound it to a Repeater control in ASP.net.
List<Blog> taggedPosts = new List<Blog>();
You then have a generic collection of Blog objects which relate to tagged blog posts from Technorati. Very cool and very simple.
Details of the API can be found at http://technorati.com/developers/api/tag.html
Sample code can be downloaded here:
http://blog.benhall.me.uk/code/CSharp/Technorati/GetTechnoratiPosts.txt
http://blog.benhall.me.uk/code/CSharp/Technorati/BlogWeblogObject.txt
http://blog.benhall.me.uk/code/CSharp/Technorati/ASPRepeater.txt
I’m using a new Live Writer plugin I found for code highlighting, I think its cool and a big improvement over the previous plugin I tried. One improvement to it would be to trim whitespace from the beginning of each line, as when copying from VS it keeps the tabs and makes it look a mess. Hope you like it.
http://stevedunns.blogspot.com/2006/08/code-formatter-plugin-for-windows-live.html