require "faraday" require 'nokogiri' require 'sqlite3' require 'time' require 'date' class NdlSearch def get_book_info(title, creator = nil) data = [] query = { :mediatype => 1, :cnt => 1 } query[:title] = title query[:creator] = creator if creator print "query :#{query}" puts puts response = ndl_get('/api/opensearch', query) xml = Nokogiri::XML(response.body) xml.remove_namespaces! items = xml.xpath('//item') unless items.any? then #puts 'no item' data << {"totalResults"=>"0"} else #pp items.to_s items.each do |item| #puts #puts item book = {} item.children.each do |c| key = c.name next if key == 'text' val = "#{c.content}" label = c.attribute("type") if label label = "#{label}".gsub(/^dcndl:|^dcterms:/,'') book[label] ||= [] book[label] << val unless book[label].include?(val) val = "#{label}:#{val}" end book[key] ||= [] book[key] << val unless book[key].include?(val) end book = book.map {|key,val| [key, val.join(',')]}.to_h data << book end end #puts #puts data data end private def ndl_get(path, pram) con = Faraday.new(:url => 'https://iss.ndl.go.jp') do |f| f.request :url_encoded #f.response :logger f.adapter :net_http end con.get path, pram end end #DB SQL =< book_title #book_data[1] #=> author #book_data[2] #=> title #book_data[3] #==> id mangathank_title = book_data[2].to_s.gsub(/\'/, "\'\'") if book_data[2] == "null" then #p count #pp book_data new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}','book_title:nothing','author:nothing','#{mangathank_title}','#{book_data[3]}');") else author_data = book_data[2].to_s.slice(/((?<=\[).*?(?=\]))/) #puts "author_dat:#{author_data}" if author_data != nil author_data.gsub!(/\ x\ /,' ') author_data.sub!(/((?<=[\p{Hiragana}\p{Han}\p{Katakana}])x(?=[\p{Hiragana}\p{Han}\p{Katakana}]))/,' ') author_data.gsub!(/\(|\)/,"\(" =>' ',"\)"=>'') author_data.gsub!(/×/,' ') author_data.gsub!(/\ &/,' ') end if /(\ )/.match(author_data) then #/(\S+$)/.match(author_data) #person = /(?<=['\ '])\S.*$/.match(author_data) #str_array = person.to_s.split str_array = author_data.to_s.split person = str_array.pop else person = author_data.to_s end #print("author_data: " + author_data + " person: " + person) #puts num = book_data[2].to_s.slice(/((?<=第)\d+(?=巻|卷$))/) #num = /((?<=第)\d+(?=巻$))/.match(book_data[0].to_s) #book_data_0 = book_data[0].to_s.sub(/((?=第).*巻)/,'') book_data_0 = book_data[2].to_s.gsub(/((?=第).*(巻|卷))/,'') book_data_0.gsub!(/((?=第).*話)/,'') book_data_0.gsub!(/(.(?<=\()文庫版(?=\)).)/,'') book_data_0.gsub!(/(.(?<=\[)文庫版(?=\]).)/,'') book_data_0.gsub!(/文庫版/,'') book_data_0.gsub!(/(.(?<=\()完(?=\)).)/,'') book_data_0.gsub!(/(.(?<=【).*(?=】).)/,'') book_data_0.gsub!(/(.(?<=\[).+?(?=\]).)/,'') book_data_0.lstrip! book_data_0.rstrip! if book_data_0 == temp_title then if str_array then person = temp_author end else temp_title = book_data_0 temp_author = person end if num != nil then num = num.to_i book_data_0 += ' ' + num.to_s end #puts #puts book_data[0] #puts book_data_0 #puts ndl_search = NdlSearch.new res = ndl_search.get_book_info( book_data_0,person ) onemore = 'true' if res == nil then #puts "res: empty" book_data_0.gsub!(/\'/,"\'\'") puts book_data_0 new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}','#{book_data_0}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' else res.each_with_index do |book,index| #print "book : " if book != "null" then #puts "res:#{book}" #puts "item:#{index}" book.each do |key, val| #puts "#{key}:#{val}" if key == 'totalResults' then #puts #print "no match title name #{person} ",book_data[3],' ' book_data_0.gsub!(/\'/,"\'\'") #puts book_data_0,person,mangathank_title unless str_array.nil? then if str_array.size > 0 then person = str_array.shift puts print "#{person} ?" puts puts onemore = 'true' break else new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}', '#{book_data_0}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' break end else new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}', '#{book_data_0}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' end break end #puts "#{key}:#{val}" if key == 'title' then temp_author = person puts puts "#{key}:#{val}" title = val.to_s.gsub(/\'/, "\'\'") new_db.execute("insert into tbl_bookdata (id, book_title, mangathank_title, ex_id ) values('#{count}', '#{title}','#{mangathank_title}','#{book_data[3]}');") elsif key == 'author' then author = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set author = '#{author}' where id = '#{count}';") elsif key == 'creatorTranscription' then creatortranscription = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set creatortranscription = '#{creatortranscription}' where id = '#{count}';") elsif key == 'volume' then volume = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set volume = '#{volume}' where id = '#{count}';") elsif key == 'link' then url = val new_db.execute("update tbl_bookdata set url = '#{url}' where id = '#{count}';") elsif key == 'publisher' then puts "#{key}:#{val}" publisher = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set publisher = '#{publisher}' where id = '#{count}';") elsif key == 'ISBN' then isbn = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set isbn = '#{isbn}' where id = '#{count}';") elsif key == 'seriesTitle' then seriestitle = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set seriestitle = '#{seriestitle}' where id = '#{count}';") onemore = 'false' else #new_db.execute("update tbl_bookdata set author = '', creatortranscription = '', volume = '', url = '', publisher = '', isbn = '', seriestitle = '' ;") onemore = 'false' next end end else onemore = 'false' puts "error" mangathank_title = book_data[2].to_s.gsub(/\'/, "\'\'") new_db.execute("insert into tbl_bookdata (id, author, mangathank_title, ex_id ) values('#{count}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") end end end sleep 0.3 while onemore == 'true' do onemore = 'false' puts 'in while loop' mangathank_title = book_data[2].to_s.gsub(/\'/, "\'\'") # num = /((?<=第)\d+(?=巻$))/.match(book_data[0].to_s) # book_data_0 = book_data[0].to_s.sub(/((?=第).*巻)/,'') # if num != nil then # num = num.to_s # book_data_0 += ' ' + num.to_i.to_s # end res = ndl_search.get_book_info( book_data_0,person ) #res = ndl_search.get_book_info( book_data[0],person ) if res == nil then #puts "res: empty" new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}','#{book_data[0]}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' else res.each_with_index do |book,index| #print "book : " if book != "null" then #puts "res:#{book}" #puts "item:#{index}" book.each do |key, val| #puts "#{key}:#{val}" if key == 'totalResults' then #puts #puts print "no match title name #{person} ",book_data[3],' ' book_data_0.gsub!(/\'/,"\'\'") #puts book_data_0,person,mangathank_title unless str_array.nil? then if str_array.size > 0 then person = str_array.shift puts print "#{person} ?" puts puts onemore = 'true' break else new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}', '#{book_data_0}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' break end else new_db.execute("insert into tbl_bookdata (id, book_title, author, mangathank_title, ex_id ) values('#{count}', '#{book_data_0}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") onemore = 'false' end break end #puts "#{key}:#{val}" if key == 'title' then temp_author = person puts puts "#{key}:#{val}" title = val.to_s.gsub(/\'/, "\'\'") new_db.execute("insert into tbl_bookdata (id, book_title, mangathank_title, ex_id ) values('#{count}', '#{title}','#{mangathank_title}','#{book_data[3]}');") elsif key == 'author' then author = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set author = '#{author}' where id = '#{count}';") elsif key == 'creatorTranscription' then creatortranscription = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set creatortranscription = '#{creatortranscription}' where id = '#{count}';") elsif key == 'volume' then volume = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set volume = '#{volume}' where id = '#{count}';") elsif key == 'link' then url = val new_db.execute("update tbl_bookdata set url = '#{url}' where id = '#{count}';") elsif key == 'publisher' then publisher = val.to_s.gsub(/\'/, "\'\'") puts "#{key}:#{val}" new_db.execute("update tbl_bookdata set publisher = '#{publisher}' where id = '#{count}';") elsif key == 'ISBN' then isbn = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set isbn = '#{isbn}' where id = '#{count}';") elsif key == 'seriesTitle' then seriestitle = val.to_s.gsub(/\'/, "\'\'") new_db.execute("update tbl_bookdata set seriestitle = '#{seriestitle}' where id = '#{count}';") onemore = 'false' else onemore = 'false' next end end else onemore = 'false' puts "error" new_db.execute("insert into tbl_bookdata (id, author, mangathank_title, ex_id ) values('#{count}','#{author_data}','#{mangathank_title}','#{book_data[3]}');") end end end end end sleep 0.4 end;nil