skip to main | skip to sidebar

Using jQuery to Get the Nth Twitter Status
As of my ongoing preparation for the 1K tweet :) I was interested to see the 1000th tweet from some friends timeline. And when I didn't find an existing method, I thought I could write few jQuery lines to solve this..

Twitter provides a RESTful API to query and page through tweets in JSON format (which is the simplest format for a pure Javascript solution).
So,  this code makes 2 API calls to solve it.

1- Get User's Tweets Count

This code uses jQuery "ajax" function to call "users/show" method to get "statuses_count" which is the tweets count of that user. and then upon success it calls the 2nd function "getLatestTweet".
function getTweetCount(username,n) {
   $.ajax({
      url:'http://twitter.com/users/show.json'
      ,data:{screen_name:username}
      ,dataType:'jsonp'
      ,success:function(json){
         // Can not go back more than 3200 tweet
         var min = json.statuses_count-3200;
         // min can not be less than 0
         if(min<0) min =0;
         // n has to be greater than min and less than tweet count
         if(n>min && n<=json.statuses_count) {
            // page = count - Nth + 1
            getLatestTweet(username, json.statuses_count - n + 1);
         }
      }
   });
}

You can see now 2 calculations,
1st equation is there because Twitter only keeps your latest 3200 tweets!!
2nd one to calculate the page index to use in next function since tweets are sorted starting from the latest.

2- Get User's Nth Tweet

This simply calls "statuses/user_timeline" method to get 1 tweet at a page index that was calculated in previous step.
function getLatestTweet(username,page) {
   $.ajax({
      url:'http://twitter.com/statuses/user_timeline.json'
      ,data:{screen_name:username,count:1,page:page}
      ,dataType:'jsonp'
      ,success:function(json){
         if(json.length) showTweet(json[0]);
      }
   });
}

3200 Tweets Only!

I wasn't really happy to know that we can only access the latest 3200 statuses from a timeline due to Twitter pagination limits. But it's just another cause for services like Twitter backup to exist!
So, for a web designer like Louis Gubitosi (56,356 tweets!) this code can only get a tweet between 53,157 and 56,356.

* Original Bird Icon by playground.ebiene.de

8 comments

  1. Lam Nguyen // February 13, 2010 at 7:31:00 AM GMT+11  

    Yet another great jQuery plugin my friend!

  2. Mike // February 13, 2010 at 7:37:00 AM GMT+11  

    thanks @Lam :)

  3. Louis Gubitosi // May 26, 2010 at 12:33:00 AM GMT+10  

    haha, thanks for the mention Mike!

  4. Mike // May 26, 2010 at 4:51:00 AM GMT+10  

    you most welcome @Louis

  5. TutorialPortals // July 18, 2010 at 2:01:00 AM GMT+10  

    Really useful plugin, do you mind if I add this tutorial link in my site :)

    Thanks

  6. Mike // July 18, 2010 at 2:05:00 AM GMT+10  

    @TutorialPortals,
    Thanks, linkbacks are always welcome :)

  7. Kevin Ebaugh // March 3, 2012 at 7:17:00 AM GMT+11  

    Hmm, looks like this is bringing up inaccurate results these days.

  8. Mike // March 3, 2012 at 8:13:00 AM GMT+11  

    @Kevin,
    why?

Post a Comment

Thank you for taking the time to comment..
* If you have a tech issue with one of my plugins, you may email me on mike[at]moretechtips.net

Mike

Mike MoreWeb developer, jQuery plugin author, social media fan and Technology Blogger.
My favorite topics are: jQuery , Javascript , ASP.Net , Twitter , Google..
<connect with="me"> </connect>

Subscribe by email

Enter your email address:

or via RSS