$VERSION = 0.03;
@ISA = qw(Exporter);
@EXPORT = qw(
- &get_amazon_details
- &check_search_inside
+ get_amazon_details
+ get_amazon_tld
);
}
+
+sub get_amazon_tld {
+ my %tld = (
+ CA => '.ca',
+ DE => '.de',
+ FR => '.fr',
+ JP => '.jp',
+ UK => '.co.uk',
+ US => '.com',
+ );
+
+ my $locale = C4::Context->preference('AmazonLocale');
+ my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
+ return $tld;
+}
+
+
=head1 NAME
C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
-=head1 FUNCTIONS
+=head2 FUNCTIONS
This module provides facilities for retrieving Amazon.com content in Koha
-=head2 get_amazon_details
+=over
+
+=item get_amazon_detail( $isbn, $record, $marcflavour, $services )
+
+Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
+
+Parameters:
+
+=over
+
+=item $isbn
-=over 4
+Biblio record isbn
-my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
+=item $record
+
+Biblio MARC record
+
+=item $marcflavour
+
+MARC flavor, MARC21 or UNIMARC
+
+=item $services
+
+Requested Amazon services: A ref to an array. For example,
+[ 'Similarities', 'EditorialReviews', 'Reviews' ].
+No other service will be accepted. Services must be spelled exactly.
+If no sercice is requested, AWS isn't called.
=back
-Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
+=item get_amazon_tld()
+
+Get Amazon Top Level Domain depending on Amazon local preference: AmazonLocal.
+For example, if AmazonLocal is 'UK', returns '.co.uk'.
+
+=back
=cut
+
sub get_amazon_details {
- my ( $isbn, $record, $marcflavour ) = @_;
+ my ( $isbn, $record, $marcflavour, $aws_ref ) = @_;
+
+ return unless defined $aws_ref;
+ my @aws = @$aws_ref;
+ return if $#aws == -1;
# Normalize the fields
$isbn = GetNormalizedISBN($isbn);
my $upc = GetNormalizedUPC($record,$marcflavour);
my $ean = GetNormalizedEAN($record,$marcflavour);
-
# warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
- my ( $id_type, $item_id);
- if (length($isbn) eq 13) { # if the isbn is 13-digit, search Amazon using EAN
- $id_type = 'EAN';
- $item_id = $isbn;
- }
- elsif ($isbn) {
- $id_type = 'ASIN';
- $item_id = $isbn;
- }
- elsif ($upc) {
- $id_type = 'UPC';
- $item_id = $upc;
- }
- elsif ($ean) {
- $id_type = 'EAN';
- $item_id = $upc;
- }
- else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
- return undef;
- }
-
- my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
- my $formats;
- $formats->{'a'} = 'Books';
- $formats->{'g'} = 'Video';
- $formats->{'j'} = 'Music';
-
- my $search_index = $formats->{$format};
-
- # Determine which content to grab in the request
-
- # Determine correct locale
- my $locale_hashref = {
- CA => '.ca',
- DE => '.de',
- FR => '.fr',
- JP => '.jp',
- UK => '.co.uk',
- US => '.com',
- };
-
- my $amazon_locale_syspref = C4::Context->preference('AmazonLocale');
- my $tld = $locale_hashref->{$amazon_locale_syspref} || '.com'; # default top level domain is .com
-
- # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
- my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
-
- #grab the associates tag: mine is 'kadabox-20'
- my $af_tag=C4::Context->preference('AmazonAssocTag');
- my $response_group = "Similarities,EditorialReview,Reviews,ItemAttributes,Images";
- my $url = "http://ecs.amazonaws$tld/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=$aws_access_key_id&Operation=ItemLookup&AssociateTag=$af_tag&Version=2007-01-15&ItemId=$item_id&IdType=$id_type&ResponseGroup=$response_group";
- if ($id_type ne 'ASIN') {
- $url .= "&SearchIndex=$search_index";
- }
- # warn $url;
+ # Choose the appropriate and available item identifier
+ my ( $id_type, $item_id ) =
+ defined($isbn) && length($isbn) == 13 ? ( 'EAN', $isbn ) :
+ $isbn ? ( 'ASIN', $isbn ) :
+ $upc ? ( 'UPC', $upc ) :
+ $ean ? ( 'EAN', $upc ) : ( undef, undef );
+ return unless defined($id_type);
+
+ # grab the item format to determine Amazon search index
+ my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
+ my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
+
+ my $url =
+ "http://ecs.amazonaws" . get_amazon_tld() .
+ "/onca/xml?Service=AWSECommerceService" .
+ "&AWSAccessKeyId=" . C4::Context->preference('AWSAccessKeyID') .
+ "&Operation=ItemLookup" .
+ "&AssociateTag=" . C4::Context->preference('AmazonAssocTag') .
+ "&Version=2009-02-01" .
+ "&ItemId=$item_id" .
+ "&IdType=$id_type" .
+ "&ResponseGroup=" . join( ',', @aws );
+ $url .= "&SearchIndex=$search_index" if $id_type ne 'ASIN';
+ #warn $url;
my $content = get($url);
warn "could not retrieve $url" unless $content;
my $xmlsimple = XML::Simple->new();
my $response = $xmlsimple->XMLin(
$content,
- forcearray => [ qw(SimilarProduct EditorialReview Review) ],
+ forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
) unless !$content;
return $response;
}