Skip to content

Instantly share code, notes, and snippets.

@rudak
Created October 18, 2012 13:56
Show Gist options
  • Save rudak/3911979 to your computer and use it in GitHub Desktop.
Save rudak/3911979 to your computer and use it in GitHub Desktop.
// si tu as une div #twitage et que tu mets ton nom twitter
// la ou il faut et que tu as chargée la lib jQuery qui va
// bien ca marche tout seul...
// [a améliorer]
var moulinette = {
sortie : '',
nettoyage: function(chaine) {
this.sortie = this.parse_liens(chaine);
this.sortie = this.parse_listes(this.sortie);
this.sortie = this.parse_listes(this.sortie);
this.sortie = this.parse_users(this.sortie)
this.sortie = this.parse_hashtags(this.sortie);
return this.sortie;
},
parse_liens: function(chaine) {
return chaine.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(rien, m1, m2, m3, m4) {
var http = m2.match(/w/) ? 'http://' : '';
var texte_lien = (m1.length > 25) ? m1.substr(0, 24) + '...' : m1;
return '<a class="twitter-link" target="_blank" href="' + http + m1 + '">' + texte_lien + '</a>' + m4;
});
},
parse_users: function(chaine) {
return chaine.replace(/\B[@@]([a-zA-Z0-9_]{1,20})/g, function(rien, nomdugars) {
return '<a target="_blank" class="twitter-user" href="http://twitter.com/intent/user?screen_name=' + nomdugars + '">@' + nomdugars + '</a>';
});
},
parse_listes: function(chaine) {
return chaine.replace(/\B[@@]([a-zA-Z0-9_]{1,20}\/\w+)/g, function(rien, userlist) {
return '<a target="_blank" class="twitter-list" href="http://twitter.com/' + userlist + '">@' + userlist + '</a>';
});
},
parse_hashtags: function(chaine) {
return chaine.replace(/(^|\s+)#(\w+)/gi, function(rien, avant, hashtag) {
console.log('avant : ' + avant);
return avant + '<a target="_blank" class="twitter-hashtag" href="http://twitter.com/search?q=%23' + hashtag + '">#' + hashtag + '</a>';
});
}
}
function dateDuTweet(dateString) {
var toutdesuite = new Date();
var datedutweet = new Date(dateString);
if ($.browser.msie) {
// IE can't parse these crazy Ruby dates
datedutweet = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
}
var diff = toutdesuite - datedutweet;
var seconde = 1000,
minute = seconde * 60,
heure = minute * 60,
jour = heure * 24;
if (isNaN(diff) || diff < 0)
return "Je sais pas trop";
if (diff < seconde * 2)
return "juste a l'instant";
if (diff < minute)
return "Il y a " +Math.floor(diff / seconde) + " secondes";
if (diff < minute * 2)
return "Il y a environ 1 minute";
if (diff < heure)
return "Il y a environ "+Math.floor(diff / minute) + " minutes";
if (diff < heure * 2)
return "Il y a environ 1 heure";
if (diff < jour)
return "Il y a environ "+Math.floor(diff / heure) + " heure" + ((Math.floor(diff / heure) > 1)?'s':'');
if (diff > jour && diff < jour * 2)
return "Hier";
if (diff < jour * 365)
return "Il y a "+Math.floor(diff / jour) + " jours";
else
return "Il y a plus d'un an";
}
$.ajax({
url: 'http://api.twitter.com/1/statuses/user_timeline.json/',
type: 'GET',
dataType: 'jsonp',
data: {
screen_name: 'TON_NOM_TWITTER_ICI', // ! ! ! ! ! ! <== a remplir et hop ca marche
include_rts: true,
count: 5,
include_entities: true
},
success: function(data) {
var i,texte,date,sortie;
for(i in data){
texte = data[i].text;
date = data[i].created_at;
sortie = '<p class="texte">'+moulinette.nettoyage(texte)+'</p>';
sortie += '<p class="date">'+dateDuTweet(date)+'</p>';
$('#twitage').append(sortie); // #twitage c'est l'id de ma div de destination
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment