Amazon Web Service を Ruby にて操作


「.amazonrc」の内容
Amazon Web Service(http://aws.amazon.com/)にアカウント登録すれば、key_idやsecret_key_idなどの情報を取得できます。

key_id = 'XXXXX'
secret_key_id = 'XXXXX'
associate = 'XXXXX'
cache = false
locale = 'jp'
encoding = 'utf-8'

書籍検索サンプルコード

#!/usr/local/bin/ruby -Ku
# -*- coding: utf-8 -*-

# デフォルトだと設定ファイルは「/etc/amazonrc」「~/.amazonrc」などを読みにいく
# 今回はカレントの「.amazonrc」を読みにいくよう設定
ENV['AMAZONRCDIR'] = './'
ENV['AMAZONRCFILE'] = '.amazonrc'

require 'amazon/aws'
require 'amazon/aws/search'

req = Amazon::AWS::Search::Request.new()

#------------------------------------------------------------
# 色々な条件にて、ItemSearch を使用してみたよ
#------------------------------------------------------------
is = Amazon::AWS::ItemSearch.new( 'Books', {'Title' => 'ruby'} )
#is = Amazon::AWS::ItemSearch.new( 'Books', {'Author' => 'るびきち'} )
#is = Amazon::AWS::ItemSearch.new( 'Books', {'Title' => '逆引き', 'Author' => 'るびきち'} )

rg = Amazon::AWS::ResponseGroup.new( 'Large' )
page = req.search( is, rg )

#------------------------------------------------------------
# 1ページ分表示
#------------------------------------------------------------
puts "search count : #{page.item_search_response.items.total_results}"

page.item_search_response.items.item.each do |i|
  puts "title = #{i.item_attributes.title}"
  puts "publisher = #{i.item_attributes.publisher}"
  author = i.item_attributes.author
  author.each { |k, v| puts "author = #{k}" } if not author.nil?
  puts ""
end

#------------------------------------------------------------
# ページごと表示
#------------------------------------------------------------
total_page = page.item_search_response.items.total_pages.to_i
puts "page count : #{total_page}"

if total_page > 1 then
  page_cnt = 0
  page = req.search( is, rg, total_page )  # 1〜指定ページまで読み込み
  page.each do |p|
    puts "----------------------------------------"
    puts "#{page_cnt+=1}ページ"
    puts "----------------------------------------"
    p.item_search_response.items.item.each do |i|
      i.item_attributes.to_h.each do |k, v|
        if Amazon::AWS::AWSArray === v
          puts "#{k} = #{v.join("  /  ")}" if not v.nil?
        else
          puts "#{k} = #{v}" if not v.nil?
        end
      end
      puts ""
    end
  end
end

書籍検索サンプルコード(一気に全件取得)

#!/usr/local/bin/ruby -Ku
# -*- coding: utf-8 -*-

#--------------------------------------------------------------------------------
# Amazon Web Service / 書籍名にて検索(全件数を一気に取得)
#--------------------------------------------------------------------------------
ENV['AMAZONRCDIR'] = './'
ENV['AMAZONRCFILE'] = '.amazonrc'

require 'amazon/aws'
require 'amazon/aws/search'

req = Amazon::AWS::Search::Request.new()
is = Amazon::AWS::ItemSearch.new( 'Books', {'Title' => 'ruby'} )
rg = Amazon::AWS::ResponseGroup.new( 'Large' )
page = req.search( is, rg, :ALL_PAGES)

page.each do |p|
  p.item_search_response.items.item.each do |i|
    i.item_attributes.to_h.each do |k, v|
      if Amazon::AWS::AWSArray === v
        puts "#{k} = #{v.join("  /  ")}" if not v.nil?
      else
        puts "#{k} = #{v}" if not v.nil?
      end
    end
    puts ""
  end
end