Last active
December 10, 2015 12:48
-
-
Save bamorim/4436234 to your computer and use it in GitHub Desktop.
This file contains 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
//To get your token, go to https://developers.facebook.com/tools/explorer/ | |
//Remember to include jQuery before. | |
token = ""; //Set your token here | |
page = 0; | |
responses = []; | |
me = ""; | |
messages = []; | |
friends = {}; | |
getUrl = function(token,page){ | |
query = "SELECT thread_id, message_count, recipients FROM thread WHERE folder_id = 0 LIMIT 50 OFFSET " + (page-1)*50; | |
return "https://api.facebook.com/restserver.php?format=json&method=fql.query&access_token=" + token + "&query=" + encodeURI(query) | |
}; | |
sortMessages = function(){ | |
responses.forEach(function(thread) { | |
if(thread.recipients.length > 2) return; | |
other = ""; | |
thread.recipients.forEach(function(p){ | |
if(p != me) | |
other = p; | |
}); | |
messages.push({friend_id: other, count: thread.message_count}); | |
}); | |
messages.sort(function(a,b){return b.count - a.count}); | |
} | |
fetchMe = function() { | |
$.getJSON("https://graph.facebook.com/me?access_token="+token,function(data){ | |
me = data.id; | |
fetchFriends(); | |
}); | |
} | |
fetchFriends = function() { | |
$.getJSON("https://graph.facebook.com/me/friends?access_token="+token,function(data){ | |
data.data.forEach(function(friend){ | |
friends[friend.id] = friend.name; | |
}); | |
fetchMessages(null); | |
}); | |
} | |
printTop = function(max) { | |
for(i=0;i<max;i++){ | |
name = friends[messages[i].friend_id]; | |
if(!name) | |
name = "" + messages[i].friend_id; | |
count = messages[i].count; | |
console.log("" + (i+1) + " - " + name + ": " + count); | |
} | |
}; | |
fetchMessages = function(response) { | |
if(response) | |
response.forEach(function(elm){ | |
responses.push(elm); | |
}); | |
if( !response || response.length == 50) { | |
page = page + 1; | |
console.log("fetching page "+page); | |
$.ajax({ | |
url: getUrl(token,page), | |
datatype: "jsonp", | |
success: fetchMessages | |
}); | |
} else { | |
console.log("finished fetching"); | |
sortMessages(); | |
printTop(100); | |
} | |
}; | |
fetchMe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment