Using Facebook's graph API and the koala Facebook gem for ruby we can write a self-contained friend accountant that can determine whether new friends have been added since the last run or whether friends have been removed. The script is written similar to a virus where we do not use an external database to store the data but rather store the data within the script itself after a defined marker. The advantage is that the script becomes fully contained and can remember an old friend list by itself.
This script requires the koala
gem that can be installed by issuing:
gem install koala
Secondly, since Facebook removed the ability to query your own friends list remotely, we will be creating a Facebook access token by creating a new canvas application of the game type. This can be performed by going to the explorer development corner and creating a new application by selecting the menu item My Apps→Add a New App
. After that, select a Canvas
application with the type set to Game
. Finally, follow the wizard till the end, adding an URL and filling in most of the fields that the wizard requires.
Lastly, we need to generate an application key for yourself by going to the explorer development corner and then selecting your newly created application from the drop-down box Graph API Explorer
, and then selecting Get User Access Token
from the Get Token
drop-down menu.
After you accept to spy on yourself, the utility will display a very large hash in the bar which you will need to add to the script below.
#!/usr/bin/ruby ########################################################################### ## Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 ## ## Please see: http://www.gnu.org/licenses/gpl.html for legal details, ## ## rights of fair usage, the disclaimer and warranty conditions. ## ########################################################################### ########################################################################### # CONFIGURATION # ########################################################################### ACCESS_TOKEN="CBANDsD5FkXweAwyvutwqnjbE4nfZBBkfXUCvNHLBZAURZCKqYuXu54ZB46fhtUupaou3lG0xUYIcik9DMPbSJpyeGbOFGNLjleoJy1kiNIrtwcKyjrj0xmZAQTMR5vZCKvRUjYL8Y9lirIVVdUOBe6QhFRNyAp4QWqZC18IlaUEDXmwczXYhcK1" ########################################################################### # INTERNALS # ########################################################################### require "koala" require "json" # Get the friends list through Koala. @graph = Koala::Facebook::API.new(ACCESS_TOKEN) friends = @graph.get_connections( "me", "invitable_friends", { limit: 5000 } ) # Read the friends stored in the script. store = Array.new mark = false File.open(File.expand_path(__FILE__), "r") do | s | s.each_line do | line | if line.start_with? "# FACEBOOK FRIENDS LIST" mark = true next end if mark store.push line.split('#').last.strip end end end store = store.sort # Get the new list of friends. fetch = Array.new friends.each do | f | fetch.push f["name"] end fetch = fetch.sort # Check for friends that are no longer friends. store.each do | f | if !fetch.include? f puts f + " " + "is no longer your friend." end end # Check for newly added friends. fetch.each do | f | if !store.include? f puts f + " " + "is a new friend." end end # Read the entire script up to the friends marker script = Array.new File.open(File.expand_path(__FILE__), "r") do | s | s.each_line do | line | if line.start_with? "# FACEBOOK FRIENDS LIST" break; end script.push line end end # Write the script back to the file File.open(File.expand_path(__FILE__), "w") do |s| script.each do | line | s.write line end # And append the new friends s.write "# FACEBOOK FRIENDS LIST" + "\n" fetch.each do |f | s.write "#" + " " + f + "\n" end end puts "You have a total of: " + fetch.length.to_s + " friends." ### Here we store the previous list of friends # FACEBOOK FRIENDS LIST