Skip to content

Instantly share code, notes, and snippets.

@samchandra
Created March 8, 2012 23:40
Show Gist options
  • Save samchandra/2004171 to your computer and use it in GitHub Desktop.
Save samchandra/2004171 to your computer and use it in GitHub Desktop.
Twitter Timeline and Facebook Pages Feed Aggregator with Ruby
require 'rubygems'
require 'bundler/setup'
require 'time'
require 'date'
require 'twitter'
require 'fb_graph'
require 'pp'
require 'json'
# Twitter
t_results = Twitter.user_timeline("your_user").map do |tweet|
id_str = tweet.id.to_s
utime = tweet.created_at.to_i
text = tweet.text
url = "http://#{text.split('http://').last}"
t_url = "http://mobile.twitter.com/your_user/status/#{id_str}"
{:id => id_str,
:text => text,
:picture => url,
:s_url => t_url,
:utime => utime
}
end
# Facebook
app_id = "your_app_id"
app_secret = "your_app_secret"
page_id = "your_app_page_id"
my_app = FbGraph::Application.new(app_id)
acc_tok = my_app.get_access_token(app_secret)
page = FbGraph::Page.fetch(page_id, :access_token => acc_tok)
# puts page.feed.count
f_result = page.feed.map do |item|
{:id => item.identifier,
:text => item.message,
:picture => item.picture,
:s_url => item.link,
:utime => item.created_time.to_i
}
end
# combine result and place latest on top
total_result = t_results.concat(f_result).sort_by{|e|e[:utime]}.reverse
File.open("combined_feed.json", "w") do |f|
f.puts ({:feeds => total_result, :count => total_result.count}.to_json)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment