Skip to content

Instantly share code, notes, and snippets.

@masao
Last active December 9, 2015 22:29
Show Gist options
  • Save masao/4337721 to your computer and use it in GitHub Desktop.
Save masao/4337721 to your computer and use it in GitHub Desktop.
Check the library holdings at CiNii books and NDL Search.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "uri"
require "open-uri"
require "rexml/document"
require "rexml/xpath"
baseurl = {
:NDL => "http://iss.ndl.go.jp/api/opensearch",
:CINII => "http://ci.nii.ac.jp/books/opensearch/search",
}
NS_MAPPING = {
"opensearch11" => "http://a9.com/-/spec/opensearch/1.1/",
"opensearch10" => "http://a9.com/-/spec/opensearchrss/1.0/",
}
if $0 == __FILE__
q = ARGV[0] || "isbn=978-4-8204-1219-9"
baseurl.each do |k, u|
url = u + "?format=rss&#{ URI.escape q }"
cont = open( url ){|io| io.read }
doc = REXML::Document.new( cont )
root = doc.root
total_e = REXML::XPath.first( doc, "//opensearch11:totalResults", NS_MAPPING )
total_e = REXML::XPath.first( doc, "//opensearch10:totalResults", NS_MAPPING ) if total_e.nil?
total = total_e.text.to_i
#p items
if total > 0
puts "[#{ k }] #{ total } hits:"
root.elements.each( "//item" ) do |item|
url = item.elements[ "./link" ].text
case k
when :CINII, :NDL
cont = open( url + ".rdf" ){|io| io.read }
#puts cont
rdf = REXML::Document.new( cont )
authors = rdf.elements.to_a( "//dc:creator" )
authors_s = authors.map{|e| e.first }.join( ", " )
title = rdf.elements[ "//dc:title" ]
title = if title.elements[ "//rdf:value" ]
title.elements[ "//rdf:value" ].text
else
title.text
end
puts [ authors_s, title ].join( ": " )
owners = []
owners = rdf.root.elements.to_a( "//bibo:owner" ) if k == :CINII
owners = rdf.root.elements.to_a( "//dcndl:holdingAgent" ) if k == :NDL
owners.each do |owner|
puts "# " << owner.elements.to_a( ".//foaf:name" ).first.text
end
puts [ url, "#{ owners.size } holdings." ].join( " - " )
else
puts url
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment