Aws4as22ver='$Id: aws4as2.rb,v 1.3 2006/11/04 16:13:23 waka Exp waka $'

require 'iconv'
require 'rubygems'
require 'amazon/ecs'

class String
  def mytoeuc
    begin
      Iconv.conv("EUC-JP", "UTF-8", self)
    rescue
      self
    end
  end

  def mytosjis
    begin
      Iconv.conv("CP932", "UTF-8", self)
    rescue
      self
    end
  end
end

class Aws
  attr_accessor :media
  attr_accessor :product_name
  attr_accessor :third_party_new_price
  attr_accessor :similar_products
  attr_accessor :reviews
  attr_accessor :catalog
  attr_accessor :tracks
  attr_accessor :browse_list
  attr_accessor :used_price
  attr_accessor :total_customer_reviews
  attr_accessor :availability
  attr_accessor :release_date
  attr_accessor :asin
  attr_accessor :isbn
  attr_accessor :jancode

  attr_accessor :lists
  attr_accessor :image_url_medium
  attr_accessor :our_price
  attr_accessor :image_url_small
  attr_accessor :average_customer_rating
  attr_accessor :num_media
  attr_accessor :manufacturer
  attr_accessor :sales_rank
  attr_accessor :image_url_large
  attr_accessor :url
  attr_accessor :list_price
  attr_accessor :artists

  attr_accessor :features
  attr_accessor :collectible_price
  attr_accessor :directors
  attr_accessor :product_description
  attr_accessor :starring

  attr_accessor :platforms
  attr_accessor :mpn

  attr_accessor :kcode
  def initialize(aws_token, associate_id)
    @locale = 'jp'
    @aws_token = aws_token
    @associate_id = associate_id
    @response = Amazon::Ecs::Response.new("")
    @media = ''
    @product_name = ''
    @similar_products = []
    @reviews = ''
    @catalog = ''
    @tracks  = []
    @browse_list = []
    @total_customer_reviews = ''
    @availability = ''
    @release_date = ''
    @asin = ''
    @isbn = ''
    @jancode = ''
    @lists = []

    @our_price = ''
    @list_price = ''
    @used_price = ''
    @third_party_new_price = ''

    @image_url_small = ''
    @image_url_medium = ''
    @image_url_large = ''

    @average_customer_rating = ''
    @num_media= ''
    @manufacturer = ''
    @sales_rank= ''
    @url = ''
    @artists = []

    @features = []
    @collectible_price = ''
    @directors = []
    @product_description = ''
    @starring = []

    @authors = []

    @platforms = []
    @mpn = ''
    @kcode = 'sjis'
  end

  def research(product_code)
    search(product_code)
    @srch_results.each{|product|
      product.instance_variables.each{|val_symbol|
        next if self.instance_variables.include?(val_symbol)
        puts val_symbol
      }
    }
  end

  # JAN コード ==> ISBN 変換
  # Thomas さん
  # http://tmas.s54.xrea.com/diary/200607.html#08_t2
  # 2006-11-5(Sun) waka : chk_sum が 11 の時の考慮もれ修正
  def jan2isbn(jan_code)
    return nil if ! (jan_code.length == 13 && jan_code =~ /^97[89]/)
    isbn_array = jan_code.split('')[3..-2]
    chk_sum = 0
    isbn_array.each_with_index{|char, idx|
      chk_sum += (10 - idx) * char.to_i
    }
    chk_digit = 0
    if chk_sum % 11 > 0
      chk_digit = 11 - (chk_sum % 11)
    elsif chk_sum % 11 == 1
      chk_digit = 'X'
    end
    isbn = isbn_array.push(chk_digit).to_s
    return isbn
  end

  def search(product_code)
    Amazon::Ecs.options = {
      :aWS_access_key_id => [@aws_token],
      :associate_tag => @associate_id ,
      :country => :jp 
    }
    @response = Amazon::Ecs.item_lookup(product_code , { 
      :search_index => 'Blended' ,
      :response_group => 'Large' 
      }
    )
    if @response.has_error? then
      @response = Amazon::Ecs.item_search(product_code , { 
        :search_index => 'Blended' ,
        :response_group => 'Large' 
        }
      )
      if @response.has_error? then
        puts @response.error
      end
    end
  end

  def product_code=(code)
    @product_code = code
  end

  def power_search(product_code)
    search(product_code)
  end

  def as2request
    request = ""
    keys=["media", "product_name", "catalog", "release_date",
         "asin", "isbn", "image_url_medium", "image_url_large",
         "image_url_small", "num_media", "manufacturer", "url",
         "artists", "directors", "authors", "our_price", "list_price",
         "features", "starring", "mpn", "product_description", "jancode",
         "platforms"]
    values=["binding","/itemattributes/title","catalog","releasedate",
           "/asin", "isbn", "/mediumimage/url","/largeimage/url",
           "/smallimage/url","numberofdiscs" , "manufacturer", "detailpageurl", 
           "artist", "director", "author", "price/formattedprice","listprice/formattedprice",
           "features", "starring", "mpn", "editorialreview/content", "jancode",
           "platform"]
    alist=keys.zip(values)
    item = @response.first_item
    alist.each{|key3,key4|
      value = item.get_array(key4)
      next if value.size == 0
      if value.size > 1
        if "euc" == kcode
          value.collect!{|v| v.mytoeuc }
        else
          value.collect!{|v| v.mytosjis }
        end
        request += ["@"+key3, value].join(", ")
      else
        if "euc" == kcode
          request += ["@"+key3, value.to_s.mytoeuc].join(", ")
        else
          request += ["@"+key3, value.to_s.mytosjis].join(", ")
        end
      end
      request += "\n"
    }
    return request
  end
end
