Skip to content

Instantly share code, notes, and snippets.

@mmb
Created October 4, 2009 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmb/201697 to your computer and use it in GitHub Desktop.
Save mmb/201697 to your computer and use it in GitHub Desktop.
fetch a user's facebook news feed
#!/usr/bin/ruby
# fetch a user's facebook news feed
require 'rubygems'
require 'httparty'
require 'digest/md5'
require 'pp'
API_KEY = ''
# infinite session, must previously exist
SESSION_SECRET = ''
SESSION_KEY = ''
# user id whose stream to get
USER_ID = ''
class Stream
include HTTParty
base_uri 'http://api.facebook.com/restserver.php'
format :json
def self.gett(api_key, secret, session_key, user_id)
get('', :query => sign(secret,
'api_key' => api_key,
'format' => 'JSON',
'method' => 'stream.get',
'session_key' => session_key,
'viewer_id' => user_id
))
end
def self.sign(secret, q)
q.merge(:sig => sig(secret, q))
end
def self.sig(secret, q)
Digest::MD5.hexdigest(
q.sort.collect { |k,v| "#{k}=#{v}" }.join('') + secret)
end
end
stream = Stream.gett(API_KEY, SESSION_SECRET, SESSION_KEY, USER_ID)
users = Hash[*stream['profiles'].collect { |x| [x['id'], x['name']] }.flatten]
# pp stream['posts']
stream['posts'].each do |p|
puts "#{users[p['actor_id']]}: #{p['message']}"
p['comments']['comment_list'].each do |c|
puts " #{users[c['fromid']]}: #{c['text']}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment