Filtering Out Twitterfeed Tweets
Develop | Posted December 05, 2008

Taking a cue from Guy Kawasaki, I registered the Smart Bear blog RSS feed on twitterfeed. The title of each blog entry we post will now get automatically put into a tweet by twitterfeed, along with a very convenient tinyurl. So for example, twitterfeed created this tweet for this blog entry.



Awesome! Only one small problem. In the sidebar of this blog we display our 3 most recent tweets. It seems kind of wasteful to display the tweets from twitterfeed because those tweets are just references to our blog entries.



Jason asked the obvious question: can we filter the tweets? The answer is "yes," but a bit of custom HTML and JavaScript are required. This blog is hosted by Typepad, so you will have to adjust the instructions accordingly for other platforms, but the concept would be the same. Regardless of platform, the key requirement is that you must have access to the templates for your blog/web site. With Typepad, this means using their Advanced Templates feature.



The tweets widget we use comes from twitter and uses twitter's web services API. The key portion of the widget's HTML is this bit:



1 <script type="text/javascript" 
2         src="http://twitter.com/javascripts/typepad.js">
3 </script>
4 <script type="text/javascript"
5         src="http://twitter.com/statuses/user_timeline/SmartBears.json?callback=twitterCallback2&amp;count=5">
6 </script>


The web service URL is http://twitter.com/statuses/user_timeline/SmartBears.json and as you might suspect, it returns JSON. Two parameters are being passed: a string to use as a wrapper around the result and the number of tweets to return. The string wrapper is just the name of a JavaScript method, which means the end result is that the method gets invoked with the JSON return value from the web service as a parameter. The JavaScript method that is invoked is at http://twitter.com/javascripts/typepad.js:



 1 function twitterCallback2(D){
2     var B=D;var A="";var E="";
3     for(var C=0;C<B.length;C++){
4         E=B[C].user.screen_name;
5         A+=('<li class="module-list-item"><span>'+
6             B[C].text+
7             '</span> <a style="font-size:85%" href="http://twitter.com/'+
8             E+
9             "/statuses/"+
10             B[C].id+
11             '">'+relative_time(B[C].created_at)+
12             "</a></li>")
13     }
14     document.getElementById("twitter_update_list").innerHTML=A
15 }


The fix is simple. I created my own version of twitterCallback2 and placed it in the sidebar template HTML for the blog. The changes are highlighted:



function sbTwitterCallback(D){
    var B=D;var A="";var E="";var F=0;
    for(var C=0;C<B.length;C++){
        if (B[C].source.indexOf('twitterfeed') != -1) {
            continue;
        }

        E=B[C].user.screen_name;
        A+=('<li class="module-list-item"><span>'+
            B[C].text+'</span> <a style="font-size:85%" href="http://twitter.com/'+
            E+
            "/statuses/"+
            B[C].id+
            '">'+relative_time(B[C].created_at)+
            "</a></li>");
        F++;
        if (F==3)
            break;

    }
    document.getElementById("twitter_update_list").innerHTML=A
};


I changed the method name and I added a check that skips over a tweet if the source of the tweet was twitterfeed. I also had to add a check to break out of the loop after 3 non-twitterfeed tweets had been located, since the maximum number of tweets we want displayed on the blog is 3.



The change to the HTML is even simpler - wrap the JSON from the web service with the name of the new JavaScript method and request 10 tweets instead of 5; this means I am assuming that within our 10 most recent tweets, at least 3 of them did not come from twitterfeed. I could leave off the count=10 and have the web service return all of our tweets, but that seems like a performance problem waiting to happen.  The modified HTML is:



<script type="text/javascript" 
        src="http://twitter.com/javascripts/typepad.js">
</script>            
<script type="text/javascript"
        src="http://twitter.com/statuses/user_timeline/SmartBears.json?callback=sbTwitterCallback&amp;count=10">
</script>

Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox