Created
April 16, 2015 18:58
-
-
Save dannyvassallo/562ecd3d289ed28cbeae to your computer and use it in GitHub Desktop.
Facebook Sentiment Report RUBY
This file contains hidden or 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
# Generate Acces Token Here | |
# https://developers.facebook.com/tools/explorer/ | |
require 'sad_panda' | |
require 'httparty' | |
require 'json' | |
access_token = "*" | |
event_id = "[PUT THE EVENT IS HERE]" | |
the_url = "https://graph.facebook.com/#{event_id}/feed" | |
response = HTTParty.get(the_url, | |
:query => {:access_token => access_token}) | |
json = JSON.parse(response.body) | |
class String | |
def is_number? | |
true if Float(self) rescue false | |
end | |
end | |
postcount = 0 | |
json['data'].each_with_index do |this| | |
postcount = postcount + 1 | |
puts "====================" | |
puts "--------------------" | |
puts "** Post Number #{postcount} **" | |
puts "--------------------" | |
puts "User = " + this["from"]["name"] | |
puts "Post Message = " + this["message"].to_s | |
puts "Post Emotion = " + SadPanda.emotion(this["message"].to_s) | |
puts "Post Polarity = " + SadPanda.polarity(this["message"].to_s).to_s | |
puts "--------------------" | |
puts "** Comments on Post Number #{postcount} **" | |
puts "--------------------" | |
comments = this['comments'].to_a | |
comments.each_with_index do |that| | |
commentblock = that[1] | |
puts "......................" | |
if commentblock.is_a?(Array) | |
commentblock.each_with_index do |thing| | |
thing.map do |x| | |
singlets = x[1] | |
if singlets.is_a?(Hash) | |
puts "User = " + singlets["name"] | |
elsif singlets.is_a?(String) | |
d = Date.parse(singlets) rescue nil | |
if d | |
# do nothing | |
elsif singlets.is_number? | |
# do nothing | |
else | |
puts "Comment = " + singlets | |
puts "Comment Emotion = " + SadPanda.emotion(singlets) | |
puts "Comment Polarity = " + SadPanda.polarity(singlets).to_s | |
end | |
end | |
end | |
puts "......................" | |
end | |
end | |
end | |
puts "====================" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment