Created
August 15, 2010 15:44
-
-
Save keeguon/525624 to your computer and use it in GitHub Desktop.
Basic Twitter object, demo at the bottom (require jQuery here but could also write your own Ajax function). This should work w/ all Twitter APIs which do not require authentification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Twitter = { | |
version: '1.0', | |
apiUrl: 'http://api.twitter.com', | |
apiVersion: '1', | |
api: function(endpoint, options, callback) { | |
var fullUrl = this.apiUrl + '/' + this.apiVersion + '/' + endpoint + '.json'; | |
$.ajax({ | |
async: true, | |
url: fullUrl, | |
data: options.data, | |
dataType: 'jsonp', | |
success: function(response, status, xhr) { | |
callback(response); | |
}, | |
error: function(xhr, status, error) { | |
console.log(error); | |
} | |
}); | |
} | |
}; | |
var TwitterHelper = { | |
htmlize: function(tweet) { | |
// replace url w/ links | |
tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '<a href="$&" target="_blank">$&</a>'); | |
// replace hashtags w/ links | |
tweet = tweet.replace(/#(\w+)/g, '<a href="http://search.twitter.com/search?q=%23$1" target="_blank">#$1</a>'); | |
// replace @username w/ links | |
tweet = tweet.replace(/@(\w+)/g, '<a href="http://twitter.com/$1" target="_blank">@$1</a>'); | |
// return HTMLized tweet | |
return tweet; | |
}, | |
timediff: function(timestamp, created_at) { | |
// Time constants | |
var second = 1000; | |
var minute = second * 60; | |
var hour = minute * 60; | |
var day = hour * 24; | |
var week = day * 7; | |
// Timediff | |
timediff = timestamp - created_at; | |
// Create timestring | |
if (timediff < second * 7) | |
return 'right now'; | |
if (timediff < minute) | |
return Math.floor(timediff / second) + ' seconds ago'; | |
if (timediff < minute * 2) | |
return 'about a minute ago'; | |
if (timediff < hour) | |
return Math.floor(timediff / minute) + ' minutes ago'; | |
if (timediff < hour * 2) | |
return 'about an hour ago'; | |
if (timediff < day) | |
return Math.floor(timediff / hour) + ' hours ago'; | |
if (timediff > day && timediff < day * 2) | |
return 'yesterday'; | |
if (timediff < week * 52) | |
return Math.floor(timediff / day) + ' days ago'; | |
else | |
return 'over a year ago'; | |
} | |
}; | |
// Demo usage | |
Twitter.api('statuses/user_timeline', { data: 'screen_name=keeguon&count=50&include_rts=true' }, function(response) { | |
// variables | |
var limit = 10; | |
var timeline = ''; | |
var now = new Date(); | |
// tweet loop | |
for(var i = 0; i < response.length; i++) { | |
if (!limit) break; | |
if (response[i].in_reply_to_screen_name || response[i].in_reply_to_status_id || response[i].in_reply_to_user_id) { continue; } | |
else { | |
var tweet = ''; | |
var created_at = new Date(response[i].created_at); | |
tweet += '<div class="tweet">'; | |
tweet += '<p class="text">'+TwitterHelper.htmlize(response[i].text)+'</p>'; | |
tweet += '<p class="meta">'+TwitterHelper.timediff(now, created_at)+'</p>'; | |
tweet += '</div>'; | |
tweet += '<hr />'; | |
timeline+=tweet; | |
limit--; | |
} | |
} | |
// changing the html | |
$('div.timeline').html(timeline); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment