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;
29 use vars qw($VERSION @ISA @EXPORT);
52 my $locale = C4::Context->preference('AmazonLocale');
53 my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
60 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
64 This module provides facilities for retrieving Amazon.com content in Koha
68 =item get_amazon_detail( $isbn, $record, $marcflavour, $services )
70 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
86 MARC flavor, MARC21 or UNIMARC
90 Requested Amazon services: A ref to an array. For example,
91 [ 'Similarities', 'EditorialReviews', 'Reviews' ].
92 No other service will be accepted. Services must be spelled exactly.
93 If no sercice is requested, AWS isn't called.
97 =item get_amazon_tld()
99 Get Amazon Top Level Domain depending on Amazon local preference: AmazonLocal.
100 For example, if AmazonLocal is 'UK', returns '.co.uk'.
107 sub get_amazon_details {
108 my ( $isbn, $record, $marcflavour, $aws_ref ) = @_;
110 return unless defined $aws_ref;
112 return if $#aws == -1;
114 # Normalize the fields
115 $isbn = GetNormalizedISBN($isbn);
116 my $upc = GetNormalizedUPC($record,$marcflavour);
117 my $ean = GetNormalizedEAN($record,$marcflavour);
118 # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
120 # Choose the appropriate and available item identifier
121 my ( $id_type, $item_id ) =
122 defined($isbn) && length($isbn) == 13 ? ( 'EAN', $isbn ) :
123 $isbn ? ( 'ASIN', $isbn ) :
124 $upc ? ( 'UPC', $upc ) :
125 $ean ? ( 'EAN', $upc ) : ( undef, undef );
126 return unless defined($id_type);
128 # grab the item format to determine Amazon search index
129 my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
130 my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
133 "http://ecs.amazonaws" . get_amazon_tld() .
134 "/onca/xml?Service=AWSECommerceService" .
135 "&AWSAccessKeyId=" . C4::Context->preference('AWSAccessKeyID') .
136 "&Operation=ItemLookup" .
137 "&AssociateTag=" . C4::Context->preference('AmazonAssocTag') .
138 "&Version=2009-02-01" .
141 "&ResponseGroup=" . join( ',', @aws );
142 $url .= "&SearchIndex=$search_index" if $id_type ne 'ASIN';
144 my $content = get($url);
145 warn "could not retrieve $url" unless $content;
146 my $xmlsimple = XML::Simple->new();
147 my $response = $xmlsimple->XMLin(
149 forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
154 sub check_search_inside {
156 my $ua = LWP::UserAgent->new(
157 agent => "Mozilla/4.76 [en] (Win98; U)",
162 my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
163 my $req = HTTP::Request->new(GET => $uri);
165 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
166 'Accept-Charset' => 'iso-8859-1,*,utf-8',
167 'Accept-Language' => 'en-US' );
168 my $res = $ua->request($req);
169 my $content = $res->content();
170 if ($content =~ m/This book is temporarily unavailable/) {
183 Joshua Ferraro <jmf@liblime.com>