162 lines
6.6 KiB
Markdown
162 lines
6.6 KiB
Markdown
---
|
||
title: AmazonのWebサービスを使ってみる
|
||
author: kazu634
|
||
date: 2009-05-28
|
||
url: /2009/05/28/_1253/
|
||
wordtwit_post_info:
|
||
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4617;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
||
categories:
|
||
- Perl
|
||
|
||
---
|
||
<div class="section">
|
||
<p>
|
||
はてなで日記を書いて本の紹介をするときは、本の裏をめくってisbn13をasinに変換している。でも、この方法だと雑誌の場合うまくいかない。雑誌はasinでBから始まっている。普通のisbn13をasin(=isbn10)に変換するアルゴリズムでは変換できないからだ。
|
||
</p>
|
||
|
||
<p>
|
||
ある日、ふとAmazonでisbn13(=実はバーコードの数字と同じ)を入力すると、検索できることが判明。前に試したときはできなかったような気がするのだけれど。。。でも、isbn13を入力すれば、Amazonで管理しているID(=asin)が判明することがわかった。
|
||
</p>
|
||
|
||
<p>
|
||
というわけで、PerlでAmazonのWebサービスを使ってみようと決意したのであった。
|
||
</p>
|
||
|
||
<h4>
|
||
Perlのコード
|
||
</h4>
|
||
|
||
<p>
|
||
Perlの偉い人が「<a href="http://blog.livedoor.jp/dankogai/archives/51211577.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://blog.livedoor.jp/dankogai/archives/51211577.html', '404 Blog Not Found:perl – URI::Amazon::APA released!');" target="_blank">404 Blog Not Found:perl – URI::Amazon::APA released!</a>」というのを公開しているので、それを活用する。とりあえずサンプルをそのままコピペしてみた。
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
<span class="synComment"># === Libraries ===</span>
|
||
<span class="synStatement">use strict</span>;
|
||
<span class="synStatement">use warnings</span>;
|
||
<span class="synStatement">use </span>LWP::UserAgent;
|
||
<span class="synStatement">use </span>URI::Amazon::APA;
|
||
<span class="synStatement">use </span>XML::Simple;
|
||
<span class="synStatement">use </span>YAML::Syck;
|
||
<span class="synComment"># === Main part ===</span>
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$u</span> = URI::Amazon::APA-><span class="synStatement">new</span>(<span class="synConstant">'http://ecs.amazonaws.jp/onca/xml'</span>);
|
||
<span class="synIdentifier">$u</span>->query_form(
|
||
<span class="synConstant">Service </span>=> <span class="synConstant">'AWSECommerceService'</span>,
|
||
<span class="synConstant">Operation </span>=> <span class="synConstant">'ItemLookup'</span>,
|
||
<span class="synConstant">ItemId </span>=> <span class="synStatement">shift</span> || <span class="synConstant">'9784798118925'</span>,
|
||
<span class="synConstant">IdType </span>=> <span class="synConstant">'EAN'</span>,
|
||
<span class="synConstant">SearchIndex </span>=> <span class="synConstant">'Books'</span>,
|
||
);
|
||
<span class="synIdentifier">$u</span>->sign(
|
||
<span class="synConstant">key </span>=> <span class="synConstant">'キー'</span>,
|
||
<span class="synConstant">secret </span>=> <span class="synConstant">'秘密鍵'</span>
|
||
);
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$ua</span> = LWP::UserAgent-><span class="synStatement">new</span>;
|
||
<span class="synStatement">my</span> <span class="synIdentifier">$r</span> = <span class="synIdentifier">$ua</span>->get(<span class="synIdentifier">$u</span>);
|
||
<span class="synStatement">print</span> <span class="synIdentifier">$r</span>->status_line, <span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, YAML::Syck::Dump( XMLin( <span class="synIdentifier">$r</span>->content ) );
|
||
</pre>
|
||
|
||
<p>
|
||
注意すべきは、
|
||
</p>
|
||
|
||
<ul>
|
||
<li>
|
||
IdTypeはEANを指定する(デフォルトだとasinで検索してしまうため。今回はバーコードの数字で検索できるように指定してあげる)
|
||
</li>
|
||
<li>
|
||
SearchIndexはBooks(今回は本なので)
|
||
</li>
|
||
</ul>
|
||
|
||
<p>
|
||
という点です。
|
||
</p>
|
||
|
||
<h4>
|
||
実行結果
|
||
</h4>
|
||
|
||
<p>
|
||
実行してみます:
|
||
</p>
|
||
|
||
<pre class="syntax-highlight">
|
||
~<span class="synStatement">/</span><span class="synConstant">bin on simoom634 </span><span class="synSpecial">[503]</span><span class="synConstant"> </span><span class="synIdentifier">$: </span><span class="synConstant">perl </span><span class="synSpecial">.</span><span class="synStatement">/</span>access_amazon.pl
|
||
<span class="synConstant">200</span> OK
|
||
---
|
||
Items:
|
||
Item:
|
||
ASIN: <span class="synConstant">4798118923</span>
|
||
DetailPageURL: http://www.amazon.co.jp/More-Joel-Software-Spolsky/dp/<span class="synConstant">4798118923</span>%3FSubscriptionId%3DAKIAI54BKVADN7EPRVZA%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798118923
|
||
ItemAttributes:
|
||
Author: Joel Spolsky
|
||
Creator:
|
||
Role: 翻訳
|
||
content: 青木 靖
|
||
Manufacturer: 翔泳社
|
||
ProductGroup: Book
|
||
Title: More Joel on Software
|
||
ItemLinks:
|
||
ItemLink:
|
||
-
|
||
Description: Add To Wishlist
|
||
-
|
||
Description: Tell A Friend
|
||
-
|
||
Description: All Customer Reviews
|
||
-
|
||
Description: All Offers
|
||
Request:
|
||
IsValid: <span class="synConstant">'True'</span>
|
||
ItemLookupRequest:
|
||
Condition: New
|
||
DeliveryMethod: Ship
|
||
IdType: EAN
|
||
ItemId: <span class="synConstant">9784798118925</span>
|
||
MerchantId: Amazon
|
||
OfferPage: <span class="synConstant">1</span>
|
||
ResponseGroup: Small
|
||
ReviewPage: <span class="synConstant">1</span>
|
||
ReviewSort: -SubmissionDate
|
||
SearchIndex: Books
|
||
VariationPage: All
|
||
OperationRequest:
|
||
Arguments:
|
||
Argument:
|
||
-
|
||
Name: Operation
|
||
Value: ItemLookup
|
||
-
|
||
Name: Service
|
||
Value: AWSECommerceService
|
||
-
|
||
Name: Version
|
||
Value: <span class="synConstant">2009-01-01</span>
|
||
-
|
||
Name: SearchIndex
|
||
Value: Books
|
||
-
|
||
Name: Signature
|
||
Value: gwfPyW7fYVfp/lFxseRThAn8uSCKV4+SRj5CAAJeBws=
|
||
-
|
||
Name: ItemId
|
||
Value: <span class="synConstant">9784798118925</span>
|
||
-
|
||
Name: IdType
|
||
Value: EAN
|
||
-
|
||
Name: AWSAccessKeyId
|
||
Value: AKIAI54BKVADN7EPRVZA
|
||
-
|
||
Name: Timestamp
|
||
Value: <span class="synConstant">2009-05</span>-28T14:<span class="synConstant">46</span>:19Z
|
||
HTTPHeaders:
|
||
Header:
|
||
Name: UserAgent
|
||
Value: libwww-perl/<span class="synConstant">5.826</span>
|
||
RequestId: 73da30ef-a5eb-4f67-bb5e-50b699de5177
|
||
RequestProcessingTime: <span class="synConstant">0.0748140000000000</span>
|
||
xmlns: http://webservices.amazon.co<span class="synStatement">m/</span><span class="synConstant">AWSECommerceService</span><span class="synStatement">/</span><span class="synConstant">2008-10-06</span>
|
||
</pre>
|
||
</div> |