調べながら30分くらいでシュッと書いた.あとでもうちょい綺麗にする.
検索には国立国会図書館サーチのAPIを使った.
require 'rexml/document' require 'csv' require 'rest-client' # usage: # bundle exec ruby search.rb source.csv > result.csv table = CSV.table ARGV[0] table.each do |row| res = RestClient.get "http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&query=isbn=#{row[:isbn]}" xml = REXML::Document.new res.to_str record = REXML::XPath.match(xml, '/searchRetrieveResponse/records/record').first data = record.elements['recordData'] row[:title] = data.to_s.match(/<dc:title>(.+?)<\/dc:title>/)[1] row[:author] = data.to_s.match(/<dc:creator>(.+?)<\/dc:creator>/)[1] sleep 2 end print table.to_csv
ネームスペースが登録されてなくてdc:title
みたいなタグ抜くの大変だったので雑に正規表現でやった.
↓こんな感じでCSV書いてsoruceとして指定すると良い
isbn,title,author 9784873113074,, 9784873115658,,
こんなのが出力される
isbn,title,author 9784873113074,エンジニアのための時間管理術,"Thomas A.Limoncelli 著,クイープ 訳" 9784873115658,リーダブルコード : より良いコードを書くためのシンプルで実践的なテクニック,"Dustin Boswell, Trevor Foucher 著,角征典 訳"
ワイ