2 * Twitter Search Plugin jquery.twitter.js
3 * http://code.bocoup.com/jquery-twitter-plugin/
5 * Copyright (c) 2010 Bocoup, LLC
6 * Authors: Boaz Sender, Rick Waldron, Nick Cammarata
7 * Dual licensed under the MIT and GPL licenses.
8 * http://code.bocoup.com/license/
11 var linkify = linkify || function() {};
12 ;(function($, linkify) {
15 mention = function( str ) {
16 return str.replace("/[@]+[A-Za-z0-9-_]+/ig", function( username ) {
17 return username.link("http://twitter.com/"+ username.replace("@","") );
20 hashtags = function( str ) {
21 return str.replace("/[#]+[A-Za-z0-9-_]+/ig", function( tag ) {
22 return tag.link("http://search.twitter.com/search?q="+tag.replace("#","%23"));
26 $.twitter = function (options, callback) {
27 // Fail if the options arg is not set
32 // Set a temporary default query object
34 // Set up a string to be used later in the case that exclusions have been set
36 // Set up a regex to be used later in the case that exclusions have been set
37 exclusionsExp = new RegExp(false);
39 // If options is a string use it as standalone query
40 if ( typeof options === "string" ) {
41 query = $.extend({}, $.twitter.opts, {
44 // Else prepare the options object to be serialized
46 // If a limit is set, add it to the query object
47 options.rpp = options.limit ? options.limit : options.rpp;
49 // If no limit is set, make the limit the rpp
50 options.limit = options.limit ? options.limit : options.rpp;
52 // If there are exlusions, turn them into a regex string
53 exclusionsStr = options.exclusions ? options.exclusions.replace(" ", "|") : false;
55 // If there are exlusions, turn the regex string we just made into a RegExp
56 exclusionsExp = exclusionsStr ? new RegExp( exclusionsStr ) : false;
58 // Make a new object that is a merger of the options passed in with the default $.twitter.opts object
59 // and assign it to the query variable
60 query = $.extend({}, $.twitter.opts, options);
62 // If there are exclusions, or replies or retweets are set to false, multiply the results to ask for from twitter by ten
63 // We need to do this so that we have some meat to work with if the exclusions are common
64 query.rpp = query.exclusions || !query.replies || !query.retweets ? (query.rpp * 10) : query.rpp;
70 $.getJSON("http://search.twitter.com/search.json?callback=?", query, function(tweets){
71 callback(tweets, query, exclusionsExp);
75 $.fn.twitter = function( options ) {
76 // Fail gracefully if the options arg is not set
77 // return the jQuery obj so that chaining does not break
82 // Begin to iterate over the jQuery collection that the method was called on
83 return this.each(function () {
87 $.twitter(options, function( tweets, query, exclusionsExp ) {
88 //Create and cache a new UL
89 var $tweets = $("<ul>"),
90 // Create a counter variable to count up how many tweets we have rendered
91 // unfortunately we have to do this, because exclusions, retweet booleans and replies booleans
92 // are not supported by the Twitter Search API
96 // If there are results to work with
97 if ( tweets.results && tweets.results.length ) {
99 // Iterate over returned tweets
100 for ( i in tweets.results ) {
102 // Cache tweet content
103 var tweet = tweets.results[i],
104 // Set a variable to determine weather replies are set to false, and if so, weather the tweet starts with a reply
105 allowReply = !query.replies && tweet.to_user_id ? false : true,
106 // Set a variable to determine weather retweets are set to false, and if so, weather the tweet starts with a retweet
107 allowRetweet = !query.retweets && tweet.text.slice(0,2) === "RT" ? false : true;
109 // Only proceed if allow reply is false
114 // Only proceed if allow retweet is false
115 if ( !allowRetweet ) {
119 // If exlusions set and none of the exlusions is found in the tweet then add it to the DOM
120 if ( exclusionsExp && exclusionsExp.test(tweet.text) ) {
124 // Create and cache new LI
125 var $tweet = $("<li/>", {
129 // Make the avatar, and append it to the $tweet
130 if ( query.avatar === true ) {
131 $tweet.append($("<a/>", {
132 href: "http://twitter.com/" + tweet.from_user,
133 html: "<img src='" + tweet.profile_image_url + "'/>"
137 // Make the tweet text, and append it to the $tweet, then to the parent
138 $tweet.append($("<span>", {
140 html: "<a href='http://twitter.com/" + tweet.from_user + "'>@" + tweet.from_user + "</a>: " + mention(hashtags(linkify(tweet.text)))
142 // Append tweet to the $tweets ul
145 // Count up our counter variable
148 // If the counter is equal to the limit, stop rendering tweets
149 if ( limitInt === query.limit ) {
154 // Inject the $tweets into the DOM
157 // Else there are no results to work with
159 // Update the DOM to reflect that no results were found
160 $this.html($("<h3/>", {
161 "class": "twitter-notFound",
162 text: query.notFoundText
170 // Number of tweets to get
171 // not in twitter search api, maps to and supersedes rpp (results per page)
173 // Space delimited list of strings to exclude (eg: "_ s gr @b")
174 // not in twitter search api, done in plugin
176 // Text to display if no results are found
177 // not in twitter search api, done in plugin
178 notFoundText: "No results found on twitter",
180 // not in twitter search api, done in plugin
183 // not in twitter search api, done in plugin
185 // All of these words
189 // Any of these words
191 // None of these words
195 // Written in language
201 // Referencing this person
205 // Within this distance
207 // Distance unit (miles or kilometers)
213 // Attitude: "?" or :)" or ":)"
215 // Containing: "links"
217 // Include retweet?: "retweets"
223 // Add an avatar image of the user