Related Post Widget for Blogger - Filter URLs and Display Related

Till this point, we have used the Google Feed API to get all the related URLs for a given blog post. We have also invoked it iteratively and pushed all the resulting URLs into an Array.

The step that is left over is to filter the duplicate URLs from that array and stamp the result to an output HTML file. Once this is done, we have a running code for us, and to integrate it to blogger, we have to do some additional stuff (like converting < to &lt; and so on..). Before we move on, when we invoked the 'getBlogPostFeed' we can get errors back in the call (time out etc). In such cases, we can handle the errors in the 'handleError' function. A sample of that is provided below

var handleError = function(error) {
document.write("unable to load the related posts now");
};

The 'handleQueryResults' call will have to be slightly modified to fit in a requirement of adding these additional functionality. The modified function is provided below. (additions are given in red)

var funcvar1=0;
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);
}
funcvar1++;
if (funcvar1 == filtArray.length) {
var urlarrayfinal=eliminateDuplicates(finalurlarray1);
for (var i = 0; i < urlarrayfinal.length; i++){
document.write(urlarrayfinal[i]);
}
}
};  

We maintain a loop counter to detect, if the label that is being processed is the last label to be processed. If it is the last label. (compared with our label array), then we invoke 'eliminateDuplicates' passing the URL array as an input. We get a filtered array, 'urlarrayfinal' which contains the final set of URLs to be displayed as related posts.

We loop through this array and use the document.write method to push the output in the browser. That completes the pseudo logic description of creating a blogger widget to display the related posts.We now have the task of integrating this pseudo code into blogger.

No comments:

Post a Comment