1 package C4::External::Amazon;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use HTTP::Request::Common;
27 use Digest::SHA qw(hmac_sha256_base64);
32 use vars qw($VERSION @ISA @EXPORT);
56 my $locale = C4::Context->preference('AmazonLocale');
57 my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
64 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
68 This module provides facilities for retrieving Amazon.com content in Koha
70 =head2 get_amazon_details
74 my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
78 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
82 sub get_amazon_details {
83 my ( $isbn, $record, $marcflavour ) = @_;
86 $isbn = _normalize_match_point ($isbn);
88 my $upc = _get_amazon_upc($record,$marcflavour);
89 my $ean = _get_amazon_ean($record,$marcflavour);
91 # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
93 # Choose the appropriate and available item identifier
94 my ( $id_type, $item_id ) =
95 defined($isbn) && length($isbn) == 13 ? ( 'EAN', $isbn ) :
96 $isbn ? ( 'ASIN', $isbn ) :
97 $upc ? ( 'UPC', $upc ) :
98 $ean ? ( 'EAN', $upc ) : ( undef, undef );
99 return unless defined($id_type);
101 # grab the item format to determine Amazon search index
102 my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
103 my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
105 my $parameters={Service=>"AWSECommerceService" ,
106 "AWSAccessKeyId"=> C4::Context->preference('AWSAccessKeyID') ,
107 "Operation"=>"ItemLookup",
108 "AssociateTag"=> C4::Context->preference('AmazonAssocTag') ,
109 "Version"=>"2009-06-01",
112 "ResponseGroup"=> join( ',', @aws ),
113 "Timestamp"=>strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
115 $$parameters{"SearchIndex"} = $search_index if $id_type ne 'ASIN';
117 while (my ($key,$value)=each %$parameters){
118 push @params, qq{$key=}.uri_escape($value, "^A-Za-z0-9\-_.~" );
122 if (C4::Context->preference('AWSPrivateKey')) {
123 $url = qq{http://webservices.amazon} . get_amazon_tld() .
124 "/onca/xml?" . join("&",sort @params) . qq{&Signature=} . uri_escape(SignRequest(@params),"^A-Za-z0-9\-_.~" );
126 $url = qq{http://webservices.amazon} . get_amazon_tld() . "/onca/xml?" .join("&",sort @params);
127 warn "MUST set AWSPrivateKey syspref after 2009-08-15 in order to access Amazon web services";
130 my $content = get($url);
131 warn "could not retrieve $url" unless $content;
132 my $xmlsimple = XML::Simple->new();
133 my $response = $xmlsimple->XMLin(
135 forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
142 my $tld=get_amazon_tld();
143 my $string = qq{GET\nwebservices.amazon$tld\n/onca/xml\n} . join("&",sort @params);
144 return hmac_sha256_base64($string,C4::Context->preference('AWSPrivateKey')) . '=';
147 sub check_search_inside {
149 my $ua = LWP::UserAgent->new(
150 agent => "Mozilla/4.76 [en] (Win98; U)",
155 my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
156 my $req = HTTP::Request->new(GET => $uri);
158 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
159 'Accept-Charset' => 'iso-8859-1,*,utf-8',
160 'Accept-Language' => 'en-US' );
161 my $res = $ua->request($req);
162 my $content = $res->content();
163 if ($content =~ m/This book is temporarily unavailable/) {
169 sub _get_amazon_upc {
170 my ($record,$marcflavour) = @_;
173 if ($marcflavour eq 'MARC21') {
174 @fields = $record->field('024');
175 foreach my $field (@fields) {
176 my $indicator = $field->indicator(1);
177 my $upc = _normalize_match_point($field->subfield('a'));
178 if ($indicator == 1 and $upc ne '') {
183 else { # assume unimarc if not marc21
184 @fields = $record->field('072');
185 foreach my $field (@fields) {
186 my $upc = _normalize_match_point($field->subfield('a'));
194 sub _get_amazon_ean {
195 my ($record,$marcflavour) = @_;
198 if ($marcflavour eq 'MARC21') {
199 @fields = $record->field('024');
200 foreach my $field (@fields) {
201 my $indicator = $field->indicator(1);
202 my $upc = _normalize_match_point($field->subfield('a'));
203 if ($indicator == 3 and $upc ne '') {
208 else { # assume unimarc if not marc21
209 @fields = $record->field('073');
210 foreach my $field (@fields) {
211 my $upc = _normalize_match_point($field->subfield('a'));
219 sub _normalize_match_point {
220 my $match_point = shift;
221 (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
222 $normalized_match_point =~ s/-//g;
224 return $normalized_match_point;
234 Joshua Ferraro <jmf@liblime.com>