Skip to content

Instantly share code, notes, and snippets.

@boazsender
Created August 14, 2010 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save boazsender/524863 to your computer and use it in GitHub Desktop.
Save boazsender/524863 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Twitter Search Plugin</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="jquery.twitter.js"></script>
<style type="text/css">
.twitter-posts li {margin-bottom: 10px; font-size: 12px; clear: both; list-style-type:none;}
.twitter-posts li img {float:left; width: 48px; margin:0px 10px 10px 0px;border:1px solid #c2c2c2; -moz-box-shadow: 0px 0px 4px #c2c2c2; -webkit-box-shadow: 0px 0px 4px #c2c2c2; box-shadow: 0px 0px 4px #c2c2c2;}
.twitter-posts li a {text-decoration:none; color: #009;}
hr.space {clear:both; width: 100%; visibility:hidden;}
</style>
<script type="text/javascript">
$(function(){
// Call twitter search for tweets including the string 'bocoup loft'
$('#bocoup').twitter('bocoup loft');
// Call twitter search for tweets including the string 'omgomg'
// and limit to 5 results
// and don't show tweets with the word 'lol'
// and don't show tweets with an '_' or an 's' in them
// and don't show @replies
// and don't show retweets
// and only show results with links
$('#omg').twitter({
limit : 5, // Number of tweets to get <-- not in twitter api, maps to and supersedes rpp (results per page)
exclusions : '_ s', // Space delimited list of strings to exclude (eg: '_ s gr @b') <-- not in twitter api, done in plugin
replies : false, // Include mentions <-- not in twitter api, done in plugin
retweets : false, // Include mentions <-- not in twitter api, done in plugin
ors : 'omg', // Any of these words
nots : 'lol' // None of these words
});
// Call twitter search for tweets from boazsender
// and limit 5 results
// and set special text to display if none are found
// and don't show @replies
// and don't show retweets
// and list out all the possible options properties for learning!
$('#boazsender').twitter({
limit : 5, // Number of tweets to get <-- not in twitter api, maps to and supersedes rpp (results per page)
exclusions : '@ #', // Space delimited list of strings to exclude (eg: '_ s gr @b') <-- not in twitter api, done in plugin
notFoundText : 'No results found on twitter', // Text to display if no results are found <-- not in twitter api, done in plugin
replies : true, // Include replies? <-- not in twitter api, done in plugin
retweets : true, // Include replies? <-- not in twitter api, done in plugin
ands : '', // All of these words
phrase : '', // This exact phrase
ors : '', // Any of these words
nots : '', // None of these words
tag : '', // This hashtag
lang : '', // Written in language
from : 'boazsender', // From this person
to : '', // To this person
ref : '', // Referencing this person
near : '', // Near this place
within : '', // Within this distance
units : '', // Distance unit (miles or kilometers)
since : '', // Since this date
until : '', // Until this date
tude : '', // Attitude: '?' or ':)' or ':)'
filter : '', // Containing: 'links'
include : '', // Include retweet?: 'retweets'
rpp : 5, // Results per page
q : '' // Plain query
});
// Call twitter search for tweets from rwaldron
// and limit 5 results
// and don't show @replies
// and don't show retweets
$('#rwaldron').twitter({
from : 'rwaldron', // From this person
limit : 5, // Number of tweets to get <-- not in twitter api, maps to and supersedes rpp (results per page)
replies : false, // Include replies? <-- not in twitter api, done in plugin
retweets : false // Include replies? <-- not in twitter api, done in plugin
});
// Call twitter search for tweets from asdasdhdajkhjfdasasd
// and limit 5 results
$('#asdasdhdajkhjfdasasd').twitter({
from : 'asdasdhdajkhjfdasasd', // From this person
limit : 0, // Number of tweets to get <-- not in twitter api, maps to and supersedes rpp (results per page)
notFoundText : 'this is special text to sho if no results are found.', // Text to display if no results are found <-- not in twitter api, done in plugin
});
});
</script>
<link href="twitterSearch.jQuery.css" media="screen" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Twitter:</h1>
<h2>'bocoup loft':</h2>
<div id="bocoup" class="twitter-posts">
<h3>Loading Tweets From The Cloud With AJAX HTML5...</h3>
</div>
<hr class="space" />
<h2>'omg':</h2>
<div id="omg" class="twitter-posts">
<h3>Loading Tweets From The Cloud With AJAX HTML5...</h3>
</div>
<hr class="space" />
<h2>@BoazSender</h2>
<div id="boazsender" class="twitter-posts">
<h3>Loading Tweets From The Cloud With AJAX HTML5...</h3>
</div>
<hr class="space" />
<h2>@rwaldron</h2>
<div id="rwaldron" class="twitter-posts">
<h3>Loading Tweets From The Cloud With AJAX HTML5...</h3>
</div>
<hr class="space" />
<h2>@asdasdhdajkhjfdasasd</h2>
<div id="asdasdhdajkhjfdasasd" class="twitter-posts">
<h3>Loading Tweets From The Cloud With AJAX HTML5...</h3>
</div>
</body>
</html>
/*
* Twitter Search Plugin jquery.twitter.js
* http://code.bocoup.com/jquery-twitter-plugin/
*
* Copyright (c) 2010 Bocoup, LLC
* Authors: Boaz Sender, Rick Waldron, Nick Cammarata
* Dual licensed under the MIT and GPL licenses.
* http://code.bocoup.com/license/
*
*/
;(function ($) {
$.getScript('http://github.com/cowboy/javascript-linkify/raw/master/ba-linkify.min.js');
var mention = function(str) {
return str.replace(/[@]+[A-Za-z0-9-_]+/ig, function(username) {
return username.link('http://twitter.com/'+ username.replace('@','') );
});
},
hashtags = function(str) {
return str.replace(/[#]+[A-Za-z0-9-_]+/ig, function(tag) {
return tag.link('http://search.twitter.com/search?q='+tag.replace('#','%23'));
});
};
$.twitter = function (options, callback) {
// Fail if the options arg is not set
if ( !options ) {
return false;
}
// Set a temporary default query object
var query,
// Set up a regex to be used later in the case that exclusions have been set
exclusionsExp = new RegExp(false);
// If options is a string use it as standalone query
if(typeof options == 'string'){
query = $.extend({}, $.twitter.opts, {
q: options
});
// Else prepare the options object to be serialized
} else {
// If a limit is set, add it to the query object
options.rpp = options.limit ? options.limit : options.rpp;
// If no limit is set, make the limit the rpp
options.limit = options.limit ? options.limit : options.rpp;
// If there are exlusions, turn them into a regex string
exclusionsStr = options.exclusions ? options.exclusions.replace(' ', '|') : false;
// If there are exlusions, turn the regex string we just made into a RegExp
exclusionsExp = exclusionsStr ? new RegExp(exclusionsStr) : false;
// Make a new object that is a merger of the options passed in with the default $.twitter.opts object
// and assign it to the query variable
query = $.extend({}, $.twitter.opts, options);
// If there are exclusions, or replies or retweets are set to false, multiply the results to ask for from twitter by ten
// We need to do this so that we have some meat to work with if the exclusions are common
query.rpp = query.exclusions || !query.replies || !query.retweets ? (query.rpp * 10) : query.rpp;
}
// Call Twitter JSONP
$.getJSON('http://search.twitter.com/search.json?'+$.param(query)+'&callback=?', function(tweets){
callback(tweets, query, exclusionsExp)
});
};
$.fn.twitter = function (options) {
// Fail gracefully if the options arg is not set
// return the jQuery obj so that chaining does not break
if ( !options ) {
return this;
}
// Begin to iterate over the jQuery collection that the method was called on
return this.each(function () {
// Cache `this`
var $this = $(this);
$.twitter(options, function(tweets, query, exclusionsExp){
//Create and cache a new UL
var $tweets = $('<ul>'),
// Create a counter variable to count up how many tweets we have rendered
// unfortunately we have to do this, because exclusions, retweet booleans and replies booleans
// are not supported by the Twitter Search API
limitInt = 0;
// If there are results to work with
if (tweets.results && tweets.results.length) {
// Iterate over returned tweets
for(var i in tweets.results){
// Cache tweet content
var tweet = tweets.results[i],
// Set a variable to determine weather replies are set to false, and if so, weather the tweet starts with a reply
allowReply = !query.replies && tweet.to_user_id ? false : true,
// Set a variable to determine weather retweets are set to false, and if so, weather the tweet starts with a retweet
allowRetweet = !query.retweets && tweet.text.slice(0,2) == 'RT' ? false : true;
// Only proceed if allow reply is false
if (!allowReply) {
continue;
}
// Only proceed if allow retweet is false
if (!allowRetweet) {
continue;
}
// If exlusions set and none of the exlusions is found in the tweet then add it to the DOM
if ( exclusionsExp && exclusionsExp.test(tweet.text) ) {
continue;
}
// Create and cache new LI
$('<li/>', {
className : 'tweet'
})
// Make the avatar, and append it to the $tweet
.append($('<a/>', {
href: 'http://twitter.com/' + tweet.from_user,
html: '<img src="' + tweet.profile_image_url + '"/>'
}))
// Make the tweet text, and append it to the $tweet, then to the parent
.append($('<span>', {
className: 'content',
html: '<a href="http://twitter.com/' + tweet.from_user + '">@' + tweet.from_user + '</a>: ' + mention(hashtags(linkify(tweet.text)))
}))
// Append tweet to the $tweets ul
.appendTo($tweets);
// Count up our counter variable
limitInt++;
// If the counter is equal to the limit, stop rendering tweets
if ( limitInt === query.limit ) {
break;
}
}
// Append the $tweets to the DOM
$this.html($tweets);
// Else there are no results to work with
} else {
// Update the DOM to reflect that no results were found
$this.html($('<h3/>', {
className: 'twitter-notFound',
text: query.notFoundText
}));
}
});
});
};
$.twitter.opts = {
limit : 7, // Number of tweets to get <-- not in twitter api, maps to and supersedes rpp (results per page)
exclusions : '', // Space delimited list of strings to exclude (eg: '_ s gr @b') <-- not in twitter api, done in plugin
notFoundText : 'No results found on twitter', // Text to display if no results are found <-- not in twitter api, done in plugin
replies : true, // Include replies? <-- not in twitter api, done in plugin
retweets : true, // Include replies? <-- not in twitter api, done in plugin
ands : '', // All of these words
phrase : '', // This exact phrase
ors : '', // Any of these words
nots : '', // None of these words
tag : '', // This hashtag
lang : '', // Written in language
from : '', // From this person
to : '', // To this person
ref : '', // Referencing this person
near : '', // Near this place
within : '', // Within this distance
units : '', // Distance unit (miles or kilometers)
since : '', // Since this date
until : '', // Until this date
tude : '', // Attitude: '?' or ':)' or ':)'
filter : '', // Containing: 'links'
include : '', // Include retweet?: 'retweets'
rpp : 5, // Results per page
q : '' // Default query
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment