Last active
December 9, 2015 22:29
-
-
Save masao/4337721 to your computer and use it in GitHub Desktop.
Check the library holdings at CiNii books and NDL Search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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