Azum

Modified Twitter feed authentication (API 1.1) for search

Aug 5th, 2013
180
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JQuery Twitter Feed. Coded by Tom Elliott (Web Dev Door) www.webdevdoor.com (2013)
  2. //UPDATED TO AUTHENTICATE TO API 1.1
  3. (function($) {
  4. $(document).ready(function () {
  5. var displaylimit = 20;
  6. var twittersearchtitle = "Tweets";
  7. var showretweets = true;
  8. var showtweetlinks = true;
  9. var showtweetactions = true;
  10. var showretweetindicator = true;
  11. var autorefresh = true;
  12. var refreshinterval = 120000; //Time to autorefresh tweets in milliseconds. 60000 milliseconds = 1 minute
  13. var refreshtimer;
  14.  
  15. var headerHTML = '';
  16. var loadingHTML = '';
  17. headerHTML += '<a href="https://twitter.com/" ><img src="http://yoursite.com/forums/images/twitter-bird-light.png" width="34" style="float:left;padding:3px 12px 0px 6px" alt="twitter bird" /></a>';
  18. headerHTML += '<h1>'+twittersearchtitle+'</h1>';
  19. loadingHTML += '<div id="loading-container"><img src="http://yoursite.com/forums/images/ajax-loader.gif" width="32" height="32" alt="tweet loader" /></div>';
  20.  
  21. $('#twitter-feed').html(headerHTML + loadingHTML);
  22.  
  23. if (autorefresh == true) {
  24. refreshtimer = setInterval(gettwitterjson, refreshinterval);
  25. }
  26.  
  27. gettwitterjson();
  28.  
  29. function gettwitterjson() {
  30. $.getJSON('tweets.txt?'+Math.random(),
  31. function(feeds) {
  32. feeds = feeds.statuses; //search returns an array of statuses
  33. //alert(feeds);
  34. var feedHTML = '';
  35. var displayCounter = 1;
  36. for (var i=0; i<feeds.length; i++) {
  37. var tweetscreenname = feeds[i].user.name;
  38. var tweetusername = feeds[i].user.screen_name;
  39. var profileimage = feeds[i].user.profile_image_url_https;
  40. var status = feeds[i].text;
  41. var showtweetactions = true;
  42. var showretweetindicator = true;
  43. var isaretweet = false;
  44. var isdirect = false;
  45. var tweetid = feeds[i].id_str;
  46. //If the tweet has been retweeted, mark as Retweet
  47. if(typeof feeds[i].retweeted_status != 'undefined'){
  48. isaretweet = true;
  49. };
  50.  
  51. if (((showretweets == true) || ((isaretweet == false) && (showretweets == false)))) {
  52. if ((feeds[i].text.length > 1) && (displayCounter <= displaylimit)) {
  53. if (showtweetlinks == true) {
  54. status = addlinks(status);
  55. }
  56.  
  57. if (displayCounter == 1) {
  58. feedHTML += headerHTML;
  59. }
  60.  
  61. feedHTML += '<div class="twitter-article">';
  62. feedHTML += '<div class="twitter-pic"><a href="https://twitter.com/'+tweetusername+'" ><img src="'+profileimage+'"images/twitter-feed-icon.png" width="42" height="42" alt="twitter icon" /></a></div>';
  63. feedHTML += '<div class="twitter-text"><p><span class="tweetprofilelink"><strong><a href="https://twitter.com/'+tweetusername+'" >'+tweetscreenname+'</a></strong> <a href="https://twitter.com/'+tweetusername+'" >@'+tweetusername+'</a></span><span class="tweet-time"><a href="https://twitter.com/'+tweetusername+'/status/'+tweetid+'">'+relative_time(feeds[i].created_at)+'</a></span><br/>'+status+'</p></div>';
  64. if ((isaretweet == true) && (showretweetindicator == true)) {
  65. feedHTML += '<div id="retweet-indicator"></div>';
  66. }
  67. if (showtweetactions == true) {
  68. feedHTML += '<div id="twitter-actions"><div class="intent" id="intent-reply"><a href="https://twitter.com/intent/tweet?in_reply_to='+tweetid+'" title="Reply"></a></div><div class="intent" id="intent-retweet"><a href="https://twitter.com/intent/retweet?tweet_id='+tweetid+'" title="Retweet"></a></div><div class="intent" id="intent-fave"><a href="https://twitter.com/intent/favorite?tweet_id='+tweetid+'" title="Favorite"></a></div></div>';
  69. }
  70.  
  71. feedHTML += '</div>';
  72. displayCounter++;
  73. }
  74. }
  75. }
  76.  
  77. $('#twitter-feed').html(feedHTML);
  78. //Add twitter action animation and rollovers
  79. if (showtweetactions == true) {
  80. $('.twitter-article').hover(function(){
  81. $(this).find('#twitter-actions').css({'display':'block', 'opacity':0, 'margin-top':-20});
  82. $(this).find('#twitter-actions').animate({'opacity':1, 'margin-top':0},200);
  83. }, function() {
  84. $(this).find('#twitter-actions').animate({'opacity':0, 'margin-top':-20},120, function(){
  85. $(this).css('display', 'none');
  86. });
  87. });
  88.  
  89. //Add new window for action clicks
  90.  
  91. $('#twitter-actions a').click(function(){
  92. var url = $(this).attr('href');
  93. window.open(url, 'tweet action window', 'width=580,height=500');
  94. return false;
  95. });
  96. }
  97.  
  98. });
  99. }
  100.  
  101. //Function modified from Stack Overflow
  102. function addlinks(data) {
  103. //Add link to all http:// links within tweets
  104. data = data.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
  105. return '<a href="'+url+'" >'+url+'</a>';
  106. });
  107.  
  108. //Add link to @usernames used within tweets
  109. data = data.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
  110. return '<a href="http://twitter.com/'+reply.substring(1)+'" style="font-weight:lighter;" >'+reply.charAt(0)+reply.substring(1)+'</a>';
  111. });
  112. return data;
  113. }
  114.  
  115.  
  116. function relative_time(time_value) {
  117. var values = time_value.split(" ");
  118. time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  119. var parsed_date = Date.parse(time_value);
  120. var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  121. var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  122. var shortdate = time_value.substr(4,2) + " " + time_value.substr(0,3);
  123. delta = delta + (relative_to.getTimezoneOffset() * 60);
  124.  
  125. if (delta < 60) {
  126. return '1m';
  127. } else if(delta < 120) {
  128. return '1m';
  129. } else if(delta < (60*60)) {
  130. return (parseInt(delta / 60)).toString() + 'm';
  131. } else if(delta < (120*60)) {
  132. return '1h';
  133. } else if(delta < (24*60*60)) {
  134. return (parseInt(delta / 3600)).toString() + 'h';
  135. } else if(delta < (48*60*60)) {
  136. //return '1 day';
  137. return shortdate;
  138. } else {
  139. return shortdate;
  140. }
  141. }
  142. });
  143. })(jQuery);
RAW Paste Data