Created
February 7, 2013 14:05
-
-
Save federomero/4731105 to your computer and use it in GitHub Desktop.
Get a facebook urls and likes for the pages based on a list of urls
This file contains 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 'json' | |
require 'open-uri' | |
access_token = "…" | |
begin | |
urls_file = File.open('./urls.txt', 'w') | |
likes_file = File.open('./likes.txt', 'w') | |
STDIN.each_line do |l| | |
url = l.rstrip | |
query = url.split('.').first | |
query_url = "https://graph.facebook.com/search?fields=id,name,website,link,likes&q=#{query}&type=page&access_token=#{access_token}" | |
begin | |
result = open(query_url).read | |
list = JSON.parse(result)['data'] | |
page = list.select{|page| page['website'] =~ /#{url}\/?$/}.sort_by{|page| page['likes'].to_i}.last | |
if page | |
urls_file.puts "=hyperlink(\"#{page['link']}\";\"#{page['name']}\")" | |
likes_file.puts page['likes'] | |
else | |
urls_file.puts '' | |
likes_file.puts '' | |
end | |
sleep 0.2 | |
rescue Exception => e | |
STDERR.puts e.message | |
STDERR.puts e.backtrace | |
urls_file.puts '' | |
likes_file.puts '' | |
end | |
end | |
ensure | |
urls_file.close | |
likes_file.close | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment