FB Comment and Like Count
# This script is used to count how many times a user has liked or commented on your posts
# You will need to replace the variables "my_id" and "access_token" found below.
#
# Parameters: username year [month]
#
# Examples
# ruby is_friend.rb zuck 2012
# => Mark Zuckerberg has liked 0 posts and commented 1 times since January, 2013
#
# ruby is_friend.rb jimbru 2014 7
# => Jim Brusstar has liked 13 posts and commented 1 times since July, 2014
require 'json'
require 'open-uri'
my_id = 123456789 # replace with your id
access_token = 'Get from https://developers.facebook.com/tools/explorer'
user_alias, year, month = *ARGV
abort('Need user alias and year') unless(user_alias and year)
year, month = year.to_i, month.to_i
month = 1 if (month.zero?)
abort("#{year} is an invalid year") if (year < 2010 || year > DateTime.now.year)
abort("#{month} isn't a month") if (month > 12 || month < 1)
begin
user_data = JSON.parse(URI.parse('http://graph.facebook.com/' + user_alias).read)
user_id = user_data['id']
user_name = user_data['name']
rescue
abort("Couldn't get a valid user id for #{user_alias}") unless user_id
end
url = "https://graph.facebook.com/v2.1/#{my_id}/feed?access_token=#{access_token}"
like_count = comment_count = 0;
loop {
feed = JSON.parse(URI.parse(url).read)
feed['data'].each { |post|
post_date = Date.parse(post['created_time'])
break if (post_date.year < year) ||
(post_date.year == year && post_date.month < month)
post['likes']['data'].each { |like|
like_count += 1 if like['id'] == user_id
} unless post['likes'].nil?
post['comments']['data'].each { |comment|
next unless comment['from']
comment_count += 1 if comment['from']['id'] == user_id
} unless post['comments'].nil?
}
last_date = Date.parse(feed['data'].last['created_time'])
break if (last_date.year < year) ||
(last_date.year == year && last_date.month < month)
url = feed['paging']['next']
}
puts "#{user_name} has liked #{like_count} posts and commented #{comment_count} times since #{Date.new(year, month).strftime('%B, %Y')}"