Rubyから「はてなブックマークエントリー情報取得API」を叩いてみた。
・プログラム
# encoding: utf-8 require 'open-uri' require 'json' class Bookmark attr_accessor :user attr_accessor :tags attr_accessor :timestamp attr_accessor :comment end class Related attr_accessor :title attr_accessor :count attr_accessor :url attr_accessor :entry_url attr_accessor :eid end class HatenaBookmark API_URL = "http://b.hatena.ne.jp/entry/json/" attr_accessor :query_url attr_accessor :title attr_accessor :count attr_accessor :url attr_accessor :entry_url attr_accessor :screenshot attr_accessor :eid attr_accessor :bookmarks attr_accessor :related def initialize() @bookmarks = Array.new @related = Array.new end def search(query_url) request_url = API_URL + query_url begin open(request_url) { |f| response = JSON.parse(f.read) response.each_key do |key| case key when "title" @title = response[key] when"count" @count = response[key] when "url" @url = response[key] when "entry_url" @entry_url = response[key] when "screenshot" @screenshot = response[key] when "eid" @eid = response[key] when "bookmarks" response[key].each do |bm| bookmark = Bookmark.new bookmark.user = bm["user"] bookmark.tags = bm["tags"] bookmark.timestamp = bm["timestamp"] bookmark.comment = bm["comment"] @bookmarks.push(bookmark) end when "related" response[key].each do |rel| related = Related.new related.title = rel["title"] related.count = rel["count"] related.url = rel["url"] related.entry_url = rel["entry_url"] related.eid = rel["eid"] @related.push(related) end end end } rescue SocketError puts "SocketError" rescue OpenURI::HTTPError puts "HTTPError" end end end request_url = ARGV[0] hatebu = HatenaBookmark.new() hatebu.search(request_url) puts "タイトル: " + hatebu.title puts "ブックマーク数: " + hatebu.count puts "ブックマークURL: " + hatebu.url puts "エントリーURL: " + hatebu.entry_url puts "スクリーンショット: " + hatebu.screenshot puts "エントリーID: " + hatebu.eid puts "" puts "[ブックマーク始まり]" hatebu.bookmarks.each do |bookmark| puts "ユーザ: " + bookmark.user print "タグ: " bookmark.tags.each do |tag| print tag print " " end puts "" puts "ブックマーク時刻: " + bookmark.timestamp puts "コメント: " + bookmark.comment puts "" end puts "[ブックマーク終わり]" puts "" puts "[関連ページ始まり]" hatebu.related.each do |related| puts "タイトル: " + related.title puts "ブックマーク数: " + related.count puts "ブックマークURL: " + related.url puts "エントリーURL: " + related.entry_url puts "エントリーID: " + related.eid puts "" end puts "[関連ページ終わり]"
・実行
% ruby hatebu-api_test01.rb "http://tech.naver.jp/blog/?p=1369" タイトル: nginxの優雅な再起動 « NAVER Engineers' Blog ブックマーク数: 328 ブックマークURL: http://tech.naver.jp/blog/?p=1369 エントリーURL: http://b.hatena.ne.jp/entry/tech.naver.jp/blog/?p=1369 スクリーンショット: http://screenshot.hatena.ne.jp/images/200x150/0/d/d/3/6/e9a1aef9abd9f6bc1e41349937b41faeaff.jpg エントリーID: 86930913 [ブックマーク始まり] ユーザ: iwazer タグ: nginx setup ブックマーク時刻: 2012/03/27 16:57:11 コメント: 中略 ユーザ: tagomoris タグ: ブックマーク時刻: 2012/03/26 15:52:21 コメント: おお、nginxそんなことできるの [ブックマーク終わり] [関連ページ始まり] タイトル: そろそろApacheからNginxに移行したい人の、Nginx スターティング・ガイド ブックマーク数: 679 ブックマークURL: http://blog.av-jyo.com/2011-07/first-steps-for-switching-from-apache-to-nginx.html エントリーURL: http://b.hatena.ne.jp/entry/blog.av-jyo.com/2011-07/first-steps-for-switching-from-apache-to-nginx.html エントリーID: 49922314 タイトル: プロのサーバ管理者がApacheのStartServers, (Min|Max)SpareServers, MaxClientsを同じにする理由 - blog.nomadscafe.jp ブックマーク数: 519 ブックマークURL: http://blog.nomadscafe.jp/2010/09/apachestartservers-minmaxspareservers-maxclients.html エントリーURL: http://b.hatena.ne.jp/entry/blog.nomadscafe.jp/2010/09/apachestartservers-minmaxspareservers-maxclients.html エントリーID: 24526033 タイトル: 最速最安WEB画像サーバ構築術 - nginx + ServersMan - 新・浅く広くをモットーに | 一人WEBサービス屋メモ ブックマーク数: 571 ブックマークURL: http://d.hatena.ne.jp/uratch/20101224/1293173959 エントリーURL: http://b.hatena.ne.jp/entry/d.hatena.ne.jp/uratch/20101224/1293173959 エントリーID: 27615645 タイトル: ウノウラボ Unoh Labs: Tips for nginx ブックマーク数: 246 ブックマークURL: http://labs.unoh.net/2009/08/tips_for_nginx.html エントリーURL: http://b.hatena.ne.jp/entry/labs.unoh.net/2009/08/tips_for_nginx.html エントリーID: 15388176 タイトル: インフラエンジニアがSegmentation fault をなんとか治してみる - メモとかそんな感じなやつ ブックマーク数: 110 ブックマークURL: http://d.hatena.ne.jp/sarface2012/20101027 エントリーURL: http://b.hatena.ne.jp/entry/d.hatena.ne.jp/sarface2012/20101027 エントリーID: 26026686 [関連ページ終わり]