# $Id: sum_price.rb,v 1.5 2003-07-12 02:38:25+09 waka Exp waka $ # http://www.double-red.net/ =begin sum_price.rb nDiary Plug-in amazon.rb、amazondvd.rb で取得した商品の合計を算出し表示する example: =end class Numeric def c_sep digit = Array.new if self.zero? then return self end if self < 0 then target = self.abs minus = "-" else target = self minus = "" end mod = target.to_s.length.modulo(3) if mod.nonzero? then digit << target.to_s[0..mod-1] << target.to_s[mod..-1].scan(/\d{3}/) else digit = target.to_s.scan(/\d{3}/) end if target < 1000 then digit = minus + digit.to_s else digit = minus + digit.join(",") end return digit end end def plug(opt) dirname = ['amzcache','amzcacheDVD'] i = 0 prices = Array.new dirname.each do |f| # cache_dir = opt['dir'] + f cache_dir = @logDirectory + f price_data = Struct.new("Price", :media, :price, :id) begin dir = Dir.open(cache_dir) media = price = id ="" dir.each do |x| cache = File.join(cache_dir,x) id = File.basename(x) if !File.directory?(cache) then cacheF = File.open(cache) while line = cacheF.gets # キャッシュファイルのフィールド 6 番目(amazon,amazondvd共通) price = "" price = line.split("\t")[5] # キャッシュファイルのフィールド 12 番目(amazondvd) media = line.split("\t")[11] price = price.gsub(/[^0-9]/,"").to_i end prices[i] = price_data.new(media, price, id) i += 1 end end dir.close rescue print "Warning...\n #$!\n" end end total = dvd = cd = book = game = pcgame = Array.new(2,0) prices.each {|p| if /ゲーム機本体|Xbox|GAMECUBE|PlayStation/i =~ p.media then game = [p.price.to_i + game[0] , game[1] + 1] elsif /DVD/ =~ p.media then dvd = [p.price.to_i + dvd[0] , dvd[1] + 1] elsif /CD/ =~ p.media then cd = [p.price.to_i + cd[0] , cd[1] + 1] elsif /PC-GAME/i =~ p.media then pcgame = [p.price.to_i + pcgame[0] , pcgame[1] + 1] else book = [p.price.to_i + book[0] , book[1] + 1] end } [0,1].each do |i| eval("total[i] = dvd[i] + cd[i] + book[i] + game[i] + pcgame[i]") end type_data = Array.new a = ["dvd","cd","game","pcgame","book"] a.each_index do |i| m = "#{a[i]}" # メディア p = "#{a[i]}[0]" # 価格合計 c = "#{a[i]}[1]" # 数量 unless eval(p).zero? type_data << "[" + m.upcase type_data << "\\" + eval(p).c_sep.to_s type_data << "(" + eval(c).c_sep.to_s + ")]" end end str = "" unless total[0].zero? str << "" end return str end =begin Local Variables: mode: ruby coding: sjis-dos End: =end