Amazon Web Servicesを使ってISBN-13からASINを取得するPerlプログラム
Amazon Web Servicesを使って、ISBN-13からASINを取得するPerlプログラムを作ってみました。(下の方で他の言語版もリンクしてあります)
use strict; use warnings; use CGI; use Encode; use LWP::Simple; use XML::Simple; my $aws_access_key_id = 'あなたのAccessKeyId'; # For debug. my $OUTPUT_ENCODING = 'Shift_JIS'; # Set up ISBN. my $isbn = '9784798111117'; #『オブジェクト指向入門 第2版 原則・コンセプト』のISBN-13 # Set up URL. my $request_url = 'http://webservices.amazon.co.jp/onca/xml'; my @option_list = ( "Service=AWSECommerceService", "AWSAccessKeyId=${aws_access_key_id}", "Operation=ItemLookup", "IdType=ISBN", "ItemId=${isbn}", "SearchIndex=Books", "ResponseGroup=Request,Small", "Version=2007-01-15", ); # Assemble all options. my $option_string = join('&', @option_list); my $url = "${request_url}?${option_string}"; # Retrieve result. print "ISBN: ${isbn}\n"; my $response_string = get($url); my $response_hashref = XMLin($response_string); # Compose output string. my $output = <<"EOD"; ASIN: $response_hashref->{Items}->{Item}->{ASIN} Title: $response_hashref->{Items}->{Item}->{ItemAttributes}->{Title} Author: $response_hashref->{Items}->{Item}->{ItemAttributes}->{Author} EOD # Print as string. Encode::from_to($output, "utf8", $OUTPUT_ENCODING); print $output;
実行結果です。Windowsのコマンドラインで表示させるために$OUTPUT_ENCODINGをShift_JISにしています。
C:\work>perl amazon_isbn.pl ISBN: 9784798111117 ASIN: 4798111112 Title: オブジェクト指向入門 第2版 原則・コンセプト Author: バートランド・メイヤー
なお、日経ソフトウエアの2007年4月号では、Web API(Webサービス)が特集になります。楽しみですね。結城も記事を書きます。
↓以下は秘密のメモ(のMD5ハッシュ値)。
63 E1 52 91 7A 90 29 5E 6C 02 A7 01 D3 93 53 B4
追記:2007-01-24
弾さんから添削していただきました。ありがとうございます。なるほど、URIというモジュールがあるのですね。勉強になりました。感謝!
ところで…先日の最後に書いておいた「秘密のメモ(のMD5ハッシュ値)」の話です。これは、Shift_JISの「弾さんから添削来るといいな♪」というメッセージのMD5ハッシュ値でした♪
追記:2007-01-26, 27, ...
クィックハックには「…てみました」というフレーズが似合う。3%の誇らしさをブレンドして。
あ。
タイトルですべてを語ってしまった。