#!/usr/local/bin/ruby # cachebib.rb # ver.0.5 2002.4.7-2004.01.07 # book-info from/to cache # original; amazon.rb for amazon by not # 書誌情報をcacheとしてISBNをkeyとして読み書きする為の雛型 # 書影画像のdownloadに対応 # 各書店用classに継承させて利用 # get_data_from_id時にISBN-checkのmessageが出る不備 # get_data_from_id(id,isbn)時のCHK-accessのmessageが不備 # 各書店毎にid_check(isbn_check相当)を用意しないと # 以上ToDo if __FILE__ == $0 require 'cache' end #require 'cookie' #require 'webagent' class CacheBib < Cache attr_accessor :isbn, :url_site, :affiliate_code attr_accessor :title, :series, :label, :genre attr_accessor :author, :translator, :publisher attr_accessor :pubcode, :pubdate, :pubsize, :page, :price attr_accessor :index, :desc, :stock, :delivery attr_accessor :image, :image_width, :image_height, :image_local attr_accessor :e_title, :e_author, :e_pubdate attr_accessor :reviews, :review_num, :reviewlist, :flag_review attr_accessor :flag_image, :dir_image, :url_image alias :isbn :code def initialize () super @flag_image = false #true #download-image(書影画像) @dir_image = './' #'../www/' #download-image保存場所 @url_image = './' #download-imageへのアクセス用URL end #===== ===== main ===== ===== def get_data_from_id(id, isbn = nil) @id = id if isbn.nil? return get_data(id, :id) else @hold_keys << 'isbn' return get_data(isbn) end end #===== ===== sub parts ===== ===== def set_cache_id ( isbn, mode = nil ) #mode=nil||:id @isbn = isbn_check(isbn) if !isbn.nil? if mode != :id @cache_id = "isbn" + @isbn.delete('-').downcase else @cache_id = "id" + isbn end @file = @cache_dir + @cache_id + '.txt' @info = {} end #===== ===== get/set info ===== ===== alias url_isbnsearch url_codesearch alias url_bibinfo url_info alias parse_isbnsearch parse_codesearch alias parse_bibinfo parse_info def preset_info #===== preset local-value from @info (from cache) ===== #----- set book-info (from cache) ----- @id = @info["id"] @flag_overprint = true if @info["overprint"] @title = @info["title"] #word_deliveryでのチェックに使用 @delivery = @info["delivery"] #word_deliveryでのチェックに使用 end def get_info #===== get book-info(@info) from url ===== unless @isbn.nil? message("CHK; access; #{@url_site} -> ISBN:#{@isbn}") else message("CHK; access; #{@url_site} -> ID:#{@id}") end #----- get detail-code ----- if !@id agent = WebAgent.new() agent.uri = url_isbnsearch begin agent.get() rescue message("WAR; access-timeout.") return end body = agent.body.kconv(@insystemKcode) parse_isbnsearch(body) end #----- get book-info ----- agent = WebAgent.new() agent.uri = url_bibinfo begin agent.get() rescue message("WAR; access-timeout.") return end body = agent.body.kconv(@insystemKcode) parse_bibinfo(body) #----- get book-image ----- if !@image.nil? && @flag_image # filename = @dir_image + File.basename(@image) isbn = @isbn # ext = File.extname(@image) # for ruby1.7- ext = '.jpg' if @image =~ /(\.+?)$/ ext = $1.strip end filename = @dir_image + 'isbn' + isbn.to_s.delete('-').downcase + ext urlname = @url_image + 'isbn' + isbn.to_s.delete('-').downcase + ext if @isbn.to_s == '' && !@id.nil? filename = @dir_image + 'id' + @id + ext urlname = @url_image + 'id' + @id + ext end unless File.exist?(filename) message("CHK; download-image; #{@image} -> #{filename}") agent = WebAgent.new() agent.uri = @image agent.get() open(filename, 'wb'){ |fp| fp.write(agent.body) fp.flush } @image_local = urlname else @image_local = urlname end end if !@image_local.nil? isbn = @isbn ext = '.jpg' if @image_local =~ /(\.+?)$/ ext = $1.strip end filename = @dir_image + 'isbn' + isbn.to_s.delete('-').downcase + ext if @isbn.to_s == '' && !@id.nil? filename = @dir_image + 'id' + @id + ext end if File.exist?(filename) begin require 'imagesize' rescue LoadError require 'lib/imagesize' end w, h = ImageSize::size(filename) @image_width, @image_height = w, h unless w.zero? end end =begin if !@image_local.nil? && @image_width.nil? #----- re-check image ----- isbn = @isbn ext = '.jpg' if @image_local =~ /(\.+?)$/ ext = $1.strip end filename = @dir_image + 'isbn' + isbn.delete('-').downcase + ext if File.exist?(filename) require 'imagesize' w, h = ImageSize::size(filename) @image_width, @image_height = w, h unless w.zero? end end =end set_info end def set_info #===== set book-info(@info) from local-value ===== super #書籍に特有の情報の追加 @info["isbn"] = @isbn #ISBN @info["code"] = nil #ISBNと重複する ので除けとく @info["title"] = @title #題 @info["series"] = @series #シリーズ @info["label"] = @label #レーベル(XX文庫等) @info["genre"] = @genre #ジャンル @info["author"] = @author #著者 @info["translator"] = @translator #訳者 @info["publisher"] = @publisher #出版社 @info["pubcode"] = @pubcode #出版社内番号 @info["pubdate"] = @pubdate #発行日 @info["pubsize"] = @pubsize #版型 @info["page"] = @page #頁数 @info["index"] = @index #目次 @info["e_title"] = @e_title #洋題 @info["e_author"] = @e_author #原著 @info["e_pubdate"] = @e_pubdate #原発行日 end #===== ===== parts ===== ===== def isbn_check ( isbn ) isbn2 = isbn.strip.delete('-').upcase if isbn2.size < 9 message("WAR; ISBN size error. maybe lost-code.") return nil end if isbn2.size > 13 message("WAR; ISBN size error. maybe over-code.") return nil end if isbn2 !~ /^[\dX]+$/i message("WAR; ISBN code error. maybe No-Num or No-CheckDigit.") return nil end check = 11 0.upto(8){ |i| check = check - (isbn2[i,1].to_i * (10-i)) } check = check % 11 digit = check.to_s digit = 'X' if check == 10 digit = '0' if check == 11 if isbn2.size < 10 #----- check-digit-lost ----- message("WAR; ISBN check-digit lost. -> #{digit}") isbn2 << digit isbn << '-' if (isbn[-1,1] != '-') && (isbn =~ /-/) isbn << digit else #----- check-digit-error ----- check = isbn2[-1,1] if check != digit message("WAR; ISBN check-digit error. #{check} -> #{digit}") isbn2[-1,1] = digit isbn[-1,1] = digit end end return isbn end end =begin #利用例 book = CacheBib.new #実際はCacheBib_amazon.new等各書店用classに book.cache_dir = './' #cache保存位置 book.flag_image = true #画像download指定 book.dir_image = './' #画像保存位置 book.url_image = './' #画像読込指定位置(cache内image_local値) book.expire = 12 #12時間でcacheの再読込, 0で常時再読込、-1で再読込無 #book.expire = -1 #存在するcacheのみ利用で情報を作成 #book.flag_net = false #cacheが存在しない場合もネット接続しない isbn = '4-15-010739-4' info = book.get_data(isbn) #cacheは"isbn4150107394.txt"になる #id = '00487196' #info = book.get_data_from_id(id) #ISBN情報が無くIDのみの場合 # #cacheは"id000487196.txt"になる #info = book.get_data_from_id(id, isbn) #ISBN情報を独自に補足する場合 # #cacheは"isbn4150107394.txt"になる p info =end =begin #既存key(重要項目) (code) =isbn,id呼出に利用するkey isbn ISBN(稀にISBNではない番号の場合も有) id 書店内番号(amazonならasid,bk1ならbibid reviews 書誌情報の配列が格納される。 hold_keys cache更新時も消えない保持項目 overprint cache上書き(一旦消去して再読込でなく)モード指定 #既存key(比較的改変自由) pubcode 出版社内番号 title 題 series シリーズ label レーベル(xx文庫等) author 著者 translator 訳者 publisher 発行元(出版社) pubdate 発行日 pubsize サイズ(A4版等) price 価格 desc 内容紹介等(index=目次を含む時も有) delivery 配達時間 image 書影URL image_width 書影横幅 image_height 書影縦幅 image_local 書影URL(ローカル) e_title 原題 e_author 著者(原語) e_pubdate 発行日(原著の) reviewer 書評者名 =end