Facebookの「いいね!」ボタンが押された回数を取得する方法
2011/09/15追記
本エントリの内容は若干古く、より簡単に取得出来る方法が実装されているようです。詳しくは下記を参照ください。
2011年9月版 Facebookの「いいね!」ボタンが押された回数を取得する方法 - でぶぬる日記
↓↓↓↓↓↓↓↓以下、少々古い内容です↓↓↓↓↓↓↓↓
Facebookの「いいね!」ボタン(Likeボタン)が押された回数を、プログラムで取得するにはどうすればよいか?
こんな話題に日本の誰が興味を持つのか!?という根本的な疑問はさておき。
http://developers.facebook.com/docs/api (要ログイン)
ここらへんを見ても、書いてあるような書いてないような感じだったので、試行錯誤してみました。
1. 強引な方法
require 'open-uri' require 'nokogiri' def facebook_like_count(url) open("http://www.facebook.com/plugins/like.php?layout=button_count&show_faces=true&width=120&action=like&colorscheme=light&href=#{url}", "User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6") do |html| Nokogiri::HTML.parse(html.read).search("div.connect_widget_button_count_count").text.to_i rescue -1 end end # p facebook_like_count("http://www.yahoo.co.jp/")
はい。見ての通りですね。
Likeボタンの実体が http://www.facebook.com/plugins/like.php に存在している(各サイト上にはiframeで埋め込む)ので、ボタンのHTMLをスクレイピングして、無理やり数値を取ってきています。
openの引数に"User-Agent"を付けているのは、まともなUAを付加しないとfacebook.comで弾かれてしまう為です。
うん。多少無理やりだけど、なんだかこれで満足!
3.2 Facebookユーザーのコンテンツまたは情報を収集することはできません。また、弊社の許可を得ることなく、自動化された手段(情報収集ボット、ロボット、スパイダー、スクレーパーなど)を使用して、Facebookにアクセスすることはできません。
このプログラムだと規約に抵触してしまいます。悲しいことである。
2. 正しい答え
以下に正しいやり方が説明されてありました。
(a) 「FQL(Facebook Query Language)」というSQL類似の問い合わせ言語を使って、以下のAPI呼び出しで情報を取得出来る。
(b) 外部リンクに対する「いいね!」の情報は"link_stat"というtableにアクセスすればよい。つまりFQLで記述すると、
select like_count, total_count, share_count, click_count
from link_stat
where url="#{調べたいURL}"
これに従い、Yahoo!JAPANの「いいね!」の数を問い合わせてみます。
<fql_query_response list="true"> <link_stat> <like_count>56</like_count> <total_count>605</total_count> <share_count>462</share_count> <click_count>5</click_count> </link_stat> </fql_query_response>
3. これってひょっとして、
「Facebookに登録されてるLikeの全情報をまとめて取得出来るんじゃね?」と淡い期待を抱く。
https://api.facebook.com/method/fql.query?query=select%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat
<error_response xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd"> <error_code>601</error_code> <error_msg>Parser error: unexpected end of query.</error_msg> <request_args list="true"> <arg> <key>method</key> <value>fql.query</value> </arg> <arg> <key>query</key> <value> select like_count, total_count, share_count, click_count from link_stat </value> </arg> </request_args> </error_response>
文法エラー?where句があればいいのか?
https://api.facebook.com/method/fql.query?query=select%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%201=1
<?xml version="1.0" encoding="UTF-8"?> <error_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd"> <error_code>604</error_code> <error_msg>Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from http://wiki.developers.facebook.com/index.php/FQL_Tables </error_msg> <request_args list="true"> <arg> <key>method</key> <value>fql.query</value> </arg> <arg> <key>query</key> <value>select like_count, total_count, share_count, click_count from link_stat where 1=1</value> </arg> </request_args> </error_response>
結論としては、1件ずつの取得しか許可されてないようです。残念。