@@ -8,11 +8,14 @@ def import_isbn(isbn)
88 manifestation
99 end
1010
11+ def ndl_bib_doc ( ndl_bib_id )
12+ url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=itemno=#{ ndl_bib_id } &recordSchema=dcndl&onlyBib=true"
13+ Nokogiri ::XML ( Nokogiri ::XML ( URI . parse ( url ) . read ) . at ( '//xmlns:recordData' ) . content )
14+ end
15+
1116 # Use http://www.ndl.go.jp/jp/dlib/standards/opendataset/aboutIDList.txt
1217 def import_ndl_bib_id ( ndl_bib_id )
13- url = "https://iss.ndl.go.jp/books/R100000002-I#{ ndl_bib_id } -00.rdf"
14- doc = Nokogiri ::XML ( Faraday . get ( url ) . body )
15- import_record ( doc )
18+ import_record ( ndl_bib_doc ( ndl_bib_id ) )
1619 end
1720
1821 def import_from_ndl_search ( options )
@@ -105,6 +108,10 @@ def import_record(doc)
105108 when 'http://purl.org/dc/dcmitype/StillImage'
106109 content_type = ContentType . find_by ( name : 'still_image' )
107110 end
111+
112+ # NDLサーチのmaterialTypeは複数設定されているが、
113+ # content_typeはその最初の1件を用いて取得する
114+ break if content_type
108115 end
109116
110117 admin_identifier = doc . at ( '//dcndl:BibAdminResource[@rdf:about]' ) . attributes [ 'about' ] . value
@@ -238,14 +245,15 @@ def create_additional_attributes(doc, manifestation)
238245 end
239246
240247 def search_ndl ( query , options = { } )
241- options = { dpid : 'iss-ndl-opac' , item : 'any' , idx : 1 , per_page : 10 , raw : false , mediatype : 1 } . merge ( options )
248+ options = { dpid : 'iss-ndl-opac' , item : 'any' , idx : 1 , per_page : 10 , raw : false , mediatype : 'books' } . merge ( options )
242249 doc = nil
243250 results = { }
244251 startrecord = options [ :idx ] . to_i
245252 startrecord = 1 if startrecord == 0
246- 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 ] } "
253+ 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 ] } "
254+
247255 if options [ :raw ] == true
248- Faraday . get ( url ) . body
256+ URI . parse ( url ) . read
249257 else
250258 RSS ::Rss ::Channel . install_text_element ( 'openSearch:totalResults' , 'http://a9.com/-/spec/opensearchrss/1.0/' , '?' , 'totalResults' , :text , 'openSearch:totalResults' )
251259 RSS ::BaseListener . install_get_text_element 'http://a9.com/-/spec/opensearchrss/1.0/' , 'totalResults' , 'totalResults='
@@ -263,14 +271,16 @@ def normalize_isbn(isbn)
263271
264272 def return_xml ( isbn : nil , jpno : nil )
265273 if jpno . present?
266- rss = search_ndl ( jpno , dpid : 'iss-ndl-opac' , item : 'jpno' )
274+ url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=jpno=#{ jpno } &recordSchema=dcndl&onlyBib=true"
275+ elsif isbn . present?
276+ url = "https://ndlsearch.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&query=isbn=#{ isbn } &recordSchema=dcndl&onlyBib=true"
267277 else
268- rss = search_ndl ( isbn , dpid : 'iss-ndl-opac' , item : 'isbn' )
278+ return
269279 end
270280
271- if rss . items . first
272- doc = Nokogiri :: XML ( Faraday . get ( " #{ rss . items . first . link } .rdf" ) . body )
273- end
281+ response = Nokogiri :: XML ( URI . parse ( url ) . read ) . at ( '//xmlns:recordData' ) &. content
282+
283+ Nokogiri :: XML ( response ) if response
274284 end
275285
276286 private
0 commit comments