Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
facebook election scraper
require 'koala'
AUTH_TOKEN = ENV["AUTH_TOKEN"]
START_TIME = Date.new(2012,11,5).to_time.to_i
END_TIME = Date.new(2012,11,8).to_time.to_i
@facebook = Koala::Facebook::API.new(AUTH_TOKEN)
friends = @facebook.get_connections("me", "friends").sort_by {|friend| friend["name"]}
friends = friends[161..(friends.count - 1)]
friends.each do |friend|
posts = @facebook.fql_query(
"SELECT post_id FROM stream
WHERE source_id = #{friend['id']}
AND created_time > #{START_TIME}
AND created_time < #{END_TIME}"
)
posts.each do |post|
sleep 1
real_post = @facebook.get_object(post['post_id'])
next if real_post['message'].nil? || real_post['message'] == ""
next unless ['status', 'link'].include? real_post['type']
puts "#{friend["name"]}: #{real_post["message"]}"
comment_hash = @facebook.get_object(post['post_id'])['comments']
comment_hash["data"].each do |comment|
puts "... #{comment["from"]["name"]}: #{comment["message"]}"
end if comment_hash['count'].to_i > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment