1. (function($) {
  2.  
  3. $.fn.tweet = function(o){
  4. var s = {
  5. username: ["DJ_SOULMAN","SOUL_MGMT"], // [string] required, unless you want to display our tweets. :) it can be an array, just do ["username1","username2","etc"]
  6. avatar_size: null, // [integer] height and width of avatar if displayed (48px max)
  7. count: 5, // [integer] how many tweets to display?
  8. intro_text: null, // [string] do you want text BEFORE your your tweets?
  9. outro_text: null, // [string] do you want text AFTER your tweets?
  10. join_text: null, // [string] optional text in between date and tweet, try setting to "auto"
  11. auto_join_text_default: "we said,", // [string] auto text for non verb: "i said" bullocks
  12. auto_join_text_ed: "we", // [string] auto text for past tense: "i" surfed
  13. auto_join_text_ing: "we are", // [string] auto tense for present tense: "i was" surfing
  14. auto_join_text_reply: "we replied to", // [string] auto tense for replies: "i replied to" @someone "with"
  15. auto_join_text_url: "we were looking at", // [string] auto tense for urls: "i was looking at" http:...
  16. loading_text: null, // [string] optional loading text, displayed while tweets load
  17. query: null // [string] optional search query
  18. };
  19.  
  20. $.fn.extend({
  21. linkUrl: function() {
  22. var returning = [];
  23. var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
  24. this.each(function() {
  25. returning.push(this.replace(regexp,"<a class=\"readmore\" href=\"$1\">$1</a>"))
  26. });
  27. return $(returning);
  28. },
  29. linkUser: function() {
  30. var returning = [];
  31. var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
  32. this.each(function() {
  33. returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\">@$1</a>"))
  34. });
  35. return $(returning);
  36. },
  37. linkHash: function() {
  38. var returning = [];
  39. var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
  40. this.each(function() {
  41. returning.push(this.replace(regexp, ' <a href="http://search.twitter.com/search?q=&tag=$1&#9001;=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'))
  42. });
  43. return $(returning);
  44. },
  45. capAwesome: function() {
  46. var returning = [];
  47. this.each(function() {
  48. returning.push(this.replace(/(a|A)wesome/gi, 'AWESOME'))
  49. });
  50. return $(returning);
  51. },
  52. capEpic: function() {
  53. var returning = [];
  54. this.each(function() {
  55. returning.push(this.replace(/(e|E)pic/gi, 'EPIC'))
  56. });
  57. return $(returning);
  58. },
  59. makeHeart: function() {
  60. var returning = [];
  61. this.each(function() {
  62. returning.push(this.replace(/[<]+[3]/gi, "<tt class='heart'>&#9829;</tt>"))
  63. });
  64. return $(returning);
  65. }
  66. });
  67.  
  68. function relative_time(time_value) {
  69. var parsed_date = Date.parse(time_value);
  70. var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  71. var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  72. if(delta < 60) {
  73. return 'less than a minute ago';
  74. } else if(delta < 120) {
  75. return 'about a minute ago';
  76. } else if(delta < (45*60)) {
  77. return (parseInt(delta / 60)).toString() + ' minutes ago';
  78. } else if(delta < (90*60)) {
  79. return 'about an hour ago';
  80. } else if(delta < (24*60*60)) {
  81. return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  82. } else if(delta < (48*60*60)) {
  83. return '1 day ago';
  84. } else {
  85. return (parseInt(delta / 86400)).toString() + ' days ago';
  86. }
  87. }
  88.  
  89. if(o) $.extend(s, o);
  90. return this.each(function(){
  91. var list = $('<ul class="tweet_list">').appendTo(this);
  92. var intro = '<p class="tweet_intro">'+s.intro_text+'</p>'
  93. var outro = '<p class="tweet_outro">'+s.outro_text+'</p>'
  94. var loading = $('<p class="loading">'+s.loading_text+'</p>');
  95. if(typeof(s.username) == "string"){
  96. s.username = [s.username];
  97. }
  98. var query = '';
  99. if(s.query) {
  100. query += 'q='+s.query;
  101. }
  102. query += '&q=from:'+s.username.join('%20OR%20from:');
  103. var url = 'http://search.twitter.com/search.json?&'+query+'&rpp='+s.count+'&callback=?';
  104. if (s.loading_text) $(this).append(loading);
  105. $.getJSON(url, function(data){
  106. if (s.loading_text) loading.remove();
  107. if (s.intro_text) list.before(intro);
  108. $.each(data.results, function(i,item){
  109. // auto join text based on verb tense and content
  110. if (s.join_text == "auto") {
  111. if (item.text.match(/^(@([A-Za-z0-9-_]+)) .*/i)) {
  112. var join_text = s.auto_join_text_reply;
  113. } else if (item.text.match(/(^\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+) .*/i)) {
  114. var join_text = s.auto_join_text_url;
  115. } else if (item.text.match(/^((\w+ed)|just) .*/im)) {
  116. var join_text = s.auto_join_text_ed;
  117. } else if (item.text.match(/^(\w*ing) .*/i)) {
  118. var join_text = s.auto_join_text_ing;
  119. } else {
  120. var join_text = s.auto_join_text_default;
  121. }
  122. } else {
  123. var join_text = s.join_text;
  124. };
  125.  
  126. var join_template = '<span class="tweet_join"> '+join_text+' </span>';
  127. var join = ((s.join_text) ? join_template : ' ')
  128. var avatar_template = '<a class="tweet_avatar" href="http://twitter.com/'+ item.from_user+'"><img src="'+item.profile_image_url+'" height="'+s.avatar_size+'" width="'+s.avatar_size+'" alt="'+item.from_user+'\'s avatar" border="0"/></a>';
  129. var avatar = (s.avatar_size ? avatar_template : '')
  130. var date = '<a id="postdate" href="http://twitter.com/'+item.from_user+'/statuses/'+item.id+'" title="view tweet on twitter">'+relative_time(item.created_at)+'</a>';
  131. var text = '<span class="tweet_text">' +$([item.text]).linkUrl().linkUser().linkHash().makeHeart().capAwesome().capEpic()[0]+ '</span>';
  132.  
  133. // until we create a template option, arrange the items below to alter a tweet's display.
  134. list.append('<div class="item"><div class="repeat">' + text + '</div><div class="entry_info"><a href="http://www.twitter.com/'+ item.from_user +'">'+ item.from_user +'</a> <span>| '+ relative_time(item.created_at) +'</span></div></div>');
  135.  
  136. list.children('li:first').addClass('tweet_first');
  137. list.children('li:odd').addClass('tweet_even');
  138. list.children('li:even').addClass('tweet_odd');
  139. });
  140. if (s.outro_text) list.after(outro);
  141. });
  142.  
  143. });
  144. };
  145. })(jQuery);