Skip to content

Instantly share code, notes, and snippets.

@phstc
Created December 29, 2010 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phstc/758591 to your computer and use it in GitHub Desktop.
Save phstc/758591 to your computer and use it in GitHub Desktop.
/*
@pablocantero
http://pablocantero.com
https://github.com/phstc/jquery-twitter/
*/
(function(){
/*
Customized callback to show an error message on the tweets's lookup
This callback is optional, the default error callback shows an alert(errorMessage)
*/
var tweetsErrorCallback = function(errorMessage, tweet){
var msg = 'Oops! <a href="http://dev.twitter.com/pages/rate-limiting">Twitter Rate Limit Exceeded</a>';
msg += 'Try again later or search <a href="http://twitter.com/search?q=' + tweet.toQuery() + '">directly on Twitter</a>';
$('#tweets').html(msg);
}
/*
Customized callback to show the tweets
Remember that, this plugin is only for tweets's search, it does not generate view
*/
var tweetsSuccessCallback = function(data){
var tweetsLi = '';
for(var i = 0; i < data.results.length; i++){
var tweet = data.results[i];
// Calculate how many hours ago was the tweet posted
// http://www.lupomontero.com/fetching-tweets-with-jquery-and-the-twitter-json-api/
var dateTweet = new Date(tweet.created_at);
var dateNow = new Date();
var dateDiff = dateNow - dateTweet;
var hours = Math.round(dateDiff/(1000*60*60));
tweetsLi += '<li>';
tweetsLi += ' <dl>';
tweetsLi += ' <dt><img src="' + tweet.profile_image_url + '" /></dt>';
tweetsLi += ' <dd>';
tweetsLi += ' <span><a href="http://twitter.com/' + tweet.from_user + '">' + tweet.from_user + '</a></span>';
tweetsLi += ' <span>' + hours + ' hours ago</span></dd></dl>';
tweetsLi += ' <div>' + tweet.text + '</div>';
tweetsLi += '</li>';
}
$('#tweets').html('<ul>' + tweetsLi + '</ul>');
$('#tweets').autolink({addBr:true});
}
$(document).ready(function(){
/*
Here is the example of the plugin usage
*/
var tweets = Twitter.tweets();
tweets.containing('jquery.twitter').all(tweetsSuccessCallback, tweetsErrorCallback);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment