Skip to content

Instantly share code, notes, and snippets.

@fael
Created September 12, 2011 21:04
Show Gist options
  • Save fael/1212422 to your computer and use it in GitHub Desktop.
Save fael/1212422 to your computer and use it in GitHub Desktop.
Get and format tweets
getTweets = function(alvo, usuario, qtdMensagens){
var o = {}
o.qtdMensagens = qtdMensagens;
o.caixaTwitter = $("#" + alvo);
o.caixaTwitter.append("<li><a href='#'>carregando tweets...</a></li>");
$.jTwitter(usuario, o.qtdMensagens, function(data){
o.caixaTwitter.empty();
o.list = '';
$.each(data, function(i, post){
o.hora = H(post.created_at);
o.texto = formatTwitString(post.text);
o.list += '<li>' + o.texto + "<span class='hora'>" + o.hora + '</span></li>';
});
o.caixaTwitter.append(o.list);
});
}
formatTwitString = function(str){
str=' '+str;
str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
return str;
}
// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
var a = navigator.userAgent;
return {
ie: a.match(/MSIE\s([^;]*)/)
}
}();
var H = function (a) {
var b = new Date();
var c = new Date(a);
if (K.ie) {
c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
}
var d = b - c;
var e = 1000,
minute = e * 60,
hour = minute * 60,
day = hour * 24,
week = day * 7;
if (isNaN(d) || d < 0) {
return ""
}
if (d < e * 7) {
return "agora pouco"
}
if (d < minute) {
return Math.floor(d / e) + " segundos atr&aacute;s"
}
if (d < minute * 2) {
return "h&aacute; 1 minuto"
}
if (d < hour) {
return "h&aacute; " + Math.floor(d / minute) + " minutos"
}
if (d < hour * 2) {
return "h&aacute; 1 hora"
}
if (d < day) {
return "h&aacute; " + Math.floor(d / hour) + " horas atr&aacute;s"
}
if (d > day && d < day * 2) {
return "ontem"
}
if (d < day * 365) {
return "h&aacute; " + Math.floor(d / day) + " dias"
} else {
return "h&aacute; mais de 1 ano"
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment