#!/usr/bin/env ruby # ippo_cache.rb # ver.0.1 2002.3.21-2004.01.06 # book-infoをcacheとしてlocal保持する贋shop-siteからのcache-download # ISBNよりbook-infoを引く # ※cookie,webagent,cache,cachebibは必須 # ※他の各書店ユニットは # 有れば上書きで贋トライ→失敗したら本家巡回に # 無ければ新規で贋トライのみに # まとめ落とし時の順序・完動チェックまだ # →まとめ鯖→個々で鯖→個々で書店→手持ちキャッシュ読みのみで対処→ # →|ここで終わってしまってる? かも? # 以上ToDo if __FILE__ == $0 require 'cookie' require 'webagent' require 'cache' require 'cachebib' require 'amazon' require 'bk1' require 'rakuten' end module CacheBib_ippo #===== ===== url ===== ===== def url_cacheserver #moduleに定数を持たせる簡便な方法はあるのか? return 'http://ippo.itbdns.com/cgi/cache/bookcache.cgi' end def url_bibinfo_ippo () url = url_cacheserver + '?' url << "shop=#{@shop_id};" unless @isbn.to_s == '' url << "isbn=#{@isbn}" else url << "id=#{@id}" end # url << ";logoff" #テスト用等 return url end #===== ===== parse ===== ===== def parse_bibinfo_ippo ( str ) if str =~ /\A\s/ || str.to_s.strip =~ /^ #{mes_code}") #----- get book-info ----- agent = WebAgent.new() agent.uri = url_bibinfo_ippo begin agent.get() rescue message("WAR; access-timeout. (cache-server)") raise end body = agent.body.kconv(@insystemKcode) parse_bibinfo_ippo(body) preset_info rescue begin message("WAR; out-cache-server. get_info_orig.") get_info_orig message("WAR; CHK; get_info_orig.") rescue end end end # alias :get_info_orig :get_info if defined?(get_info) # alias :get_info :get_info_ippo # ここでのaliasは効いていない? end class CacheBib_ippo_parts #moduleの関数を直接指定する方法はないのか? include CacheBib_ippo #一旦ダミーのclassに入れてから読んでる訳だが end if defined?(CacheBib_amazon) class CacheBib_amazon include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_amazon < CacheBib def initialize; super; @shop_id = 'amz'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_bk1) class CacheBib_bk1 include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_bk1 < CacheBib def initialize; super; @shop_id = 'bk1'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_rakuten) class CacheBib_rakuten include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_rakuten < CacheBib def initialize; super; @shop_id = 'rak'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_yahoo) class CacheBib_yahoo include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_yahoo < CacheBib def initialize; super; @shop_id = 'yho'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_asahiya) class CacheBib_asahiya include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_asahiya < CacheBib def initialize; super; @shop_id = 'ash'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_jbook) class CacheBib_jbook include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_jbook < CacheBib def initialize; super; @shop_id = 'jbk'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_kinokuniya) class CacheBib_kinokuniya include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_kinokuniya < CacheBib def initialize; super; @shop_id = 'kin'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_skysoft) class CacheBib_skysoft include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_skysoft < CacheBib def initialize; super; @shop_id = 'sky'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_esbooks) class CacheBib_esbooks include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_esbooks < CacheBib def initialize; super; @shop_id = 'esb'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_webcatplus) class CacheBib_webcatplus include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_webcatplus < CacheBib def initialize; super; @shop_id = 'wcp'; end include CacheBib_ippo alias :get_info :get_info_ippo end end if defined?(CacheBib_webcatplus) class CacheBib_webcatplus include CacheBib_ippo alias :get_info_orig :get_info alias :get_info :get_info_ippo end else class CacheBib_webcatplus < CacheBib def initialize; super; @shop_id = 'wcp'; end include CacheBib_ippo alias :get_info :get_info_ippo end end =begin CacheBib_amazon_com CacheBib_a1books CacheBib_powells CacheBib_maruzen CacheBib_amazon_uk CacheBib_bookshop CacheBib_boople CacheBib_rcom CacheBib_reviewlink =end #cacheserverからのまとめ落とし用 #cache_ippo.rb内で無く、affiliates, flag_reviews等を加えた上で一般化し、 #cache_round.rbとかしてもよかったが、一段深くなって見通しが悪くなるのを嫌い、 #まとめ落としの時のみ起動とした #(nDiaryでは1filter/plugin内で二度もCacheBibが立つ事になる) #※「message」が欲しかったのでCacheを継承。不具合あるかな??? class CacheBib_ippo_sum < Cache include CacheBib_ippo attr_accessor :isbn, :expire, :flag_net attr_accessor :flag_review #, :flag_bk1_image, :url_bk1_image attr_accessor :shoplist, :cache_dir, :affiliate alias :initialize_cache :initialize def initialize( isbn = nil, shoplist = [], cache_dir = {} ) initialize_cache @isbn = isbn @shoplist = shoplist @cache_dir = cache_dir @flag_review = false # @flag_bk1_image = nil @flag_net = true end def get checklist download end =begin def makelist ( shoplist = [] ) shoplist.each{ |shop| book = nil begin case shop when 'amz', 'amazon' book = CacheBib_amazon.new() when 'bk1' book = CacheBib_bk1.new() when 'rak', 'rakuten' book = CacheBib_rakuten.new() when 'yho', 'yahoo' book = CacheBib_yahoo.new() when 'ash', 'asahiya' book = CacheBib_asahiya.new() when 'jbk', 'jbook' book = CacheBib_jbook.new() when 'kin', 'kinokuniya' book = CacheBib_kinokuniya.new() when 'sky', 'skysoft' book = CacheBib_skysoft.new() when 'esb', 'esbooks' book = CacheBib_esbooks.new() when 'wcp', 'webcatplus' book = CacheBib_webcatplus.new() when 'amc','amazon_com' book = CacheBib_amazon_com.new() when 'a1b', 'a1books' book = CacheBib_a1books.new() when 'pwl', 'powells' book = CacheBib_powells.new() when 'mrz', 'maruzen' book = CacheBib_maruzen.new() when 'amu', 'amazon_uk' book = CacheBib_amazon_uk.new() when 'bsp', 'bookshop' book = CacheBib_bookshop.new() when 'bpl', 'boople' book = CacheBib_boople.new() when 'rcm', 'rcom' book = CacheBib_rcom.new() when 'rlk', 'reviewlink' book = CacheBib_reviewlink.new() when '', nil next else message("WAR; unknown-bookstore; code:#{shop}") next end rescue Exception message("WAR; define-error; code:#{shop}") next end # book.affiliate_code = affiliate[shop] unless affiliate[shop].empty? book.flag_review = @flag_review book.flag_net = @flag_net @expire = (@expire - 1) if (@expire.to_i - 1) > 0 #1時間分先読み book.expire = @expire unless @expire.nil? book.cache_dir = cache_dir[shop] unless cache_dir[shop].nil? book.set_cache_id(isbn) cachebibs[book.shop_id] = book downloads << book.shop_id if book.expire_over? } end =end def checklist ( isbn = @isbn, shoplist = @shoplist, cache_dir = @cache_dir ) return if isbn.nil? cachebibs, downloads = {}, [] #-----全書店分cachebibの作成----- # cachebibs = makelist if @cachebibs.nil? shoplist.each{ |shop| book = nil begin case shop when 'amz', 'amazon' book = CacheBib_amazon.new() when 'bk1' book = CacheBib_bk1.new() when 'rak', 'rakuten' book = CacheBib_rakuten.new() when 'yho', 'yahoo' book = CacheBib_yahoo.new() when 'ash', 'asahiya' book = CacheBib_asahiya.new() when 'jbk', 'jbook' book = CacheBib_jbook.new() when 'kin', 'kinokuniya' book = CacheBib_kinokuniya.new() when 'sky', 'skysoft' book = CacheBib_skysoft.new() when 'esb', 'esbooks' book = CacheBib_esbooks.new() when 'wcp', 'webcatplus' book = CacheBib_webcatplus.new() when 'amc','amazon_com' book = CacheBib_amazon_com.new() when 'a1b', 'a1books' book = CacheBib_a1books.new() when 'pwl', 'powells' book = CacheBib_powells.new() when 'mrz', 'maruzen' book = CacheBib_maruzen.new() when 'amu', 'amazon_uk' book = CacheBib_amazon_uk.new() when 'bsp', 'bookshop' book = CacheBib_bookshop.new() when 'bpl', 'boople' book = CacheBib_boople.new() when 'rcm', 'rcom' book = CacheBib_rcom.new() when 'rlk', 'reviewlink' book = CacheBib_reviewlink.new() when '', nil next else message("WAR; unknown-bookstore; code:#{shop}") next end rescue Exception message("WAR; define-error; code:#{shop}") next end # book.affiliate_code = affiliate[shop] unless affiliate[shop].empty? book.flag_review = @flag_review book.flag_net = @flag_net @expire = (@expire - 1) if (@expire.to_i - 1) > 0 #1時間分先読み book.expire = @expire unless @expire.nil? book.cache_dir = cache_dir[shop] unless cache_dir[shop].nil? book.set_cache_id(isbn) cachebibs[book.shop_id] = book downloads << book.shop_id if book.expire_over? } @cachebibs, @downloads = cachebibs, downloads return cachebibs, downloads end def download ( isbn = @isbn, cachebibs = @cachebibs, downloads = @downloads ) return unless @flag_net return if isbn.nil? return if downloads.size == 0 url = url_cacheserver + '?' url << "isbn=#{isbn};" shops = [] downloads.each{ |shop| url << "shop=#{shop};" shops << shop } # url << ";logoff" #テスト用等 message("CHK; access; #{url_cacheserver} #{shops.join(',')} -> ISBN:#{isbn}") agent = WebAgent.new agent.uri = url begin agent.get rescue message("WAR; access-timeout.") return end files = agent.body.split(/\n\n\n/) if files.first[0,1] =~ /\s/ || files.first.to_s.strip =~ /^ ISBN:#{isbn}") agent = WebAgent.new agent.uri = url begin agent.get rescue message("WAR; access-timeout. (gets:cache-server)") return end files = agent.body.split(/\n\n\n/) if files.first =~ /\A\s/ || files.first.to_s.strip =~ /^