Skip to content

Instantly share code, notes, and snippets.

@zthomae
Created May 1, 2012 01:23
Show Gist options
  • Select an option

  • Save zthomae/2564173 to your computer and use it in GitHub Desktop.

Select an option

Save zthomae/2564173 to your computer and use it in GitHub Desktop.
A stripped-down version of the Klouchebag script, putting scores in localStorage
function fetchTweets(user) {
$.ajax({
type : 'GET',
dataType : 'jsonp',
url : 'http://api.twitter.com/1/statuses/user_timeline.json',
data : { screen_name: user,
count: 200,
include_rts: 1,
trim_user: 0 },
success :
function(data){
showResults(user, data);
},
error :
function(xml,status) {
errorout();
}
});
}
function showResults(user, data) {
var r = calculateTotalScore(data);
/*switch(data[0].user.screen_name.toLowerCase()) {
case 'tomscott':
case 'piersmorgan':
r = 100;
break;
}*/
console.log(r);
localStorage.setItem(user + '_score', r);
}
function errorout() {
screen('error');
}
function restart() {
screen('input');
}
function calculateTotalScore(t) {
var msg = '';
var textspeak = 0;
var social = 0;
var retweets_total = 0;
var retweets_fake = 0;
var retweets_please = 0;
var profanity = 0;
for (var i in t) {
textspeak += textSpeakScore(t[i]);
social += socialScore(t[i]);
if (t[i].retweeted_status) { retweets_total++; }
if (isFakeRetweet(t[i])) { retweets_fake++; }
profanity += profanityScore(t[i]);
}
textspeak -= 40;
textspeak /= 250;
if (textspeak < 0) { textspeak = 0; }
if (textspeak > 1) { textspeak = 1; }
social /= 20;
if (social > 1) { social = 1; }
var retweets = 0;
if (retweets_total == 0 ) { retweets += 10; }
if (retweets_total > 10 ) { retweets += (retweets_total - 10); }
retweets += retweets_fake * 5;
retweets += retweets_please * 10;
retweets /= 30;
if (retweets > 1) { retweets = 1; }
profanity /= 20;
if (profanity > 1) { profanity = 1; }
var total = Math.round((textspeak+social+retweets+profanity)*25)
return total;
}
function textSpeakScore(tweet) {
// standard rules from Stupid Fight
var d = 0;
var msg = tweet.text;
d += rCount(msg,/(.)\1{3}/g);
d += rCount(msg,/(.)\1{7}/g);
d += rCount(msg,/(!!)/g);
d += rCount(msg,/(\?\?)/g);
if (!msg.match(/[\.,!\?;:]/)) { d += 10; }
d += rCount(msg.toLowerCase(),/\s[b-h|j-z]\s/g) * 5;
d += rCount(msg.toLowerCase(),/(lol|omg|wtf|ftw|\&lt\;3|luv|xo|yr|wit\W|\Wur\W|btw|rofl|lmao|stfu)/g) * 5;
return d;
}
function socialScore(tweet) {
// social checkins
var d = 0;
var source = tweet.source.toLowerCase();
var msg = tweet.text.toLowerCase();
d += rCount(source,/foursquare/g);
d += rCount(source,/jam/g);
d += rCount(source,/miso/g);
d += rCount(source,/agram/g);
d += rCount(source,/zeebox/g);
d += rCount(source,/klout/g);
d += rCount(source,/dlvr/g);
d += rCount(source,/paper\.li/g);
d += rCount(msg,/klout\.li/g);
d += rCount(msg,/klouchebag\.li/g);
return d;
}
function isFakeRetweet(tweet) {
msg = tweet.text.toLowerCase();
var d = rCount(msg,/rt\ @/g);
if (d > 0) {
if (tweet.retweeted_status) { return false; } else { return true; }
} else {
return false;
}
}
function pleaseRetweet(tweet) {
msg = tweet.text.toLowerCase();
d += rCount(source,/(pls|plz|please) (rt|retweet)/g);
d += rCount(source,/(rt|retweet) (pls|plz|please|thx|thanks|if)/g);
}
function profanityScore(tweet) {
var d = 0;
// broken up to avoid overaggressive web filtering
// words and weighting taken from Ofcom's incredible 2000 "delete expletives?" report: http://www.ofcom.org.uk/static/archive/itc/uploads/Delete_Expletives.pdf
// ps: mum, please don't read this
var epithets = [
'fu'+'kin', 'cu'+'nt', 'fu'+'ck', 'fuk', 'fu'+'kers', 'wan'+'k', 'motherf'+'ucker', 'mo'+'fo', // Hey hey, it's lousy obscene spelling time!
'bast'+'ard', 'tw'+'at', 'as'+'shole', 'ars'+'ehole',
'nig'+'ger','fa'+'ggot' // I'm only writing these in source code, and I still feel uncomfortable typing them
// ...is it somehow less offensive if I copy and paste them instead?
]
var msg = tweet.text.toLowerCase();
var regex;
for (var i in epithets) {
regex = RegExp(epithets[i],'g');
d += rCount(msg,regex);
}
return d;
}
function rCount(msg,reg) {
result = msg.match(reg);
if (result) {
return result.length;
} else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment