Skip to content

Instantly share code, notes, and snippets.

@akasper
Created September 21, 2011 15:13
Show Gist options
  • Save akasper/1232315 to your computer and use it in GitHub Desktop.
Save akasper/1232315 to your computer and use it in GitHub Desktop.
With many page IDs
require 'rubygems'
require 'httparty'
require 'json'
require 'csv'
PAGE_IDS = ['vitrue', 'buddymedia' ] # ... and so on
ACCESS_TOKEN = 'AAACEdEose0cBAMRhF8cj7Gzt1SpNxPwW28dHkXPDLjqRVo2gfxB3lpDDZCdBZAyYv4xn1mTLtJn7c5s6MTBjEIYC73QR7fU1wHxjGShgZDZD'
def url_for_posts(page_id, token=ACCESS_TOKEN)
"https://graph.facebook.com/#{page_id}/posts?limit=500&access_token=#{token}"
end
def posts_for(page_id)
response = HTTParty.get(url_for_posts(page_id))
JSON.parse(response.body)['data']
end
def name
@post["from"]["name"] rescue ''
end
def created_time
@post["created_time"] rescue ''
end
def likes_count
@post["likes"]["count"] rescue ''
end
def comments_count
@post["comments"]["count"] rescue ''
end
def application_id
@post["application"]["id"] rescue ''
end
contents = CSV.generate('', {}) do |csv|
csv << ["name", "page id", "created_time", "likes count", "comments count", "application id"]
PAGE_IDS.each do |page_id|
posts_for(page_id).each do |post|
@post = post
puts row.inspect.to_s
csv << [name, page_id, created_time, likes_count, comments_count, application_id]
end
end
end
File.open("result_" + Time.now.to_i.to_s + ".csv", 'w') {|f| f.write(contents)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment