#!/usr/bin/env ruby # kinokuniya.rb # ver.0.3 2002.4.7-2003.1.20 # ver.0.4 2004.01.06-2004.01.08 新キャッシュに対応 #【絵】illustrator #以上、やり残し if __FILE__ == $0 require 'cookie' require 'webagent' require 'cache' require 'cachebib' end class CacheBib_kinokuniya < CacheBib attr_accessor :title_kana, :limage, :pubtype attr_accessor :nipcd, :author_comment def initialize () super @shop_id = 'kin' @url_site = 'http://bookweb.kinokuniya.co.jp/' end #===== ===== parts ===== ===== def word_delivery () delivery = '' flag = false if !@title.nil? # title-check -> no-info / get-info check flag = true if @delivery.nil? delivery = "ない" else delivery = @delivery if @delivery =~ /4/s delivery = "4日" elsif @delivery =~ /品切れの場合お取り寄せ/s delivery = "取寄?" elsif @delivery =~ /冊数/s #parse-error delivery = "取寄" else delivery = '?' end end else message("WAR; maybe data-error, no-title.") end return delivery, flag end #===== ===== url ===== ===== def url_isbnsearch ( isbn = @isbn ) url_bibinfo(isbn) end def url_bibinfo ( id = @id ) id = @isbn if id.nil? id = id.delete('-').upcase url = @url_site + "guest/cgi-bin/wshosea.cgi?W-ISBN=#{id}" return url end #===== ===== parse ===== ===== def parse_bibinfo ( str ) #----- set key and data ------ mode_tbl = { :title => '和書籍データベース検索結果', # +title_kana,series :e_title => '\[原書名:', # +e_author :image => '', :isbn => 'ISBN:', :main => '
', # publisher,pubdate # +pubsize,page # +pubtype,genre,price # +stock, delivery :stock => '
', # =nipcd :desc => '', :none => 'お買い物のしかた', :author_comment => '\[BOOK著者紹介情報\]' } mode = :none data = Hash.new() #----- read and sepalate by key(mode_tbl) ------ str.each{|line| l = line.chomp.chomp("\r").kconv(@insystemKcode) mode_tbl.each{ |key,keydata| mode = key if /#{keydata}/s =~ l } if mode != :none if data[mode] data[mode] << l else data[mode] = l end end } #----- parse ----- data_title = nil if data[:title] =~ /<\/table>/ data_title = $'.strip end if data_title =~ /([^>]+?)<\/small>/m data_title, @title_kana = $'.strip, $1.strip @title_kana = @title_kana.gsub(/\s+/s, ' ').to_s.strip if @title_kana =~ /^\((.+)\)$/ @title_kana = $1.strip end end if data_title =~ /(.+?)([^>]+?)<\/b>/m series, @title = $1.strip, $2.strip series.gsub!(/<[^>]+>/,'').strip! @series = series if @seires.to_s != '' end if data[:e_title] =~ /([^>]+?)<\/b>/ @e_title = $1.strip if @e_title =~ /(.+?)〈(.+?)〉/ @e_title, @e_author = $1.strip, $2.strip end end if data[:image] =~ /]+>/,'').strip end if data[:isbn] =~ /ISBN:(.+?)
/ @isbn = $1.strip end if data[:main] =~ /
/ data_name = $& data_name.scan(//) { |str| name = str.gsub(/<[^>]+>/,'').strip if str =~ /【訳】/ if @translator.nil? then @translator = '' else @translator << "\t" end @translator << name.chomp('【訳】') else if @author.nil? then @author = '' else @author << "\t" end @author << name.split(/【著】/s).join # @author << name.chomp('【著】') end } end #【絵】illustratorも存在する if !data[:main].nil? data[:main].split("
").each { |word| if word =~ /出版/s str = word.gsub(/<[^>]+>/,'').strip @publisher = str.gsub(/\s+/,' ').strip if str =~ /\d/ @publisher = $`.gsub(/\s+/,' ').strip.chomp('(').strip end if str =~ /[\d\/\-]+/ @pubdate = $&.gsub(/\s+/,' ').strip end elsif word =~ /(p|cm)/s && word !~ /【著】/s @pubsize = word.gsub(/<[^>]+>/,'').strip pub_str = @pubsize.split(/ |\s+/s) pub_str.each{ |word2| if word2 =~ /p/s @page = word2.strip end if (word2 =~ /cm/s) || (word2 =~ /cm/) @pubsize = word2.strip end } elsif word =~ /\[.+?\(税別\)<\/b>/s data_info = $& if data_info =~ /\[(.+?)\].+?(.+?)<\/b>.+?(.+?)<\/b>/ @pubtype, @genre, @price = $1.strip, $2.strip, $3.strip end end } if data[:main] =~ /color=\"green\">([^<]+)/i @stock = $1.strip if @stock =~ /(.+。)(.+?。)/s @stock, @delivery = $1.strip, $2.strip end end # @etc = data[:main].gsub(/
.+?$/mi,'').gsub(/<[^>]+>/,'').strip.gsub(/\s+/,' ') end if data[:stock] =~ // @nipcd = $1.strip end =begin if data[:stock] =~ /<\/form>/ data_stock = $` if data_stock =~ /.+?/m data_stock = $& end stock = data_stock.gsub(/<[^>]+>/,'').strip if stock != '' @stock = stock @delivery = @stock if @stock =~ /。/ @stock = ($` + $&).strip @delivery = $'.strip end end end =end if !data[:desc].nil? desc = [] data[:desc].scan(//mi){ desc << $&.gsub(/\s+/,' ').gsub(/
/,"\t").gsub(/<[^>]+>/,'').strip } desc = desc.join("\t").strip @desc = desc if desc != '' end # if data[:content] =~ /.+?<\/td>/ # @desc = $&.strip.gsub(/
/,"\t").gsub(/<[^>]+>/,'').strip # end if data[:author_comment] =~ /#{mode_tbl[:author_comment]}(.+?)<\/td>/ @author_intro = $1.gsub(/\s+/,' ').gsub(/
/,"\t").gsub(/

/,"\t").gsub(/<[^>]+>/,'').strip end end #===== ===== get/set info ===== ===== def preset_info #===== set local-value from @info (from cache) ===== super @id = @isbn.delete('-').upcase #@info["id"] end def set_info #===== set book-info(@info) from local-value ===== super #----- set info add ----- #紀伊国屋独自の情報類 @info["title_kana"] = @title_kana #題フリガナ @info["limage"] = @limage #大画像 @info["pubtype"] = @pubtype #判型 @info["nipcd"] = @nipcd #NIPCD @info["author_intro"] = @author_intro #著者紹介情報 end end if __FILE__ == $0 p CacheBib_kinokuniya.new.get_data('4-15-010739-4') # p CacheBib_kinokuniya.new.get_data('4344004299') end