Monday, November 19, 2007

Pulling twitter updates with JSON and jQuery

I wrote a little script tonight to pull in my latest 3 tweets from twitter and display them on my blog.  You can see it in action on the right.

Here is how I did it.  I used the Twitter's API and called my timeline with a JSON call and comsummed it with jQuery and outputted it to a blank div.

       var url = "http://twitter.com/status/user_timeline/RedWolves.json?count=3&callback=?";        $.getJSON(url,         function(data){             $.each(data, function(i, item) {                 $("img#profile").attr("src", item.user["profile_image_url"]);                 $("#tweets ul").append("<li>"                       + item.text.linkify()                       + " <span class='created_at'>"                       + relative_time(item.created_at)                       + " via "                       + item.source                       + "</span></li>");             });         });

Basically what this does is pulls in the data from twitter and makes them available as objects.  I then loop through each item and pull out the data I want and write it out to a unordered list. Update: make sure to look at the complete working example below as it has the two functions this code block is using (linkify and relative_time) to transform the JSON data into how I'd like it to look.

Here is the HTML stub it's going to:

   <div id="tweets">         <img id="profile">         <ul></ul>     </div>

You can download a working example here: twitter-json-jquery.html (1.79 KB)

Saturday, June 07, 2008 4:17:15 PM (Eastern Standard Time, UTC-05:00)
Found out that dooce.com is using the above twitter/jQuery code. The Announcement.

Is anyone else using this code on their website? if so drop me the link.
Thursday, October 30, 2008 9:42:33 AM (Eastern Standard Time, UTC-05:00)
Ralph - I'm finding that this code doesn't seem to work when the page is refreshed. I can reproduce the problem on your example page. When refreshing, no data is shown. I've seen this with my own code as well. The public timeline requests don't have an API limit, so I don't think that is the problem.

It looks like the problem has to do with the callback. The callback doesn't appear to be correctly named when the request is returned:

Error: jsonp1225377433525 is not defined
Source File: http://twitter.com/status/user_timeline/kindohm.json?count=3&callback=jsonp1225378000592&_=1225378000622
Line: 1

Thoughts?
Friday, November 07, 2008 6:11:06 PM (Eastern Standard Time, UTC-05:00)
@mike, @Ralph - I ran into the same issue Mike. Requests of the page cause an error. It takes a while before the problem goes away and you can see the tweets again. I've been looking into how to fix this but having come up with anything. Still working on it though...
Tuesday, November 18, 2008 12:41:23 AM (Eastern Standard Time, UTC-05:00)
I am not sure but I think it's either a bug with jqueries getJSON method and it's substituting the callback=? to the callback method name. Or it's a bug with the Twitter API as the JSON that comes back after jQuery adds the callback method is not valid.

I'll start with submitting a bug with jQuery and see how far I get.

Thanks for finding this bug.
Monday, January 12, 2009 3:45:52 PM (Eastern Standard Time, UTC-05:00)
Thanks a lot, I have used this as the basses of my own twitter parser, I thought you might be interested in my "atify" function that I did inspired by your "linkify" function to turn @replies into links to the ated user's account on twitter:

<code>
String.prototype.atify = function() {
return this.replace(/@[\w]+/g, function(m) {
return "<a href='http://www.twitter.com/"+m.replace('@','')+"'>"+m+"</a>";
});
};
</code>

you can chain it with linkify, but it should go AFTER linkify (or else linkify will linkify the links that atify makes!) like so:

<code>
tweet.text.linkify().atify();
</code>

cheers!


Monday, January 12, 2009 3:55:35 PM (Eastern Standard Time, UTC-05:00)
Oh wow that's awesome. Thanks, I'll add it in.
Tuesday, January 13, 2009 9:52:47 AM (Eastern Standard Time, UTC-05:00)
@mike @aaron I think twitter fixed thier API finally I am not able to replicate the refresh problem.
Sunday, February 15, 2009 2:02:56 PM (Eastern Standard Time, UTC-05:00)
I just blogged about a jQuery plugin for displaying Twitter feeds. It's quite useful and if nothing else provides a great example of how jQuery can be used to parse RSS feeds.

http://blarnee.com/wp/a-jquery-and-twitter-plugin
Thursday, March 05, 2009 4:02:09 PM (Eastern Standard Time, UTC-05:00)
Hi! Using parts of your code on my blog to display my last tweets.
Great work!
Saturday, April 04, 2009 3:19:01 AM (Eastern Standard Time, UTC-05:00)
Hello,
thank you for the help.
I have a little problem with the function relative_time. My site is in russian language and the grammatical is different.
e.g.
In english you say 1 DAY, 2,3,4,5, etc DAYS
but in russian is so:
1 ДЕНЬ (DEN')
2,3,4 ДНЯ (DNIA)
5 and > ДНЕЙ (DNEI)

For minutes, months, and years is the same.
How can i fix it?
Regards
selimoff
Tuesday, May 26, 2009 6:39:56 AM (Eastern Standard Time, UTC-05:00)
A friend of my sent me a link to this post, but copy-pasting the above code did not work for me. I am new to jQuery and all the web thing, but found <a href="http://coda.co.za/blog/2008/10/26/jquery-plugin-for-twitter" rel="nofollow">coda's</a> jquery plugin and <a href="http://jquery-howto.blogspot.com/2009/04/jquery-twitter-api-plugin.html">this article useful</a>.
Ammy
Tuesday, May 26, 2009 8:45:07 AM (Eastern Standard Time, UTC-05:00)
Ammy pasting the above code is missing two important functions. If you download the source code file you'll see everything.
Sunday, June 21, 2009 1:11:26 PM (Eastern Standard Time, UTC-05:00)
Wow nice tutorial. I ahve also used twitter api in my application.I am not getting how to do pagination for twitter.Could you help me?
Deepali Kherudkar
Saturday, June 27, 2009 4:22:56 PM (Eastern Standard Time, UTC-05:00)
Thanks man great tut you're a life saver. I got it working now, the callback=? part was what I was missing, it didn't work for me with out it.
Is the callback part of the url important or did I probably make a mistake some where else?
Mohammed
Name
E-mail
Home page

Comment (Some html is allowed: )  

Enter the code shown (prevents robots):

Blog Posts by:

Currently Viewable:

My Twitter Updates

View Twitter Page