Last active
December 16, 2015 13:09
-
-
Save tomoyamkung/5439826 to your computer and use it in GitHub Desktop.
[Ruby]amazon-ecs を使った商品情報取得サンプル。ResponseGoup は ItemAttributes, SalesRank, OfferSummary を指定。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'amazon/ecs' | |
require 'yaml' | |
require 'nokogiri' | |
class PaapiSample | |
def initialize | |
@config = YAML.load_file('./paapi-sample.yaml') | |
Amazon::Ecs.options = { | |
:associate_tag => @config['associate_tag'], | |
:AWS_access_key_id => @config['AWS_access_key_id'], | |
:AWS_secret_key => @config['AWS_secret_key'] | |
} | |
end | |
def get asin | |
info = Hash::new | |
res = Amazon::Ecs.item_lookup(asin, :response_group => "ItemAttributes, SalesRank, OfferSummary", :country => 'jp') | |
res.items.each do |item| | |
element = item.get_element('ItemAttributes'); | |
info[:asin] = asin | |
info[:title] = element.get('Title') | |
info[:amount] = element.get('ListPrice/Amount') | |
info[:public_date] = element.get('PublicationDate') | |
info[:url] = "http://www.amazon.co.jp/dp/#{asin}?tag=#{@config['associate_tag']}" | |
element = item.get_element('SalesRank'); | |
Nokogiri::XML(element.to_s).xpath('/').each do |node| | |
info[:sales_rank] = node.text | |
end | |
element = item.get_element('OfferSummary'); | |
info[:used_price] = element.get('LowestUsedPrice/Amount') | |
info[:num_of_new] = element.get('TotalNew') | |
info[:num_of_used] = element.get('TotalUsed') | |
end | |
return info | |
end | |
end | |
if __FILE__ == $0 | |
obj = PaapiSample.new | |
asin = "4894712288" | |
asin = ARGV[0] unless ARGV[0] == nil | |
info = obj.get(asin) | |
puts info | |
puts info[:title] | |
puts info[:url] | |
puts info[:sales_rank] | |
puts info[:amount] | |
puts info[:num_of_new] | |
puts info[:used_price] | |
puts info[:num_of_used] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
associate_tag: XXX | |
AWS_access_key_id: YYY | |
AWS_secret_key: ZZZ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment