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
* Original Bird Icon by playground.ebiene.de
Yet another great jQuery plugin my friend!
thanks @Lam :)
haha, thanks for the mention Mike!
you most welcome @Louis
Really useful plugin, do you mind if I add this tutorial link in my site :)
Thanks
@TutorialPortals,
Thanks, linkbacks are always welcome :)
Hmm, looks like this is bringing up inaccurate results these days.
@Kevin,
why?