Get public Twitter timeline with jQuery ajax - with retweets


/ Published in: jQuery
Save to your folder(s)

This is a snippet for grabbing and displaying a public user twitter timeline from Twitter's API using jQuery ajax().
As a bonus, it also gives some hinting on how to test if its a retweet, and if so, grab the original tweeter's information - so the timeline looks more like twitter.com's timline.


Copy this code and paste it in your HTML
  1. $.ajax({
  2. type:'GET',
  3. dataType:'jsonp',
  4. url:'http://api.twitter.com/1/statuses/user_timeline.json',
  5. data:{screen_name:'USERNAME', include_rts:1}, //show retweets
  6. success:function(data, textStatus, XMLHttpRequest) {
  7. var tmp = false;
  8. var results = $('#twitter_results');
  9. console.log(data);
  10. for(i in data) {
  11. if(data[i].retweeted_status != null) {
  12. tmp = $('<li class="retweet" itemid="'+data[i].retweeted_status.id_str+'"><div class="dogear"></div><img src="'+data[i].retweeted_status.user.profile_image_url+'" alt="" align="left" width="48" height="48" /><cite>'+data[i].retweeted_status.user.screen_name+'</cite><p>'+data[i].retweeted_status.text.linkify_tweet()+'</p></li>');
  13. if(data[i].retweeted_status.favorited) {
  14. tmp.addClass('favorite');
  15. }
  16. } else {
  17. tmp = $('<li itemid="'+data[i].id_str+'"><div class="dogear"></div><img src="'+data[i].user.profile_image_url+'" alt="" align="left" width="48" height="48" /><cite>'+data[i].user.screen_name+'</cite><p>'+data[i].text.linkify_tweet()+'</p></li>');
  18. if(data[i].favorited) {
  19. tmp.addClass('favorite');
  20. }
  21. }
  22.  
  23. results.append(tmp);
  24. }
  25. },
  26. error:function(req, status, error) {
  27. alert('error: '+status);
  28. }
  29. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.