やりたいこと
Rubyにて、ISBN投げるだけで書籍名とか出版社が一発でポーンと帰ってくる魔法
用意するもの
キーの取得
これが一番面倒だった
アクセスキーとシークレットキーはrootユーザーでないとダメっぽい ソース
IAMは現在サポートされておりません。ルートアカウントのアクセスキーをご使用ください。
まずは叩いてみる
今回はRailsでの利用を想定している。 先人様が便利なgemを用意してくれたので使わない手はない
Gemfileにgem 'amazon-ecs'
を追加するだけ
次にconfig/initializers/amazon-ecs.rbを新規作成して以下
Amazon::Ecs.configure do |options| options[:associate_tag] = 'アフィリエイトID-22' options[:AWS_access_key_id] = 'アクセスキー' options[:AWS_secret_key] = 'シークレットキー' end
まずは単純に検索
Amazon::Ecs.debug = true options = { country: 'jp' } res = Amazon::Ecs.item_search("new game 2巻", options) puts res.doc.to_s
実行結果は以下
Adding AWSAccessKeyId=************ Adding AssociateTag=********-22 Adding Keywords=new game 2巻 Adding Operation=ItemSearch Adding SearchIndex=Books Adding Service=AWSECommerceService Adding Timestamp=2015-12-02T23:15:32Z Adding Version=2011-08-01 Request URL: http://ecs.amazonaws.jp/onca/xml?AWSAccessKeyId=AKIAILDBTZAKHZP5QIEQ&AssociateTag=*****-22&Keywords=new%20game%202%E5%B7%BB&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2015-12-02T23%3A15%3A32Z&Version=2011-08-01&Signature=tYCJ0YdbzcwsVRukVwfKb%2FNFinZJnE%2Fx05TU7et4gjI%3D <?xml version="1.0" encoding="UTF-8"?> <ItemSearchResponse> <OperationRequest> <HTTPHeaders> <Header Name="UserAgent" Value="Ruby"/> </HTTPHeaders> <RequestId>26ee7f67-fca5-48bc-8a95-05627249bd81</RequestId> <Arguments> <Argument Name="AWSAccessKeyId" Value="**********"/> <Argument Name="AssociateTag" Value="*******-22"/> <Argument Name="Keywords" Value="new game 2巻"/> <Argument Name="Operation" Value="ItemSearch"/> <Argument Name="SearchIndex" Value="Books"/> <Argument Name="Service" Value="AWSECommerceService"/> <Argument Name="Timestamp" Value="2015-12-02T23:15:32Z"/> <Argument Name="Version" Value="2011-08-01"/> <Argument Name="Signature" Value="tYCJ0YdbzcwsVRukVwfKb/NFinZJnE/x05TU7et4gjI="/> </Arguments> <RequestProcessingTime>0.0245360000000000</RequestProcessingTime> </OperationRequest> <Items> (略) <ItemAttributes> <Author>得能正太郎</Author> <Manufacturer>芳文社</Manufacturer> <ProductGroup>eBooks</ProductGroup> <Title>NEW GAME! 1巻 (まんがタイムKRコミックス)</Title> </ItemAttributes> </Item> </Items> </ItemSearchResponse>
まぁ要領はわかった
ISBNを投げる
# Amazon::Ecs.debug = true res = Amazon::Ecs.item_search( "9784797372274", search_index: 'Books', response_group: 'Medium', country: 'jp' ) info = { title: res.first_item.get('ItemAttributes/Title'), author: res.first_item.get('ItemAttributes/Author'), publisher: res.first_item.get('ItemAttributes/Manufacturer'), published_date: res.first_item.get('ItemAttributes/PublicationDate') } puts info.to_s
結果
{:title=>"たのしいRuby第4版", :author=>"高橋 征義", :publisher=>"SBクリエイティブ", :published_date=>"2015-04-24"}
response_group
はどれほどの情報量を持ってくるかの値で指定できる値はSmall, Medium, Largeの三種類。Smallが一番スマートだけどpublished_date
が取得できないので断念。