enju_ndl 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +35 -0
- data/app/controllers/ndl_books_controller.rb +38 -0
- data/app/helpers/ndl_books_helper.rb +11 -0
- data/app/models/ndl_book.rb +87 -0
- data/app/views/layouts/ndl_books.html.erb +23 -0
- data/app/views/ndl_books/index.html.erb +48 -0
- data/config/locales/en.yml +3 -0
- data/config/locales/ja.yml +3 -0
- data/config/routes.rb +3 -0
- data/lib/enju_ndl/crd.rb +67 -0
- data/lib/enju_ndl/engine.rb +4 -0
- data/lib/enju_ndl/ndl_search.rb +48 -0
- data/lib/enju_ndl/porta.rb +165 -0
- data/lib/enju_ndl/version.rb +3 -0
- data/lib/enju_ndl.rb +36 -0
- data/lib/tasks/enju_ndl_tasks.rake +4 -0
- data/test/enju_ndl_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +101 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'EnjuNdl'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :spec
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class NdlBooksController < ApplicationController
|
2
|
+
before_filter :authenticate_user!
|
3
|
+
before_filter :check_librarian
|
4
|
+
|
5
|
+
def index
|
6
|
+
if params[:page].to_i == 0
|
7
|
+
page = 1
|
8
|
+
else
|
9
|
+
page = params[:page]
|
10
|
+
end
|
11
|
+
@query = params[:query].to_s.strip
|
12
|
+
books = NdlBook.search(params[:query], page)
|
13
|
+
@books = WillPaginate::Collection.create(page, NdlBook.per_page, books[:total_entries]) do |pager|
|
14
|
+
pager.replace books[:items]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def create
|
19
|
+
if params[:book]
|
20
|
+
@manifestation = NdlBook.import_from_sru_response(params[:book][:nbn])
|
21
|
+
if @manifestation
|
22
|
+
flash[:notice] = t('controller.successfully_created', :model => t('activerecord.models.manifestation'))
|
23
|
+
flash[:porta_import] == true
|
24
|
+
redirect_to manifestation_items_url(@manifestation)
|
25
|
+
else
|
26
|
+
flash[:notice] = t('page.record_not_found')
|
27
|
+
redirect_to ndl_books_url
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def check_librarian
|
34
|
+
unless current_user.try(:has_role?, 'Librarian')
|
35
|
+
access_denied
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module NdlBooksHelper
|
3
|
+
def link_to_import(nbn)
|
4
|
+
manifestation = Manifestation.where(:nbn => nbn).first
|
5
|
+
unless manifestation
|
6
|
+
link_to '追加', ndl_books_path(:book => {:nbn => nbn}), :method => :post
|
7
|
+
else
|
8
|
+
'登録済み'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
class NdlBook
|
3
|
+
def self.search(query, page = 1, per_page = self.per_page)
|
4
|
+
if query
|
5
|
+
cnt = self.per_page
|
6
|
+
page = 1 if page.to_i < 1
|
7
|
+
idx = (page.to_i - 1) * cnt + 1
|
8
|
+
url = "http://api.porta.ndl.go.jp/servicedp/opensearch?dpid=zomoku&any=#{URI.encode(query.strip)}&cnt=#{cnt}&idx=#{idx}"
|
9
|
+
Rails.logger.debug url
|
10
|
+
xml = open(url).read
|
11
|
+
doc = Nokogiri::XML(xml)
|
12
|
+
items = doc.xpath('//channel/item')
|
13
|
+
total_entries = doc.at('//channel/openSearch:totalResults').content.to_i
|
14
|
+
{:items => items, :total_entries => total_entries}
|
15
|
+
else
|
16
|
+
{:items => [], :total_entries => 0}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.per_page
|
21
|
+
10
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.find_by_isbn(isbn)
|
25
|
+
url = "http://api.porta.ndl.go.jp/servicedp/opensearch?dpid=zomoku&isbn=#{isbn}&cnt=1&idx=1"
|
26
|
+
Rails.logger.debug url
|
27
|
+
xml = open(url).read
|
28
|
+
doc = Nokogiri::XML(xml).at('//channel/item')
|
29
|
+
{
|
30
|
+
:title => doc.at('./title').content,
|
31
|
+
:publisher => doc.xpath('./dc:publisher').collect(&:content).join(' '),
|
32
|
+
:creator => doc.xpath('./dc:creator[@xsi:type="dcndl:NDLNH"]').collect(&:content).join(' '),
|
33
|
+
:isbn => doc.at('./dc:identifier[@xsi:type="dcndl:ISBN"]').try(:content).to_s
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.import_from_sru_response(jpno)
|
38
|
+
manifestation = Manifestation.where(:nbn => jpno).first
|
39
|
+
return if manifestation
|
40
|
+
response = Nokogiri::XML(open("http://api.porta.ndl.go.jp/servicedp/SRUDp?operation=searchRetrive&maximumRecords=10&recordSchema=dcndl_porta&query=%28jpno=#{jpno}%29")).at('//xmlns:recordData', 'xmlns' => "http://www.loc.gov/zing/srw/")
|
41
|
+
return unless response.content
|
42
|
+
doc = Nokogiri::XML(response.content)
|
43
|
+
title = {
|
44
|
+
:manifestation => doc.xpath('//dc:title').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' '),
|
45
|
+
:transcription => doc.xpath('//dcndl:titleTranscription').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' '),
|
46
|
+
:original => doc.xpath('//dcterms:alternative').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' ')
|
47
|
+
}
|
48
|
+
lang = doc.at('//dc:language[@xsi:type="dcterms:ISO639-2"]').try(:content)
|
49
|
+
creators = []
|
50
|
+
doc.xpath('//dc:creator[@xsi:type="dcndl:NDLNH"]').each do |creator|
|
51
|
+
creators << creator.content.tr('a-zA-Z0-9 ‖', 'a-zA-Z0-9 ')
|
52
|
+
end
|
53
|
+
publishers = []
|
54
|
+
doc.xpath('//dc:publisher').each do |publisher|
|
55
|
+
publishers << publisher.content.tr('a-zA-Z0-9 ‖', 'a-zA-Z0-9 ')
|
56
|
+
end
|
57
|
+
pub_date = doc.at('//dcterms:issued').content.try(:tr, '0-9.', '0-9-').to_s.gsub(/(.*)/, '')
|
58
|
+
unless pub_date =~ /^\d+(-\d{0,2}){0,2}$/
|
59
|
+
pub_date = nil
|
60
|
+
end
|
61
|
+
|
62
|
+
Manifestation.transaction do
|
63
|
+
language = Language.where(:iso_639_2 => lang.downcase).first if lang
|
64
|
+
manifestation = Manifestation.new(
|
65
|
+
:original_title => title[:manifestation],
|
66
|
+
:title_transcription => title[:title_transcription],
|
67
|
+
:title_alternative => title[:original],
|
68
|
+
:pub_date => pub_date,
|
69
|
+
:isbn => doc.at('//dc:identifier[@xsi:type="dcndl:ISBN"]').try(:content),
|
70
|
+
:nbn => doc.at('//dc:identifier[@xsi:type="dcndl:JPNO"]').content,
|
71
|
+
:ndc => doc.at('//dc:subject[@xsi:type="dcndl:NDC"]').try(:content).try(:tr, '0-9.', '0-9.').to_s.gsub(/(.*)/, '')
|
72
|
+
)
|
73
|
+
manifestation.language = language if language
|
74
|
+
patron_creators = Patron.import_patrons(creators.zip([]).map{|f,t| {:full_name => f, :full_name_transcription => t}}).uniq
|
75
|
+
patron_publishers = Patron.import_patrons(publishers.zip([]).map{|f,t| {:full_name => f, :full_name_transcription => t}}).uniq
|
76
|
+
manifestation.creators << patron_creators
|
77
|
+
manifestation.publishers << patron_publishers
|
78
|
+
manifestation.save!
|
79
|
+
end
|
80
|
+
manifestation
|
81
|
+
end
|
82
|
+
|
83
|
+
attr_accessor :url
|
84
|
+
|
85
|
+
class AlreadyImported < StandardError
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= @locale.to_s -%>" lang="<%= @locale.to_s -%>">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8" />
|
5
|
+
<%= render 'page/include' %>
|
6
|
+
<%= render 'page/portlets' %>
|
7
|
+
<title><%= t('enju_ndl.import_from_porta') %></title>
|
8
|
+
</head>
|
9
|
+
<body itemscope itemtype="http://schema.org/WebPage">
|
10
|
+
|
11
|
+
<%= render 'page/header' %>
|
12
|
+
<%= render 'page/menu' %>
|
13
|
+
|
14
|
+
<div id="content">
|
15
|
+
|
16
|
+
<%= yield %>
|
17
|
+
|
18
|
+
</div>
|
19
|
+
|
20
|
+
<%= render 'page/footer' %>
|
21
|
+
|
22
|
+
</body>
|
23
|
+
</html>
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<div id="content_detail" class="ui-corner-all">
|
2
|
+
<h1 class="title">PORTA検索によるインポート</h1>
|
3
|
+
<div id="content_list">
|
4
|
+
<p id="notice"><%= notice %></p>
|
5
|
+
|
6
|
+
<%= form_for :books, :url => ndl_books_path, :html => {:method => 'get'} do -%>
|
7
|
+
<p>
|
8
|
+
<%= text_field_tag 'query', h(@query), {:id => 'search_form_top', :class => 'search_form'} -%>
|
9
|
+
<%= submit_tag '検索' -%>
|
10
|
+
</p>
|
11
|
+
<%- end -%>
|
12
|
+
|
13
|
+
<% if @query.present? %>
|
14
|
+
<p><%= @books.total_entries %> 件が見つかりました。</p>
|
15
|
+
|
16
|
+
<table class="index">
|
17
|
+
<tr>
|
18
|
+
<th></th>
|
19
|
+
<th>書名</th>
|
20
|
+
</tr>
|
21
|
+
<% @books.each do |book| %>
|
22
|
+
<tr class="line<%= cycle("0", "1") -%>">
|
23
|
+
<td>
|
24
|
+
<%= link_to_import(book.at('./dc:identifier[@xsi:type="dcndl:JPNO"]').try(:content).to_s) %>
|
25
|
+
</td>
|
26
|
+
<td>
|
27
|
+
<%= link_to book.at('./title').content, book.at('./link').content %>
|
28
|
+
<br />
|
29
|
+
<%= book.xpath('./dc:creator[@xsi:type="dcndl:NDLNH"]').collect(&:content).join(' ') %>
|
30
|
+
<%= book.xpath('./dc:publisher').collect(&:content).join(' ') %>
|
31
|
+
ISBN: <%= book.at('./dc:identifier[@xsi:type="dcndl:ISBN"]').try(:content).to_s %>
|
32
|
+
</td>
|
33
|
+
</tr>
|
34
|
+
<% end %>
|
35
|
+
</table>
|
36
|
+
|
37
|
+
<br />
|
38
|
+
|
39
|
+
<%= will_paginate(@books) %>
|
40
|
+
<% else %>
|
41
|
+
<%= javascript_tag("$('#search_form_top').focus()") -%>
|
42
|
+
<% end %>
|
43
|
+
|
44
|
+
</div>
|
45
|
+
</div>
|
46
|
+
|
47
|
+
<div id="submenu" class="ui-corner-all">
|
48
|
+
</div>
|
data/config/routes.rb
ADDED
data/lib/enju_ndl/crd.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module EnjuNdl
|
3
|
+
module Crd
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def get_crd_response(options)
|
10
|
+
params = {:query_logic => 1, :results_num => 1, :results_num => 200, :sort => 10}.merge(options)
|
11
|
+
query = []
|
12
|
+
query << "01_#{params[:query_01].to_s.gsub(' ', ' ')}" if params[:query_01]
|
13
|
+
query << "02_#{params[:query_02].to_s.gsub(' ', ' ')}" if params[:query_02]
|
14
|
+
delimiter = '.'
|
15
|
+
url = "http://crd.ndl.go.jp/refapi/servlet/refapi.RSearchAPI?query=#{URI.escape(query.join(delimiter))}&query_logic=#{params[:query_logic]}&results_get_position=#{params[:results_get_position]}&results_num=#{params[:results_num]}&sort=#{params[:sort]}"
|
16
|
+
|
17
|
+
xml = open(url).read.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_crd(options)
|
21
|
+
params = {:page => 1}.merge(options)
|
22
|
+
crd_page = params[:page].to_i
|
23
|
+
crd_page = 1 if crd_page <= 0
|
24
|
+
crd_startrecord = (crd_page - 1) * Question.crd_per_page + 1
|
25
|
+
params[:results_get_position] = crd_startrecord
|
26
|
+
params[:results_num] = Question.crd_per_page
|
27
|
+
|
28
|
+
xml = get_crd_response(params)
|
29
|
+
doc = Nokogiri::XML(xml)
|
30
|
+
|
31
|
+
response = {
|
32
|
+
:results_num => doc.at('//xmlns:hit_num').try(:content).to_i,
|
33
|
+
:results => []
|
34
|
+
}
|
35
|
+
doc.xpath('//xmlns:result').each do |result|
|
36
|
+
set = {
|
37
|
+
:question => result.at('QUESTION').try(:content),
|
38
|
+
:reg_id => result.at('REG-ID').try(:content),
|
39
|
+
:answer => result.at('ANSWER').try(:content),
|
40
|
+
:crt_date => result.at('CRT-DATE').try(:content),
|
41
|
+
:solution => result.at('SOLUTION').try(:content),
|
42
|
+
:lib_id => result.at('LIB-ID').try(:content),
|
43
|
+
:lib_name => result.at('LIB-NAME').try(:content),
|
44
|
+
:url => result.at('URL').try(:content),
|
45
|
+
:ndc => result.at('NDC').try(:content)
|
46
|
+
}
|
47
|
+
begin
|
48
|
+
set[:keyword] = result.xpath('xmlns:KEYWORD').collect(&:content)
|
49
|
+
rescue NoMethodError
|
50
|
+
set[:keyword] = []
|
51
|
+
end
|
52
|
+
response[:results] << set
|
53
|
+
end
|
54
|
+
|
55
|
+
crd_count = response[:results_num]
|
56
|
+
if crd_count > 1000
|
57
|
+
crd_total_count = 1000
|
58
|
+
else
|
59
|
+
crd_total_count = crd_count
|
60
|
+
end
|
61
|
+
|
62
|
+
resources = response[:results]
|
63
|
+
crd_results = WillPaginate::Collection.create(crd_page, Question.crd_per_page, crd_total_count) do |pager| pager.replace(resources) end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module EnjuNdl
|
3
|
+
module NdlSearch
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def rss_import(url)
|
10
|
+
doc = Nokogiri::XML(open(url))
|
11
|
+
ns = {"dc" => "http://purl.org/dc/elements/1.1/", "xsi" => "http://www.w3.org/2001/XMLSchema-instance", "dcndl" => "http://ndl.go.jp/dcndl/terms/"}
|
12
|
+
doc.xpath('//item', ns).each do |item|
|
13
|
+
isbn = item.at('./dc:identifier[@xsi:type="dcndl:ISBN"]').try(:content)
|
14
|
+
ndl_bib_id = item.at('./dc:identifier[@xsi:type="dcndl:NDLBibID"]').try(:content)
|
15
|
+
manifestation = Manifestation.where(:ndl_bib_id => ndl_bib_id).first
|
16
|
+
manifestation = Manifestation.find_by_isbn(isbn) unless manifestation
|
17
|
+
# FIXME: 日本語決めうち?
|
18
|
+
language_id = Language.where(:iso_639_2 => 'jpn').first.id rescue 1
|
19
|
+
unless manifestation
|
20
|
+
manifestation = self.new(
|
21
|
+
:original_title => item.at('./dc:title').content,
|
22
|
+
:title_transcription => item.at('./dcndl:titleTranscription').try(:content),
|
23
|
+
:isbn => isbn,
|
24
|
+
:ndl_bib_id => ndl_bib_id,
|
25
|
+
:description => item.at('./dc:description').try(:content),
|
26
|
+
:pub_date => item.at('./dc:date').try(:content).try(:gsub, '.', '-'),
|
27
|
+
:language_id => language_id
|
28
|
+
)
|
29
|
+
if manifestation.valid?
|
30
|
+
item.xpath('./dc:creator').each_with_index do |creator, i|
|
31
|
+
next if i == 0
|
32
|
+
patron = Patron.where(:full_name => creator.try(:content)).first
|
33
|
+
patron = Patron.new(:full_name => creator.try(:content)) unless patron
|
34
|
+
manifestation.creators << patron if patron.valid?
|
35
|
+
end
|
36
|
+
item.xpath('./dc:publisher').each_with_index do |publisher, i|
|
37
|
+
patron = Patron.where(:full_name => publisher.try(:content)).first
|
38
|
+
patron = Patron.new(:full_name => publisher.try(:content)) unless patron
|
39
|
+
manifestation.publishers << patron if patron.valid?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
Sunspot.commit
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module EnjuNdl
|
3
|
+
module Porta
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def import_isbn(isbn)
|
10
|
+
isbn = ISBN_Tools.cleanup(isbn)
|
11
|
+
raise EnjuNdl::InvalidIsbn unless ISBN_Tools.is_valid?(isbn)
|
12
|
+
|
13
|
+
manifestation = Manifestation.find_by_isbn(isbn)
|
14
|
+
return manifestation if manifestation
|
15
|
+
|
16
|
+
doc = return_xml(isbn)
|
17
|
+
raise EnjuNdl::RecordNotFound if doc.at('//openSearch:totalResults').content.to_i == 0
|
18
|
+
|
19
|
+
pub_date, language, nbn = nil, nil, nil
|
20
|
+
|
21
|
+
publishers = get_publishers(doc).zip([]).map{|f,t| {:full_name => f, :full_name_transcription => t}}
|
22
|
+
|
23
|
+
# title
|
24
|
+
title = get_title(doc)
|
25
|
+
|
26
|
+
# date of publication
|
27
|
+
pub_date = doc.at('//dcterms:issued').content.try(:tr, '0-9.', '0-9-').to_s.gsub(/(.*)/, '')
|
28
|
+
unless pub_date =~ /^\d+(-\d{0,2}){0,2}$/
|
29
|
+
pub_date = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
language = get_language(doc)
|
33
|
+
nbn = doc.at('//dc:identifier[@xsi:type="dcndl:JPNO"]').content
|
34
|
+
ndc = doc.at('//dc:subject[@xsi:type="dcndl:NDC"]').try(:content)
|
35
|
+
|
36
|
+
Patron.transaction do
|
37
|
+
publisher_patrons = Patron.import_patrons(publishers)
|
38
|
+
language_id = Language.where(:iso_639_2 => language).first.id rescue 1
|
39
|
+
|
40
|
+
manifestation = Manifestation.new(
|
41
|
+
:original_title => title[:manifestation],
|
42
|
+
:title_transcription => title[:transcription],
|
43
|
+
# TODO: PORTAに入っている図書以外の資料を調べる
|
44
|
+
#:carrier_type_id => CarrierType.where(:name => 'print').first.id,
|
45
|
+
:language_id => language_id,
|
46
|
+
:isbn => isbn,
|
47
|
+
:pub_date => pub_date,
|
48
|
+
:nbn => nbn,
|
49
|
+
:ndc => ndc
|
50
|
+
)
|
51
|
+
manifestation.ndc = ndc
|
52
|
+
manifestation.publishers << publisher_patrons
|
53
|
+
end
|
54
|
+
|
55
|
+
#manifestation.send_later(:create_frbr_instance, doc.to_s)
|
56
|
+
create_frbr_instance(doc, manifestation)
|
57
|
+
return manifestation
|
58
|
+
end
|
59
|
+
|
60
|
+
def import_isbn!(isbn)
|
61
|
+
manifestation = import_isbn(isbn)
|
62
|
+
manifestation.save!
|
63
|
+
manifestation
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_frbr_instance(doc, manifestation)
|
67
|
+
title = get_title(doc)
|
68
|
+
creators = get_creators(doc).zip([]).map{|f,t| {:full_name => f, :full_name_transcription => t}}
|
69
|
+
language = get_language(doc)
|
70
|
+
subjects = get_subjects(doc)
|
71
|
+
|
72
|
+
Patron.transaction do
|
73
|
+
creator_patrons = Patron.import_patrons(creators)
|
74
|
+
language_id = Language.where(:iso_639_2 => language).first.id rescue 1
|
75
|
+
content_type_id = ContentType.where(:name => 'text').first.id rescue 1
|
76
|
+
manifestation.creators << creator_patrons
|
77
|
+
if defined?(Subject)
|
78
|
+
subjects.each do |term|
|
79
|
+
subject = Subject.where(:term => term).first
|
80
|
+
manifestation.subjects << subject if subject
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def search_porta(query, options = {})
|
87
|
+
options = {:item => 'any', :startrecord => 1, :per_page => 10, :raw => false}.merge(options)
|
88
|
+
doc = nil
|
89
|
+
results = {}
|
90
|
+
startrecord = options[:startrecord].to_i
|
91
|
+
if startrecord == 0
|
92
|
+
startrecord = 1
|
93
|
+
end
|
94
|
+
url = "http://api.porta.ndl.go.jp/servicedp/opensearch?dpid=#{options[:dpid]}&#{options[:item]}=#{URI.escape(query)}&cnt=#{options[:per_page]}&idx=#{startrecord}"
|
95
|
+
if options[:raw] == true
|
96
|
+
open(url).read
|
97
|
+
else
|
98
|
+
RSS::Rss::Channel.install_text_element("openSearch:totalResults", "http://a9.com/-/spec/opensearchrss/1.0/", "?", "totalResults", :text, "openSearch:totalResults")
|
99
|
+
RSS::BaseListener.install_get_text_element "http://a9.com/-/spec/opensearchrss/1.0/", "totalResults", "totalResults="
|
100
|
+
feed = RSS::Parser.parse(url, false)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def normalize_isbn(isbn)
|
105
|
+
if isbn.length == 10
|
106
|
+
ISBN_Tools.isbn10_to_isbn13(isbn)
|
107
|
+
else
|
108
|
+
ISBN_Tools.isbn13_to_isbn10(isbn)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def return_xml(isbn)
|
113
|
+
xml = self.search_porta(isbn, {:dpid => 'zomoku', :item => 'isbn', :raw => true}).to_s
|
114
|
+
doc = Nokogiri::XML(xml)
|
115
|
+
if doc.at('//openSearch:totalResults').content.to_i == 0
|
116
|
+
isbn = normalize_isbn(isbn)
|
117
|
+
xml = self.search_porta(isbn, {:dpid => 'zomoku', :item => 'isbn', :raw => true}).to_s
|
118
|
+
doc = Nokogiri::XML(xml)
|
119
|
+
end
|
120
|
+
doc
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
def get_title(doc)
|
125
|
+
title = {
|
126
|
+
:manifestation => doc.xpath('//item[1]/title').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' '),
|
127
|
+
:transcription => doc.xpath('//item[1]/dcndl:titleTranscription').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' '),
|
128
|
+
:original => doc.xpath('//dcterms:alternative').collect(&:content).join(' ').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ').squeeze(' ')
|
129
|
+
}
|
130
|
+
end
|
131
|
+
|
132
|
+
def get_creators(doc)
|
133
|
+
creators = []
|
134
|
+
doc.xpath('//item[1]/dc:creator[@xsi:type="dcndl:NDLNH"]').each do |creator|
|
135
|
+
creators << creator.content.gsub('‖', '').tr('a-zA-Z0-9 ', 'a-zA-Z0-9 ')
|
136
|
+
end
|
137
|
+
creators
|
138
|
+
end
|
139
|
+
|
140
|
+
def get_subjects(doc)
|
141
|
+
subjects = []
|
142
|
+
doc.xpath('//item[1]/dc:subject[@xsi:type="dcndl:NDLSH"]').each do |subject|
|
143
|
+
subjects << subject.content.tr('a-zA-Z0-9 ‖', 'a-zA-Z0-9 ')
|
144
|
+
end
|
145
|
+
return subjects
|
146
|
+
end
|
147
|
+
|
148
|
+
def get_language(doc)
|
149
|
+
# TODO: 言語が複数ある場合
|
150
|
+
language = doc.xpath('//item[1]/dc:language[@xsi:type="dcterms:ISO639-2"]').first.content.downcase
|
151
|
+
end
|
152
|
+
|
153
|
+
def get_publishers(doc)
|
154
|
+
publishers = []
|
155
|
+
doc.xpath('//item[1]/dc:publisher').each do |publisher|
|
156
|
+
publishers << publisher.content.tr('a-zA-Z0-9 ‖', 'a-zA-Z0-9 ')
|
157
|
+
end
|
158
|
+
return publishers
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class AlreadyImported < StandardError
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
data/lib/enju_ndl.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "enju_ndl/engine"
|
3
|
+
require 'open-uri'
|
4
|
+
require 'enju_ndl/ndl_search'
|
5
|
+
require 'enju_ndl/crd'
|
6
|
+
require 'enju_ndl/porta'
|
7
|
+
|
8
|
+
module EnjuNdl
|
9
|
+
module ActsAsMethods
|
10
|
+
def self.included(base)
|
11
|
+
base.extend ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def enju_ndl_search
|
16
|
+
include EnjuNdl::NdlSearch
|
17
|
+
end
|
18
|
+
|
19
|
+
def enju_ndl_crd
|
20
|
+
include EnjuNdl::Crd
|
21
|
+
end
|
22
|
+
|
23
|
+
def enju_ndl_porta
|
24
|
+
include EnjuNdl::Porta
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class RecordNotFound < StandardError
|
30
|
+
end
|
31
|
+
|
32
|
+
class InvalidIsbn < StandardError
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
ActiveRecord::Base.send :include, EnjuNdl::ActsAsMethods
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
+
require "rails/test_help"
|
6
|
+
|
7
|
+
Rails.backtrace_cleaner.remove_silencers!
|
8
|
+
|
9
|
+
# Load support files
|
10
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enju_ndl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kosuke Tanabe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70201338394300 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70201338394300
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3
|
27
|
+
requirement: &70201338393660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70201338393660
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec-rails
|
38
|
+
requirement: &70201338392740 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70201338392740
|
47
|
+
description: NDL WebAPI wrapper for Next-L Enju
|
48
|
+
email:
|
49
|
+
- tanabe@mwr.mediacom.keio.ac.jp
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- app/controllers/ndl_books_controller.rb
|
55
|
+
- app/helpers/ndl_books_helper.rb
|
56
|
+
- app/models/ndl_book.rb
|
57
|
+
- app/views/layouts/ndl_books.html.erb
|
58
|
+
- app/views/ndl_books/index.html.erb
|
59
|
+
- config/locales/en.yml
|
60
|
+
- config/locales/ja.yml
|
61
|
+
- config/routes.rb
|
62
|
+
- lib/enju_ndl/crd.rb
|
63
|
+
- lib/enju_ndl/engine.rb
|
64
|
+
- lib/enju_ndl/ndl_search.rb
|
65
|
+
- lib/enju_ndl/porta.rb
|
66
|
+
- lib/enju_ndl/version.rb
|
67
|
+
- lib/enju_ndl.rb
|
68
|
+
- lib/tasks/enju_ndl_tasks.rake
|
69
|
+
- MIT-LICENSE
|
70
|
+
- Rakefile
|
71
|
+
- README.rdoc
|
72
|
+
- test/enju_ndl_test.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
homepage: https://github.com/nabeta/enju_ndl
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.8.11
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: enju_ndl plugin
|
98
|
+
test_files:
|
99
|
+
- test/enju_ndl_test.rb
|
100
|
+
- test/test_helper.rb
|
101
|
+
has_rdoc:
|