Twitter Search Results With JSON and Callbacks

A question was posed in the comments on my JSON Feeds For Fun and Profit Part 2 – Callbacks with Twitter post about handling Twitter search results for a #hashtag. As with the rest of Twitter’s API, dealing with search results is relatively straightforward. This post will examine how to handle a #hashtag search.

If you’re not familiar with JSON or callbacks you might want to read the above referenced post to get up to speed.

Otherwise… it’s on.

With the previous example in mind, the only real difference between this and the status request is the format of the returned object. Let’s take a look that should clarify the differences.

This is the status structure:

  • anonymous array with one member containing…
    • anonymous object(s) representing returned statuses
      • created_at
      • user
      • anonymous object
        • followers_count
        • description
        • screen_name
        • url
        • name
        • protected
        • profile_image_url
        • location
        • id
      • truncated
      • in_reply_to_status_id
      • favorited
      • id
      • source
      • text

And here’s the structure for the search object:

  • anonymous object containing
    • results which contains an…
      • Anonymous array(s) representing matching tweets
        • text
        • to_user_id
        • from_user
        • id
        • from_user_id
        • iso_language_code
        • profile_image_url
        • created_at
      • since_id
      • max_id
      • refresh_url
      • results_per_page
      • next_page
      • page
      • query

As you can see they’re related, but different. So need to use a different approach to get to the data. Here’s how I’m doing it…

function twitterSearch(obj) {
//this is the div I'm writing the content to
var tDiv = document.getElementById("twitter");
var user, bgcolor, tweet, postedAt, icon, userURL;
//start the ul
tDiv.innerHTML = "<ul>"
for (i=0;i<obj.results.length;i++) {
//Look at me use the JavaScript modulus operator to do even/odd rows.
if(i % 2) {
bgcolor="#efefef"
} else {
bgcolor="#ddd"
}
//we need to get some data out of the object
//and populate some variables.
//i could do this inline in the string below,
//but this is way easier for you to read
icon = obj.results[i].profile_image_url;
user = obj.results[i].from_user;
userURL = "http://twitter.com/"+user;
tweet = obj.results[i].text;
postedAt = obj.results[i].created_at;
//and here I mash it all up into a fancy li
tDiv.innerHTML +="<li style='background-color:"+bgcolor+"; background-image: url("+icon+")'><strong><a href='"+userURL+"'>"+user+"</a></strong>: "+tweet+" <span class='time'>("+postedAt+" GMT)</span> </li>";
}
//and close the UL
tDiv.innerHTML += "</ul>";
}
//this is basically the same function I was using before //with the changed search URL function twitter() {
var twitterJSON = document.createElement("script");
twitterJSON.type="text/javascript"
//here's the search URL twitterJSON.src="http://search.twitter.com/search.json?callback=twitterSearch&q=%23css"
document.getElementsByTagName("head")[0].appendChild(twitterJSON);
return false;
}

And this is what it looks like (click the link for twitter magic):

7 thoughts on “Twitter Search Results With JSON and Callbacks

  1. Thanks for the help with this. I did make a change to the formatting:

    tDiv.innerHTML +=""+user+": "+tweet+" ("+postedAt+" GMT) ";

    The change is for the avatar. You need to remove the space before the colon at url and add a no-repeat.

    This helped me greatly. Thanks again.

  2. Oh, sorry, I moved those into the attached style sheet.

    Glad to be of service, it was an interesting post for me to write (did you hear that folks? Pose interesting enough questions and you might just get an answer.)

  3. This is probably a dumb question, but how would you go about enabling the script to start without having to click to view the query?

Leave a Reply

Your email address will not be published. Required fields are marked *