Created
July 8, 2009 18:35
-
-
Save jubishop/143051 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://www.facebook.com/login.php?api_key=API_KEY_HERE&next=http://www.facebook.com/connect/login_success.html&return_session=true&req_perms=offline_access,publish_stream,read_stream,email,create_event,rsvp_event,sms,status_update,photo_upload,video_upload,create_note,share_item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'digest/md5' | |
require 'json' | |
require 'net/http' | |
class Facebook | |
public | |
# This is it. | |
def self.callMethod(methodName, args={}) | |
args ||= {} | |
args.each_pair {|key, value| args[key] = jsonify(value) } | |
args['v'] = '1.0' | |
args['format'] = 'JSON' | |
args['method'] = methodName | |
args['api_key'] = @@apiKey | |
args['session_key'] = @@sessionKey | |
args['call_id'] = (@@call_id += 1) | |
args['ss'] = true | |
hashString = args.keys.sort.map{|key| | |
"#{key}=#{args[key]}"}.push(@@sessionSecret).join | |
args['sig'] = Digest::MD5.hexdigest(hashString) | |
res = Net::HTTP.post_form( | |
URI.parse('http://api.facebook.com/restserver.php'), | |
args) | |
JSON.parse(res.body.to_s) | |
end | |
@@apiKey = '6441b756e04e4b556084f1ebae7957d2' | |
@@sessionKey = '2.Kq_eNpy1bknRvWDp26FNbw__.86400.1247101200-688626964' | |
@@sessionSecret = '2zEok7ee_84opK2_ihuFSw__' | |
@@call_id = Time.now.tv_sec | |
private | |
def self.jsonify(item) | |
if (item.class == Hash) | |
item.each_pair {|key, value| | |
item[key] = jsonify(value) | |
} | |
item.to_json | |
elsif (item.class == Array) | |
item.map{|entry| jsonify(entry)}.to_json | |
else | |
item | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'Facebook' | |
# Get the stream | |
# List of all posts known about | |
known_posts = Array.new | |
# Loop and repeat our fetching | |
loop { | |
# Fetch the stream and delete those already known | |
stream_data = Facebook::callMethod("fql.multiquery", { | |
'queries' => { | |
"stream" => "select post_id, actor_id, created_time, " + | |
"target_id, message, attachment, comments, likes " + | |
"from stream where filter_key = 'nf' order by created_time desc", | |
"friends" => "select uid, name, pic_square from user " + | |
"where uid in (select actor_id from #stream)" | |
}}) | |
# Map array into hash with name => key, fql_result_set => value | |
stream_hash = Hash.new | |
stream_data.each { |stream_result| | |
stream_hash[stream_result['name']] = stream_result['fql_result_set'] | |
} | |
# Map out friends into hash by uid | |
friends = Hash.new({"name" => "Unknown"}) | |
stream_hash['friends'].each { |friend| | |
friends[friend['uid']] = friend | |
} | |
# Remove those entries already shown | |
stream_hash['stream'].delete_if{ |stream_entry| | |
known_posts.include?(stream_entry['post_id']) | |
} | |
# Add these new entries to the list of known ids | |
known_posts = known_posts.concat(stream_hash['stream'].map{ |stream_entry| | |
stream_entry['post_id'] | |
}) | |
# Create profile pics dir, and fetch photos | |
Dir.mkdir("profile_pics") unless File.exists?("profile_pics") | |
friends.each_value { |friend| | |
next unless friend['pic_square'] | |
next if File.exists?("profile_pics/#{friend['uid']}.jpg") | |
`curl "#{friend['pic_square']}" > "profile_pics/#{friend['uid']}.jpg"` | |
} | |
# Growl entries | |
stream_hash['stream'].each { |stream_entry| | |
puts stream_entry['message'] | |
`growlnotify -m "#{stream_entry['message']}" -t "#{friends[stream_entry['actor_id']]['name']}" --image "profile_pics/#{stream_entry['actor_id']}.jpg"` | |
} | |
# Wait before doing it again | |
sleep 30 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment