Skip to content

Instantly share code, notes, and snippets.

@JustinAiken
Created December 4, 2012 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinAiken/4209796 to your computer and use it in GitHub Desktop.
Save JustinAiken/4209796 to your computer and use it in GitHub Desktop.
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