Related Post Widget for Blogger - Google Feed API

In this series of posts, we have defined an approach for developing a related post widget for blogger. We have also covered the steps of identifying the labels from a blog post and the eliminated the duplicates from the labels.

For all the qualified labels in 'filtArray', we are now ready to move on with the processing logic i.e. retrieve the related URLs.To do this, we have to include couple of Javascript libraries inside the HEAD tag. They are provided below

 <head>
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">
      google.load("gdata", "1.x", { packages : ["blogger"] });
    </script>
  </head>

It will be possible to search for a blog with a feed URL. For example, if the blog URL is 'http://thinktibits.blogspot.com' , to search for all 'ABC' posts from the blog ('ABC' as label), we can simply invoke a URL like http://thinktibits.blogspot.com/feeds/posts/default/-/ABC. We will be using this principle to retrieve the URLs alone from the result set.

A code snippet to achieve the same is given below

var feedUri,query;
for (var i = 0; i < filtArray.length; i++){
feedUri = 'http://thinktibits.blogspot.com/feeds/posts/default/-/' + filtArray [i];
query = new google.gdata.blogger.BlogPostQuery(feedUri);
bloggerService.getBlogPostFeed(query, handleQueryResults, handleError);
}

where, our bloggerService is defined as

var bloggerService = new google.gdata.blogger.BloggerService('com.appspot.interactivesampler');

When we invoke the getBlogPostFeed, the result is redirected to 'handleQueryResults' on success and to 'handleError' upon failure. Google code palyground has got a lot of interesting examples on this and it would be a good time pass to understand all of them.

Inside this 'handleQueryResults', we will put in some code to get the URLs alone from the result and frame a HTML out of it.

var finalurlarray1 = new Array();
var handleQueryResults = function(resultsFeedRoot) {
var blogFeed = resultsFeedRoot.feed;
var html,postTitle,entryUri;
var postEntries = blogFeed.getEntries();
for (var i = 0; postEntry = postEntries[i]; i++) {
postTitle = postEntry.getTitle().getText();         
entryUri = postEntry.getHtmlLink().getHref();
html = '<li>' + '<a href= '+entryUri+'>'+ postTitle + '</a>' + '</li>';
finalurlarray1.push(html);
}
}

We take the output 'resultsFeedRoot' and get the 'Post Title' and URL of the 'Post Title' into two variables viz postTitle and entryUri.We then construct a HTML list with that with the traditional 'href' tag and push the resulting link to an array => 'finalurlarray1'. This 'finalurlarray1' might contain duplicates as well, as two different labels on a blog can still point to a single post.Eliminating them and writing the output is covered in  the next post.




No comments:

Post a Comment