require 'open-uri' require 'json' # 指定したURLのFacebookいいね数を取得するクラス class FbShareCounter # FacebookグラフAPIのURL BASE_URL = 'http://graph.facebook.com/?id=' # target_urlにいいね数をカウントする対象URLを指定 def initialize(target_url) @request_url = BASE_URL + target_url end def count # グラフAPIにアクセス response = open(@request_url).read # テキストをハッシュに変換 hash = JSON.parse(response) # いいね数を取得 hash['share']['share_count'] end end counter = FbShareCounter.new('http://gihyo.jp') p counter.count
というソースコードで、コマンドプロンプトで実行した結果、
C:/samples/samples/09_03/like_counter.rb:16:in `initialize': Invalid argument @ rb_sysopen - http://graph.facebook.com/?id=http://gihyo.jp (Errno::EINVAL) from C:/samples/samples/09_03/like_counter.rb:16:in `open' from C:/samples/samples/09_03/like_counter.rb:16:in `count' from C:/samples/samples/09_03/like_counter.rb:27:in `<main>'
という結果が帰ってくるのですが、なぜでしょうか?
それで、解決したいことは、プログラムを修正して、いいね数を取得したいのです。
教えていただけると幸いです。よろしくお願いします。
質問にあるコードは、以下のページで配布されているサンプルファイルに含まれている samples/09_03/like_counter.rb だと思われます。
サポートページ:3ステップでしっかり学ぶ Ruby入門 技術評論社
https://gihyo.jp/book/2018/978-4-7741-9502-5/support
このページには補足情報あって、現在では like_counter.rb は動作しないとのことです。
==========
補足情報
(2019年6月11日更新)
FacebookのAPI変更により、9章3節のコードが動作しなくなっています。
代わりに、同じようにWeb APIを利用するサンプルコードをサンプルファイルとして掲載いたします。
参考としてください。
==========
迅速な回答ありがとうございます。明日、試してみます。又、質問時には、よろしくお願いします。
コメントありがとうございます。URL先のサポートページの補足情報の9章3節のコードを試しましたがまだ解決できていません。
コマンドプロンプトで試した結果なのですが、
C:/studyruby/like2.rb:6: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/studyruby/like2.rb:19:in `initialize': Invalid argument @ rb_sysopen - https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=ruby (Errno::EINVAL)
from C:/studyruby/like2.rb:19:in `open'
from C:/studyruby/like2.rb:19:in `search'
from C:/studyruby/like2.rb:30:in `<main>'
というエラーメッセージが出てきて、それで、
コマンドプロンプトの結果の一部の「 https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=ruby 」
の所で、そこに、カーソルを当てると、「Ctrlキーを押しながら、リンクを表示」というのが出てきて、
それを実行すると、マイクロソフトエッジというアプリで、出てきて(URLは、鍵括弧ので囲んだ通りです。「https://en.wikipedia.org/w/api.phpformat=json&action=query&list=search&srsearch=ruby」
(続く)
(続き)
で、以下のメッセージが出てきました。最初に日本語に翻訳しますか?と聞かれて、日本語に翻訳するを
クリックしましたが、日本語翻訳にはなりませんでした。
{"batchcomplete":"","continue":{"sroffset":10,"continue":"-||"},"query":{"searchinfo":{"totalhits":37356,"suggestion":"rugby","suggestionsnippet":"rugby"},"search":
[{"ns":0,"title":"Ruby","pageid":43551,"size":37725,"wordcount":3862,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum (aluminium oxide). \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is one of the most popular traditional jewelry","timestamp":"2025-03-13T12:31:27Z"},{"ns":0,"title":"Ruby character","pageid":43552,"size":17044,"wordcount":1606,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E characters or rubi characters (Japanese: ルビ; rōmaji: rubi; Korean: 루비; romaja: rubi) are small, annotative glosses that are usually placed above or","timestamp":"2025-01-17T22:35:06Z"},{"ns":0,"title":"Jack Ruby","pageid":16277,"size":95026,"wordcount":10111,"snippet":"Jack Leon \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E (born Jacob Leon Rubenstein; c. March 25, 1911 – January 3, 1967) was an American nightclub owner who murdered Lee Harvey Oswald on November","timestamp":"2025-03-19T15:21:57Z"},{"ns":0,"title":"Ruby (Jennie album)","pageid":78945812,"size":64045,"wordcount":4864,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is the debut solo studio album by South Korean singer and rapper Jennie. Released on March 7, 2025, through Odd Atelier and Columbia Records, the","timestamp":"2025-03-20T08:35:51Z"},{"ns":0,"title":"Ruby Franke","pageid":74732982,"size":28004,"wordcount":2321,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Franke (née Griffiths) is an American convicted child abuser and former family vlogger who ran the now defunct YouTube channel 8 Passengers. On August","timestamp":"2025-03-14T12:52:45Z"},{"ns":0,"title":"Ruby (programming language)","pageid":25768,"size":61259,"wordcount":5602,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is an interpreted, high-level, general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity.","timestamp":"2025-03-06T22:11:19Z"},{"ns":0,"title":"Ruby (disambiguation)","pageid":25607,"size":8474,"wordcount":1058,"snippet":"Look up \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E or \u003Cspan class=\"searchmatch\"\u003Eruby\u003C/span\u003E in Wiktionary, the free dictionary. A \u003Cspan class=\"searchmatch\"\u003Eruby\u003C/span\u003E is a red gemstone. \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E may also refer to: \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Alaska \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Arizona \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Copiah County","timestamp":"2025-01-22T07:07:23Z"},{"ns":0,"title":"Ruby Rose","pageid":19242083,"size":65946,"wordcount":5145,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Rose Langenheim (born 20 March 1986) is an Australian actress, television presenter, and model. She gained prominence for her role in season three","timestamp":"2025-03-12T04:18:34Z"},{"ns":0,"title":"Ruby Bridges","pageid":1143659,"size":28397,"wordcount":2960,"snippet":"\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Nell Bridges Hall (born September 8, 1954) is an American civil rights activist. She was the first African American child to attend formerly whites-only","timestamp":"2025-03-15T17:33:45Z"},{"ns":0,"title":"Ruby Ridge standoff","pageid":501385,"size":108783,"wordcount":12341,"snippet":"The \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Ridge standoff was the siege of a cabin occupied by the Weaver family in Boundary County, Idaho, in August 1992. On August 21, deputies of the","timestamp":"2025-03-01T06:39:21Z"}]}}
で、整形出力にチェックすると、
(続く)
(続き)
{
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
"query": {
"searchinfo": {
"totalhits": 37356,
"suggestion": "rugby",
"suggestionsnippet": "rugby"
},
"search": [
{
"ns": 0,
"title": "Ruby",
"pageid": 43551,
"size": 37725,
"wordcount": 3862,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum (aluminium oxide). \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is one of the most popular traditional jewelry",
"timestamp": "2025-03-13T12:31:27Z"
},
{
"ns": 0,
"title": "Ruby character",
"pageid": 43552,
"size": 17044,
"wordcount": 1606,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E characters or rubi characters (Japanese: ルビ; rōmaji: rubi; Korean: 루비; romaja: rubi) are small, annotative glosses that are usually placed above or",
"timestamp": "2025-01-17T22:35:06Z"
},
{
"ns": 0,
"title": "Jack Ruby",
"pageid": 16277,
"size": 95026,
"wordcount": 10111,
"snippet": "Jack Leon \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E (born Jacob Leon Rubenstein; c. March 25, 1911 – January 3, 1967) was an American nightclub owner who murdered Lee Harvey Oswald on November",
"timestamp": "2025-03-19T15:21:57Z"
},
{
"ns": 0,
"title": "Ruby (Jennie album)",
"pageid": 78945812,
"size": 64045,
"wordcount": 4864,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is the debut solo studio album by South Korean singer and rapper Jennie. Released on March 7, 2025, through Odd Atelier and Columbia Records, the",
"timestamp": "2025-03-20T08:35:51Z"
},
{
"ns": 0,
"title": "Ruby Franke",
"pageid": 74732982,
"size": 28004,
"wordcount": 2321,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Franke (née Griffiths) is an American convicted child abuser and former family vlogger who ran the now defunct YouTube channel 8 Passengers. On August",
"timestamp": "2025-03-14T12:52:45Z"
},
{
"ns": 0,
"title": "Ruby (programming language)",
"pageid": 25768,
"size": 61259,
"wordcount": 5602,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E is an interpreted, high-level, general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity.",
"timestamp": "2025-03-06T22:11:19Z"
},
{
"ns": 0,
"title": "Ruby (disambiguation)",
"pageid": 25607,
"size": 8474,
"wordcount": 1058,
"snippet": "Look up \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E or \u003Cspan class=\"searchmatch\"\u003Eruby\u003C/span\u003E in Wiktionary, the free dictionary. A \u003Cspan class=\"searchmatch\"\u003Eruby\u003C/span\u003E is a red gemstone. \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E may also refer to: \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Alaska \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Arizona \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E, Copiah County",
"timestamp": "2025-01-22T07:07:23Z"
},
{
"ns": 0,
"title": "Ruby Rose",
"pageid": 19242083,
"size": 65946,
"wordcount": 5145,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Rose Langenheim (born 20 March 1986) is an Australian actress, television presenter, and model. She gained prominence for her role in season three",
"timestamp": "2025-03-12T04:18:34Z"
},
{
"ns": 0,
"title": "Ruby Bridges",
"pageid": 1143659,
"size": 28397,
"wordcount": 2960,
"snippet": "\u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Nell Bridges Hall (born September 8, 1954) is an American civil rights activist. She was the first African American child to attend formerly whites-only",
"timestamp": "2025-03-15T17:33:45Z"
},
{
"ns": 0,
"title": "Ruby Ridge standoff",
"pageid": 501385,
"size": 108783,
"wordcount": 12341,
"snippet": "The \u003Cspan class=\"searchmatch\"\u003ERuby\u003C/span\u003E Ridge standoff was the siege of a cabin occupied by the Weaver family in Boundary County, Idaho, in August 1992. On August 21, deputies of the",
"timestamp": "2025-03-01T06:39:21Z"
}
]
}
}
となりました。これは、どういう事なのでしょうか?教えていただけると幸いです。
もし、説明不足でしたら、随時補足しますし、聞き返して頂けると嬉しいです。
一般的なAPIの利用方法を学びたいのか、Facebook APIでいいね数を取得したいのか、やりたい事はどちらになりますか?
書籍の情報が古すぎて、Rubyのopen-uri は3.0で既に廃止となった書き方をしています。
Facebook APIについては、2016年頃からアクセストークンが必須になっているはずで、まずはアプリの作成とトークンの取得が必要なはずです。
迅速な回答ありがとうございます。
それは、どちらにしても、
「Facebook APIについては、2016年頃からアクセストークンが必須になっているはずで、まずはアプリの作成とトークンの取得が必要なはずです。」
は、どうしてもやらなければいけないという事でしょうか?
教えて頂けると嬉しいです。
一般的なAPIの利用方法を学びたいだけであれば、補足情報の通りWikipediaのAPIの利用方法をしっておくだけでいいと思いますよ。
その場合はFacebook APIを使うわけではないのでアプリの作成も不要でしょう。
