# filter/sum_price_x.rb 2004/01/08 新キャッシュ用 =begin (filter/)sum_price_x.rb nDiary Filter 家計簿的数値の作成 金額を書き込んだ.priceファイルを生成します。 plugin/sum_price_x.rbとあわせて利用、ないし別の集計プログラムと利用。 format: .priseファイルは1行1件、TAB区切り date, anchor, title, type, code, price 8桁日付, 日記アンカー, 品名, 品型(book,cd等), 型番(ISBN等), 価格 以上で十分だと思うのですが。 =end class Filter BOOKSTORE = Regexp::compile(/[\((]ISBN(\d-[\d-]{9}-\w)[\))]/) unless defined?(BOOKSTORE) BOOKSTOREX = Regexp::compile(/[\((]ISBN[:|:]?\s?(\d-[\d-]{9}[-]?[\w]?)[\))]/) unless defined?(BOOKSTOREX) STORE = Regexp::compile(/[\((]SHOP[:|:]?\s?(.+?),\s?(.+?)[\))]/) unless defined?(STORE) #BOOKSTOREはbookstoreの上位コンパチの正規表現のはず。 AMAZON = Regexp::compile(/[\((]amz:(\d-[\d-]{9}-\w)[\))]/) PRICETAG = Regexp::compile(//) #ex) def sum_price_x ( str, type ) return unless @diary.kind_of?(PastDiary) unless @flag_initial_sum_price_x #initialize #-----インスタンス変数(@vaule)を多用してるので、----- #-----以下はbookstore_x等と重複・上書きされる設定有。注意。----- @flag_initial_sum_price_x = true @cache_base_dir = @diary.config['CACHE_DIRECTORY'] || @diary.logDirectory.chomp('/') + '/bookcache' @cache_base_dir.chomp!('/') @expire = @diary.config['CACHE_EXPIRE'] || 12 @flag_net = @diary.config['nonet'] ? false : true @filename = @diary.logDirectory + @diary.targetMonth + '.price' File.delete(@filename) if File.exist?(@filename) @flag_tag = true @flag_bookstore_x = true #カウント対象をbookstore_xに @flag_amazon = false #カウント対象をamazonに、未テスト @flag_bookstore = false #カウント対象をbookstoreに、未実装 @shop_list = @diary.config['BOOKSTORE_SHOP_LIST'].to_s unless defined?(@shop_list) @shop_list0 = @diary.config['BOOKSTORE_SHOP_LIST_0'].to_s unless defined?(@shop_list0) @shop_list1 = @diary.config['BOOKSTORE_SHOP_LIST_1'].to_s unless defined?(@shop_list1) @shop_list = 'amazon bk1 yahoo asahiya jbook kinokuniya rakuten skysoft' if @shop_list.empty? @shop_list0 = 'amazon amazon.com skysoft a1books powells kinokuniya maruzen' if @shop_list0.empty? @shop_list1 = 'amazon_uk bookshop' if @shop_list1.empty? @flag_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGE'] @url_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGEURL'] @user_lib_dir = @diary.config['USER_LIB_DIRECTORY'].to_s @user_lib_dir = '../user/lib' if @user_lib_dir.to_s == '' @user_lib_dir.chomp!('/') @flag_cacheserver = @diary.config['CACHE_SERVER'] #true if @flag_bookstore_x #-----各位の環境に合わせて適時改変-----ココカラ----- require "#{@user_lib_dir}/cookie" #必須・webagent用 require "#{@user_lib_dir}/webagent" #必須・webagent用 require "#{@user_lib_dir}/cache" #必須・キャッシュ用 require "#{@user_lib_dir}/cachebib" #必須・キャッシュ用 require "#{@user_lib_dir}/amazon" #以下は各書店用・不要書店分は削除可 require "#{@user_lib_dir}/bk1" require "#{@user_lib_dir}/yahoo" #eS! Booksとの提携 require "#{@user_lib_dir}/asahiya" require "#{@user_lib_dir}/jbook" require "#{@user_lib_dir}/kinokuniya" require "#{@user_lib_dir}/rakuten" require "#{@user_lib_dir}/skysoft" # require "#{@user_lib_dir}/amazon_com" #?未 # require "#{@user_lib_dir}/a1books" #?未 # require "#{@user_lib_dir}/powells" #?未 # require "#{@user_lib_dir}/maruzen" #?未 # require "#{@user_lib_dir}/amazon_co_uk" #?未 # require "#{@user_lib_dir}/bookshop" #?未 require "#{@user_lib_dir}/esbooks" # require "#{@user_lib_dir}/boople" #?未 # require "#{@user_lib_dir}/webcatplus" # require "#{@user_lib_dir}/rcom" #?未 require "#{@user_lib_dir}/cache_ippo" if @flag_cacheserver #贋書店(cache-server)への接続 require "#{@user_lib_dir}/cachebib_simple" #未設定書店はbookstoreに準じる # require '../user/lib/rl' #このファイルの下部に実装。分けるべきか? # require 'filter/amazon' #amazonカウント時 #-----書誌情報の保存フォルダの指定----- @cache_dir = {} if @cache_dir.nil? @cache_dir['amazon'] = "#{@cache_base_dir}/amz" @cache_dir['bk1'] = "#{@cache_base_dir}/bk1" @cache_dir['yahoo'] = "#{@cache_base_dir}/yho" @cache_dir['asahiya'] = "#{@cache_base_dir}/ash" @cache_dir['jbook'] = "#{@cache_base_dir}/jbk" @cache_dir['kinokuniya'] = "#{@cache_base_dir}/kin" @cache_dir['rakuten'] = "#{@cache_base_dir}/rak" @cache_dir['skysoft'] = "#{@cache_base_dir}/sky" @cache_dir['amazon_com'] = "#{@cache_base_dir}/amc" @cache_dir['a1books'] = "#{@cache_base_dir}/a1b" @cache_dir['powells'] = "#{@cache_base_dir}/pwl" @cache_dir['maruzen'] = "#{@cache_base_dir}/mrz" @cache_dir['amazon_uk'] = "#{@cache_base_dir}/amu" @cache_dir['bookshop'] = "#{@cache_base_dir}/bsp" @cache_dir['esbooks'] = "#{@cache_base_dir}/esb" @cache_dir['boople'] = "#{@cache_base_dir}/bpl" @cache_dir['webcatplus'] = "#{@cache_base_dir}/wcp" @cache_dir['rcom'] = "#{@cache_base_dir}/rcm" #-----各位の環境に合わせて適時改変-----ココマデ----- end end filename = @diary.logDirectory + @diary.targetMonth + '.price' if filename != @filename @filename = filename File.delete(@filename) if File.exist?(@filename) end case type when :P, :DL, :UL list = [] #-----tag search----- str.scan(PRICETAG){ str = $1 title, type, code, price = '', '', '', '' str.split(/:/).each{ |word| tag, val = word.split(/=/) case tag when 'title' title = val when 'type' type = val when 'code' code = val when 'price' price = val end price = tag if tag =~ /^\\|¥/ price = price.gsub(/[\\|¥|,|円|(税抜)]/,'').strip } list << [title, type, code, price] } if @flag_tag #-----ISBN search (for bookstore.rb)----- str.scan(BOOKSTORE){ isbn = $1 #chache -> price ? title, type, price = '', 'book', '' list << [title, type, isbn, price] } if @flag_bookstore #-----ISBN search (for bookstore_x.rb)----- checkbook = CacheBib.new() str.scan(BOOKSTOREX){ isbn1 = $1 isbn2 = isbn1.delete('-') newisbn = checkbook.isbn_check(isbn1) isbn1 = newisbn info = {} @bookstore_list = @shop_list.split(/\s+/) @bookstore_list = @shop_list0.split(/\s+/) if isbn2 =~ /^0/ @bookstore_list = @shop_list1.split(/\s+/) if isbn2 =~ /^1/ #-----書店の準備----- cachebibs = [] #-----全書店分cachebibの作成----- # downloadlist = [] @bookstore_list.each_index{ |i| bookstore = @bookstore_list[i] book = nil begin case bookstore when 'amazon' book = CacheBib_amazon.new() when 'bk1' book = CacheBib_bk1.new() book.flag_review = false #ここを入れないと重くなる(書評読) unless @flag_bk1_image.nil? book.flag_image = true book.dir_image = @flag_bk1_image book.url_image = @url_bk1_image unless @url_bk1_image.nil? end when 'yahoo' book = CacheBib_yahoo.new() when 'asahiya' book = CacheBib_asahiya.new() when 'jbook' book = CacheBib_jbook.new() when 'kinokuniya' book = CacheBib_kinokuniya.new() when 'rakuten' book = CacheBib_rakuten.new() when 'skysoft' book = CacheBib_skysoft.new() when 'amazon_com' book = CacheBib_amazon_com.new() when 'a1books' book = CacheBib_a1books.new() when 'powells' book = CacheBib_powells.new() when 'maruzen' book = CacheBib_maruzen.new() when 'amazon_uk' book = CacheBib_amazon_uk.new() when 'bookshop' book = CacheBib_bookshop.new() when 'esbooks' book = CacheBib_esbooks.new() when 'boople' book = CacheBib_boople.new() when 'webcatplus' book = CacheBib_webcatplus.new() when 'rcom' book = CacheBib_rcom.new() when 'reviewlink' next else $stderr.puts "WAR; unknown-bookstore; code:#{bookstore}" next end rescue Exception $stderr.puts "WAR; access-error; #{isbn1} - #{@bookstore_name[bookstore]}" next end book.expire = @expire flag_net = @flag_net #ネットへの接続を制限するなら例えば以下の様に。 #flag_net = false if bookstore == 'amazon' book.flag_net = flag_net book.cache_dir = @cache_dir[bookstore] # book.set_cache_id(isbn1) cachebibs << book # downloadlist << i if book.expire_over? } #-----書店にネット接続/キャッシュ読----- if @flag_cacheserver #-----cache鯖先読----- shoplist, cachedir = [], {} cachebibs.each{ |book| shoplist << book.shop_id cachedir[book.shop_id] = book.cache_dir } CacheBib_ippo_sum.new(isbn1, shoplist, cachedir).get end infolist = [] #-----通常のキャッシュ読----- @bookstore_list.each{ |bookstore| infolist << nil } #書店情報リスト threads = [] #thread化 cachebibs.each_index{ |i| thr = Thread.start(i){ |j| #thread化 # j = i #threadでない場合、この行を戻して「thread化」行を落とす。 #threadでは順番が先着順でstderr表示等が乱れるかもしれない。 book = cachebibs[j] info = book.get_data(isbn1) infolist[j] = info } #thread化 threads << thr #thread化 } #幾つか必要そうで標準添付でないwordをinfoに加えている threads.each{ |thr| thr.join } #thread化 infolist.delete(nil) #書店情報リストの統合 info = checkbook.merge_info( infolist ) title, type, isbn, price = info['title'], 'book', isbn1, info['price'] price = price.to_s.gsub(/[\\|¥|,|円|(税抜)]/,'').strip list << [title, type, isbn, price] } if @flag_bookstore_x str.gsub!(STORE){ bookstore, id = $1.strip, $2.strip book = nil begin case bookstore when 'amazon' book = CacheBib_amazon.new() book.affiliate_code = @amazon_id unless @amazon_id.empty? when 'bk1' book = CacheBib_bk1.new() book.affiliate_code = @bk1_id unless @bk1_id.empty? book.flag_review = false #ここを入れないと重くなる(書評読) unless @flag_bk1_image.nil? book.flag_image = true book.dir_image = @flag_bk1_image book.url_image = @url_bk1_image unless @url_bk1_image.nil? end when 'yahoo' book = CacheBib_yahoo.new() when 'asahiya' book = CacheBib_asahiya.new() when 'jbook' book = CacheBib_jbook.new() when 'kinokuniya' book = CacheBib_kinokuniya.new() when 'rakuten' book = CacheBib_rakuten.new() book.affiliate_code = @rakuten_id unless @rakuten_id.empty? when 'skysoft' book = CacheBib_skysoft.new() when 'amazon_com' book = CacheBib_amazon_com.new() when 'a1books' book = CacheBib_a1books.new() when 'powells' book = CacheBib_powells.new() when 'maruzen' book = CacheBib_maruzen.new() when 'amazon_uk' book = CacheBib_amazon_uk.new() when 'bookshop' book = CacheBib_bookshop.new() when 'esbooks' book = CacheBib_esbooks.new() when 'boople' book = CacheBib_boople.new() when 'webcatplus' book = CacheBib_webcatplus.new() when 'rcom' book = CacheBib_rcom.new() when 'reviewlink' next else $stderr.puts "WAR; unknown-bookstore; code:#{bookstore}" next end rescue Exception $stderr.puts "WAR; define-error; #{@bookstore_name[bookstore]}" next end book.expire = @expire book.flag_net = @flag_net book.cache_dir = @cache_dir[bookstore] book.get_data_from_id(id) info = book.info title, type, isbn, price = info['title'], 'book', "#{book.shop_id}:#{id}", info['price'] price = price.to_s.gsub(/[\\|¥|,|円|(税抜)]/,'').strip list << [title, type, isbn, price] } if @flag_bookstore_x #-----ASIN search (for amazon.rb)----- str.scan(AMAZON){ isbn = $1 amz = Amazon::new amz.cache_dir = @diary.logDirectory + 'amzcache' amz.isbn = isbn if @diary.respond_to?('config') then amz.expire = @diary.config['AMAZON_EXPIRE'] || 7 amz.proxy = @diary.config['PROXY'] end title, type, price = amz.title, 'book', amz.price price = price.gsub(/[\\|¥|,|円|(税抜)]/,'').strip list << [title, type, isbn, price] } if @flag_amazon #-----list----- unless list.empty? anchor = '' if @diary.paragraphAnchor if @diary.anchorEachTopic anchor = "_t#{@diary.instance_eval{@cntTopic}}_#{@diary.instance_eval{@cntParagraph + 1}}" else anchor = "_#{@diary.instance_eval{@cntParagraph + 1}}" end end anchor = "#{@diary.date2monthlyfilelink(@diary.date, true)}#{anchor}" open(@filename, 'a'){ |fp| list.each{ |line| # line.unshift(anchor) # line.unshift(@diary.date) # fp.puts line.join("\t") fp.puts [@diary.date, anchor, *line].join("\t") #出力フォーマットはdate,anchor,title,type,code,priceのTAB区切り } } end end end end