#!/usr/bin/env ruby require 'fb_scrape' require 'thor' require 'json' require 'csv' class FBScrape::CLI < Thor desc "version", "Get the version of the client running" def version puts FBScrape::VERSION end desc "id", "Get the page_id for page" option :page_name, :required => true option :token, :required => true def id begin client = FBScrape::Client.new(options[:page_name], options[:token]) puts client.id rescue ArgumentError => e puts e.message end end desc "posts", "Get all the posts for the page" option :page_id, :required => true option :token, :required => true option :limit, :required => false def posts begin limit = options[:limit] || nil client = FBScrape::Client.new(nil, options[:token], options[:page_id]) client.load(limit) posts = client.posts puts JSON.pretty_generate(posts) rescue ArgumentError => e puts "Error:" + e.message end end desc "comments", "Get all the comments for a post's shortcode" option :id, :required => true option :token, :required => true option :page_id, :required => false def comments begin token = options[:token] id = options[:id] post = FBScrape::Post.load_from_id(id, token) post.load_all_comments comments = post.comments puts JSON.pretty_generate(comments) rescue ArgumentError => e puts e.message end end desc "export_messages", "Get all the comments for a post's shortcode" option :limit, :required => true option :token, :required => true option :page_id, :required => true option :out_file, :required => true def export_messages begin # exports all a pages conversation threads to a csv file$T client = FBScrape::Client.new(nil, options[:token], options[:page_id]) client.load_conversations(options[:limit]) client.conversations.each do |c| c.load_messages puts "Loading #{c.id} messages: #{c.messages.count}" end messages = client.conversations.collect{ |c| c.messages }.flatten puts "Total messages #{messages.count}" CSV.open(options[:out_file], "w") do |w| w << ["message_id", "conversation_id", "from", "text", "is_reply", "created_at"] messages.each do |m| w << [m.id, m.conversation_id, m.from_name, m.text, m.is_reply?, m.created_at ] end end rescue ArgumentError => e puts e.message end end end FBScrape::CLI.start(ARGV)