Skip to content

Commit

Permalink
follow API changes in NDL Search (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeta authored Feb 5, 2024
1 parent add3da3 commit 29bb273
Show file tree
Hide file tree
Showing 65 changed files with 2,774 additions and 4,122 deletions.
2 changes: 1 addition & 1 deletion app/helpers/enju_biblio/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def identifier_link(identifier)
when 'doi'
link_to identifier.body, "https://doi.org/#{identifier.body}"
when 'iss_itemno'
link_to identifier.body, "https://iss.ndl.go.jp/books/#{identifier.body}"
link_to identifier.body, "https://ndlsearch.ndl.go.jp/books/#{identifier.body}"
when 'lccn'
link_to identifier.body, "https://lccn.loc.gov/#{identifier.body}"
when 'ncid'
Expand Down
32 changes: 21 additions & 11 deletions app/models/concerns/enju_ndl/enju_manifestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ def import_isbn(isbn)
manifestation
end

def ndl_bib_doc(ndl_bib_id)
url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=itemno=#{ndl_bib_id}&recordSchema=dcndl&onlyBib=true"
Nokogiri::XML(Nokogiri::XML(URI.parse(url).read).at('//xmlns:recordData').content)
end

# Use http://www.ndl.go.jp/jp/dlib/standards/opendataset/aboutIDList.txt
def import_ndl_bib_id(ndl_bib_id)
url = "https://iss.ndl.go.jp/books/R100000002-I#{ndl_bib_id}-00.rdf"
doc = Nokogiri::XML(Faraday.get(url).body)
import_record(doc)
import_record(ndl_bib_doc(ndl_bib_id))
end

def import_from_ndl_search(options)
Expand Down Expand Up @@ -105,6 +108,10 @@ def import_record(doc)
when 'http://purl.org/dc/dcmitype/StillImage'
content_type = ContentType.find_by(name: 'still_image')
end

# NDLサーチのmaterialTypeは複数設定されているが、
# content_typeはその最初の1件を用いて取得する
break if content_type
end

admin_identifier = doc.at('//dcndl:BibAdminResource[@rdf:about]').attributes['about'].value
Expand Down Expand Up @@ -238,14 +245,15 @@ def create_additional_attributes(doc, manifestation)
end

def search_ndl(query, options = {})
options = { dpid: 'iss-ndl-opac', item: 'any', idx: 1, per_page: 10, raw: false, mediatype: 1 }.merge(options)
options = { dpid: 'iss-ndl-opac', item: 'any', idx: 1, per_page: 10, raw: false, mediatype: 'books' }.merge(options)
doc = nil
results = {}
startrecord = options[:idx].to_i
startrecord = 1 if startrecord == 0
url = "https://iss.ndl.go.jp/api/opensearch?dpid=#{options[:dpid]}&#{options[:item]}=#{format_query(query)}&cnt=#{options[:per_page]}&idx=#{startrecord}&mediatype=#{options[:mediatype]}"
url = "https://ndlsearch.ndl.go.jp/api/opensearch?dpid=#{options[:dpid]}&#{options[:item]}=#{format_query(query)}&cnt=#{options[:per_page]}&idx=#{startrecord}&mediatype=#{options[:mediatype]}"

if options[:raw] == true
Faraday.get(url).body
URI.parse(url).read
else
RSS::Rss::Channel.install_text_element('openSearch:totalResults', 'http://a9.com/-/spec/opensearchrss/1.0/', '?', 'totalResults', :text, 'openSearch:totalResults')
RSS::BaseListener.install_get_text_element 'http://a9.com/-/spec/opensearchrss/1.0/', 'totalResults', 'totalResults='
Expand All @@ -263,14 +271,16 @@ def normalize_isbn(isbn)

def return_xml(isbn: nil, jpno: nil)
if jpno.present?
rss = search_ndl(jpno, dpid: 'iss-ndl-opac', item: 'jpno')
url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=jpno=#{jpno}&recordSchema=dcndl&onlyBib=true"
elsif isbn.present?
url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=isbn=#{isbn}&recordSchema=dcndl&onlyBib=true"
else
rss = search_ndl(isbn, dpid: 'iss-ndl-opac', item: 'isbn')
return
end

if rss.items.first
doc = Nokogiri::XML(Faraday.get("#{rss.items.first.link}.rdf").body)
end
response = Nokogiri::XML(URI.parse(url).read).at('//xmlns:recordData')&.content

Nokogiri::XML(response) if response
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/enju_oai/oai_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def to_dcndl
identifiers.each do |identifier|
case identifier.identifier_type.try(:name)
when 'isbn'
xml.tag! "rdfs:seeAlso", "rdf:resource" => "http://iss.ndl.go.jp/isbn/#{identifier.body}"
# xml.tag! "rdfs:seeAlso", "rdf:resource" => "http://iss.ndl.go.jp/isbn/#{identifier.body}"
xml.tag! "dcterms:identifier", identifier.body, "rdf:datatype" => "http://ndl.go.jp/dcndl/terms/ISBN"
when 'issn'
xml.tag! "dcterms:identifier", identifier.body, "rdf:datatype" => "http://ndl.go.jp/dcndl/terms/ISSN"
Expand Down
11 changes: 8 additions & 3 deletions app/models/ndl_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.search(query, page = 1, _per_page = per_page)
cnt = per_page
page = 1 if page.to_i < 1
idx = (page.to_i - 1) * cnt + 1
doc = Nokogiri::XML(Manifestation.search_ndl(query, cnt: cnt, page: page, idx: idx, raw: true, mediatype: 1).to_s)
doc = Nokogiri::XML(Manifestation.search_ndl(query, cnt: cnt, page: page, idx: idx, raw: true, mediatype: 'books').to_s)
items = doc.xpath('//channel/item').map{|node| new node }
total_entries = doc.at('//channel/openSearch:totalResults').content.to_i

Expand All @@ -66,7 +66,7 @@ def self.import_from_sru_response(itemno)
identifier = NdlBibIdRecord.find_by(body: itemno)
return if identifier

url = "https://iss.ndl.go.jp/api/sru?operation=searchRetrieve&recordSchema=dcndl&maximumRecords=1&query=%28itemno=#{itemno}%29&onlyBib=true"
url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&recordSchema=dcndl&maximumRecords=1&query=%28itemno=#{itemno}%29&onlyBib=true"
xml = Faraday.get(url).body
response = Nokogiri::XML(xml).at('//xmlns:recordData')
return unless response.try(:content)
Expand All @@ -75,7 +75,12 @@ def self.import_from_sru_response(itemno)
end

def subjects
@node.xpath('//dcterms:subject/rdf:Description').map{|a| {id: a.attributes['about'].content, value: a.at('./rdf:value').content}}
@node.xpath('//dcterms:subject/rdf:Description').map{|a|
{
id: a.attributes['about'].content,
value: a.at('./rdf:value').content
}
}
end

def authors
Expand Down
2 changes: 1 addition & 1 deletion app/views/ndl_books/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<% if @query.present? %>
<div id="submenu" class="ui-corner-all ui-widget-content">
<ul>
<li><%= link_to t('enju_ndl.same_query_in_ndl_search'), "https://iss.ndl.go.jp/books?any=#{u(@query)}" -%></li>
<li><%= link_to t('enju_ndl.same_query_in_ndl_search'), "https://ndlsearch.ndl.go.jp/books?any=#{u(@query)}" -%></li>
</ul>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/page/about.en.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</li>
<li>This system has been using bibliographic data from the following services and databases.
<ul>
<li><a href="https://iss.ndl.go.jp/">NDL Search</a></li>
<li><a href="https://ndlsearch.ndl.go.jp/">NDL Search</a></li>
<li><a href="https://crd.ndl.go.jp/jp/public/">National Diet Library Collaborative Reference Database</a></li>
</ul>
</li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/page/about.ja.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</li>
<li>このシステムは、以下のサービスやデータベースから得た書誌データを利用しています。
<ul>
<li><a href="https://iss.ndl.go.jp/">国立国会図書館サーチ (NDLサーチ)</a></li>
<li><a href="https://ndlsearch.ndl.go.jp/">国立国会図書館サーチ (NDLサーチ)</a></li>
<li><a href="https://crd.ndl.go.jp/jp/public/">国立国会図書館レファレンス協同データベース</a></li>
</ul>
</li>
Expand Down
Loading

0 comments on commit 29bb273

Please sign in to comment.