blog/content/post/2009/05/29/2009-05-29-00001163.md

117 lines
7.9 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: AmazonのWebサービスを活用してASIN記法をはき出すPerlスクリプト
author: kazu634
date: 2009-05-29
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:4619;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
先日の続きと言うことで、さっそく作ってみました。
</p>
<pre class="syntax-highlight">
<span class="synPreProc">#!/opt/local/bin/perl</span>
<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"># === 起動時の処理 ===</span>
<span class="synStatement">exit</span> <span class="synStatement">if</span> ( <span class="synIdentifier">@ARGV</span> == <span class="synConstant"></span> );
<span class="synStatement">exit</span> <span class="synStatement">unless</span> ( <span class="synIdentifier">$ARGV</span>[<span class="synConstant"></span>] =~<span class="synStatement"> /</span><span class="synSpecial">\d\d\d\d\d\d\d\d\d\d\d\d\d</span><span class="synStatement">/</span> );
<span class="synComment"># === Main part ===</span>
<span class="synStatement">my</span> <span class="synIdentifier">$u</span> = URI::Amazon::APA-&#62;<span class="synStatement">new</span>(<span class="synConstant">'http://ecs.amazonaws.jp/onca/xml'</span>);
<span class="synIdentifier">$u</span>-&#62;query_form(
<span class="synConstant">Service </span>=&#62; <span class="synConstant">'AWSECommerceService'</span>,
<span class="synConstant">Operation </span>=&#62; <span class="synConstant">'ItemLookup'</span>,
<span class="synConstant">ItemId </span>=&#62; <span class="synStatement">shift</span> || <span class="synConstant">'9784798118925'</span>,
<span class="synConstant">IdType </span>=&#62; <span class="synConstant">'EAN'</span>,
<span class="synConstant">SearchIndex </span>=&#62; <span class="synConstant">'Books'</span>,
);
<span class="synIdentifier">$u</span>-&#62;sign(
<span class="synConstant">key </span>=&#62; キー,
<span class="synConstant">secret </span>=&#62; 秘密鍵,
);
<span class="synStatement">my</span> <span class="synIdentifier">$ua</span> = LWP::UserAgent-&#62;<span class="synStatement">new</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$r</span> = <span class="synIdentifier">$ua</span>-&#62;get(<span class="synIdentifier">$u</span>);
<span class="synStatement">die</span> <span class="synConstant">&#34;Item not found.</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>
<span class="synStatement">if</span> ( <span class="synStatement">not</span>( <span class="synStatement">exists</span>( XMLin( <span class="synIdentifier">$r</span>-&#62;content )-&#62;{Items}-&#62;{Item}-&#62;{ASIN} ) ) );
<span class="synStatement">print</span>(<span class="synConstant">&#34;[asin:&#34;</span>);
<span class="synStatement">print</span> <span class="synIdentifier">XMLin</span>( <span class="synIdentifier">$r</span>-&#62;content )-&#62;{Items}-&#62;{Item}-&#62;{ASIN};
<span class="synStatement">print</span>(<span class="synConstant">&#34;:title]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
</pre>
<h4>
実行例
</h4>
<pre class="syntax-highlight">
~/bin on simoom634 <span class="synStatement">[</span><span class="synConstant">535</span><span class="synStatement">]</span> $: ./access_amazon.pl <span class="synConstant">9784798118926</span>
Item not found.
~/bin on simoom634 <span class="synStatement">[</span><span class="synConstant">536</span><span class="synStatement">]</span> $: ./access_amazon.pl <span class="synConstant">9784798118925</span>
<span class="synStatement">[</span>asin:<span class="synConstant">4798118923</span>:title<span class="synStatement">]</span>
</pre>
<h4>
「amazon」に関連する最近のエントリ
</h4>
<ul>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20090528/1243522107" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090528/1243522107', ' AmazonのWebサービスを使ってみる &#8211; 武蔵の日記');" target="_blank"> AmazonのWebサービスを使ってみる &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20090527/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090527/', '武蔵の日記');" target="_blank">武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20090426/1240754178" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090426/1240754178', ' 本日購入した本 &#8211; 武蔵の日記');" target="_blank"> 本日購入した本 &#8211; 武蔵の日記</a>
</li>
<li>
<a href="http://d.hatena.ne.jp/sirocco634/20090318/1237386398" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/sirocco634/20090318/1237386398', ' KOKIA∞AKIKO~balance~ &#8211; 武蔵の日記');" target="_blank"> KOKIA∞AKIKO~balance~ &#8211; 武蔵の日記</a>
</li>
</ul>
<p>
</p>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4774124966/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774124966/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51VDVNDQCWL._SL160_.jpg" class="hatena-asin-detail-image" alt="最新WebサービスAPIエクスプロ-ラ ~Amazon、はてな、Google、Yahoo! 4大Webサービス完全攻略" title="最新WebサービスAPIエクスプロ-ラ ~Amazon、はてな、Google、Yahoo! 4大Webサービス完全攻略" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4774124966/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4774124966/?tag=hatena_st1-22&ascsubtag=d-7ibv', '最新WebサービスAPIエクスプロ-ラ ~Amazon、はてな、Google、Yahoo! 4大Webサービス完全攻略');">最新WebサービスAPIエクスプロ-ラ ~Amazon、はてな、Google、Yahoo! 4大Webサービス完全攻略</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/Software%20Design%20%CA%D4%BD%B8%C9%F4" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/Software%20Design%20%CA%D4%BD%B8%C9%F4', 'Software Design 編集部');" class="keyword">Software Design 編集部</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B5%BB%BD%D1%C9%BE%CF%C0%BC%D2', '技術評論社');" class="keyword">技術評論社</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 2005/09/23
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 大型本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 11人 <span class="hatena-asin-detail-label">クリック</span>: 511回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4774124966" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4774124966', 'この商品を含むブログ (112件) を見る');" target="_blank">この商品を含むブログ (112件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>