playing with twitter api

i’m just playing with twitter api and jquery, so i write a function to get some tweets using twitter search, and passing hashtags to the search, we need to use jsonp, because the xss error :P

So the function:

function twitterSearch()
{
var url = “http://search.twitter.com/search.json?q=%23nowplaying”;

if(typeof(lastId)!=”undefined”)

{
var url = “http://search.twitter.com/search.json?q=%23nowplaying&since_id=”+lastId;
}
$.ajax({

type: “GET”, url: url, async:true, contentType: “application/json; charset=utf-8”, dataType: “jsonp”,
success: function(data) {
var long = data.results.length;
if(data.results!=”“)
{
lastId = data.results[0].id;   
}
for (i=0 ; i< long; i++)
{
var send= data.results[i];   
var text2insert = “<div id="tweet"><img src="” + send.profile_image_url + “"/><div class="texto" id="”+send.id+”"  >”+send.from_user+”: “+send.text+”</div></div>”;

$(‘#tweets’).fadeIn(‘slow’).append(text2insert);

}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(‘error can’t load data source’);
}
});
}

and we need a function to call our update tweet function:

$(function() {
setInterval(twitterSearch, 10000);
});

and in the document ready:

var lastID = “undefined”;

twitterSearch();

The HTML only needs a div

<div id=”tweets”></div>

and you need to write some css to order the information retreived

So again we are using jsonp to get the information using an ajax request with jquery, the lastId var is to know about the las tweet that we got in the div, if we don’t use this variable, then the tweets div are being populated with repeated tweets ;)

And more information about twitter API here

And more information about twitter API search here