[Koha-patches] [PATCH] Rework revised Amazon code.

Joe Atzberger joe.atzberger at liblime.com
Fri Jan 9 22:42:28 CET 2009


~ Make $record (and $marcflavour) optional, allowing you to use just ISBN,
~ Fallback to Amazon "Books" index,
~ This prevents warnings on undef concatenation later in the block,
~ Added test cases to t/Amazon.t,
~ Remove unused variables and double-declared my variables,
---
 C4/Amazon.pm |  110 ++++++++++++++++++++++++++++++++++-----------------------
 t/Amazon.t   |   13 ++++++-
 2 files changed, 77 insertions(+), 46 deletions(-)

diff --git a/C4/Amazon.pm b/C4/Amazon.pm
index a9cb339..c4bd230 100644
--- a/C4/Amazon.pm
+++ b/C4/Amazon.pm
@@ -22,6 +22,9 @@ use LWP::Simple;
 use LWP::UserAgent;
 use HTTP::Request::Common;
 
+use C4::Debug;
+our ($debug);
+
 use strict;
 use warnings;
 
@@ -29,7 +32,7 @@ use vars qw($VERSION @ISA @EXPORT);
 
 BEGIN {
     require Exporter;
-    $VERSION = 0.03;
+    $VERSION = 0.04;
     @ISA = qw(Exporter);
     @EXPORT = qw(
         &get_amazon_details
@@ -37,6 +40,14 @@ BEGIN {
     );
 }
 
+our $override = {};
+# To allow testing w/o DB access, populate this hashref in your script as follows:
+# $C4::Amazon::override = {
+#   AmazonAssocTag => 'MyTag',
+#   AWSAccessKeyID => 'MyKeyID',
+#   AmazonLocale   => 'FR',
+# };
+
 =head1 NAME
 
 C4::Amazon - Functions for retrieving Amazon.com content in Koha
@@ -49,12 +60,22 @@ This module provides facilities for retrieving Amazon.com content in Koha
 
 =over 4
 
-my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
+my $amazon_details = &get_amazon_details( $isbn, [$record, $marcflavour] );
 
 =back
 
 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
 
+=over 4
+
+B<$isbn> is the 10 or 13-digit ISBN of the item to retrieve.
+
+B<$record> is optionally the MARC::Record object so we can do better matching based on type.
+
+B<$marcflavour> is MARC21 or UNIMARC.  Defaults to UNIMARC.
+
+=back
+
 =cut
 
 sub get_amazon_details {
@@ -63,39 +84,44 @@ sub get_amazon_details {
     #normalize the ISBN
     $isbn = _normalize_match_point ($isbn);
 
-    my $upc = _get_amazon_upc($record,$marcflavour);
-    my $ean = _get_amazon_ean($record,$marcflavour);
-
-    # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
+    my ($upc, $ean);
+    if ($record) {
+        $upc = _get_amazon_upc($record,$marcflavour);
+        $ean = _get_amazon_ean($record,$marcflavour);
+    }
+    $debug and warn sprintf "ISBN: %s | UPC: %s | EAN: %s", ($isbn||'UNDEF'), ($upc||'UNDEF'), ($ean||'UNDEF');
 
     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;
+    if (length($isbn) == 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;
+        $id_type = 'ASIN';
+        $item_id = $isbn;
     }
     elsif ($upc) {
-	$id_type = 'UPC';
-	$item_id = $upc;
+        $id_type = 'UPC';
+        $item_id = $upc;
     }
     elsif ($ean) {
-	$id_type = 'EAN';
-	$item_id = $upc;
+        $id_type = 'EAN';
+        $item_id = $upc;
     }
     else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
-	return undef;
+        return;
     }
 
-    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};
+    my $search_index = 'Books';  # default to Books if no record or $format unrecognized
+    if ($record) {
+        my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
+        my %formats = (
+            a => 'Books',
+            g => 'Video',
+            j => 'Music',
+        );
+        ($formats{$format}) and $search_index = $formats{$format};
+    }
 
     # Determine which content to grab in the request
 
@@ -109,27 +135,26 @@ sub get_amazon_details {
         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');
+    # Check for override values first.  See note above about how to use them.
+    my $amazon_locale_syspref = $override->{AmazonLocale}   || C4::Context->preference('AmazonLocale'  );   # mine is 'US'
+    my $aws_access_key_id     = $override->{AWSAccessKeyID} || C4::Context->preference('AWSAccessKeyID');   # mine is '0V5RRRRJZ3HR2RQFNHR2'
+    my $af_tag                = $override->{AmazonAssocTag} || C4::Context->preference('AmazonAssocTag');   # mine is 'kadabox-20'
 
-    #grab the associates tag: mine is 'kadabox-20'
-    my $af_tag=C4::Context->preference('AmazonAssocTag');
+    my $tld = $locale_hashref->{$amazon_locale_syspref} || '.com'; # default top level domain is .com
     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;
+    ($id_type ne 'ASIN') and $url .= "&SearchIndex=$search_index";
+    $debug and warn "Getting url: $url";
     my $content = get($url);
-    warn "could not retrieve $url" unless $content;
+    unless ($content) {
+        warn "could not retrieve $url";
+        return;
+    }
     my $xmlsimple = XML::Simple->new();
     my $response = $xmlsimple->XMLin(
         $content,
         forcearray => [ qw(SimilarProduct EditorialReview Review) ],
-    ) unless !$content;
+    );
     return $response;
 }
 
@@ -157,10 +182,8 @@ sub check_search_inside {
 
 sub _get_amazon_upc {
 	my ($record,$marcflavour) = @_;
-	my (@fields,$upc);
-
 	if ($marcflavour eq 'MARC21') {
-		@fields = $record->field('024');
+		my @fields = $record->field('024');
 		foreach my $field (@fields) {
 			my $indicator = $field->indicator(1);
 			my $upc = _normalize_match_point($field->subfield('a'));
@@ -170,7 +193,7 @@ sub _get_amazon_upc {
 		}
 	}
 	else { # assume unimarc if not marc21
-		@fields = $record->field('072');
+		my @fields = $record->field('072');
 		foreach my $field (@fields) {
 			my $upc = _normalize_match_point($field->subfield('a'));
 			if ($upc ne '') {
@@ -182,10 +205,9 @@ sub _get_amazon_upc {
 
 sub _get_amazon_ean {
 	my ($record,$marcflavour) = @_;
-	my (@fields,$ean);
 
 	if ($marcflavour eq 'MARC21') {
-		@fields = $record->field('024');
+		my @fields = $record->field('024');
 		foreach my $field (@fields) {
 			my $indicator = $field->indicator(1);
 			my $upc = _normalize_match_point($field->subfield('a'));
@@ -195,7 +217,7 @@ sub _get_amazon_ean {
 		}
 	}
 	else { # assume unimarc if not marc21
-		@fields = $record->field('073');
+		my @fields = $record->field('073');
 		foreach my $field (@fields) {
 			my $upc = _normalize_match_point($field->subfield('a'));
 			if ($upc ne '') {
@@ -207,7 +229,7 @@ sub _get_amazon_ean {
 
 sub _normalize_match_point {
 	my $match_point = shift;
-	(my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
+	(my $normalized_match_point) = $match_point =~ /([\d-]+[X]*)/;
 	$normalized_match_point =~ s/-//g;
 
 	return $normalized_match_point;
diff --git a/t/Amazon.t b/t/Amazon.t
index 2d085a7..4e4fb58 100755
--- a/t/Amazon.t
+++ b/t/Amazon.t
@@ -6,9 +6,18 @@
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 4;
 
 BEGIN {
-        use_ok('C4::Amazon');
+    use_ok('C4::Amazon');
 }
 
+$C4::Amazon::override = {
+    AWSAccessKeyID => "0V5RRRRJZ3HR2RQFNHR2",
+    AmazonAssocTag => "kadabox-20",
+    AmazonLocale   => "US",
+};
+ok(get_amazon_details("0553380958"    ), "get_amazon_details: 10-digit"       );  # Snow Crash
+ok(get_amazon_details("055308853X"    ), "get_amazon_details: 10-digit (w/ X)");  # Snow Crash
+ok(get_amazon_details("978-0553380958"), "get_amazon_details: 13-digit"       );  # Snow Crash
+
-- 
1.5.5.GIT




More information about the Koha-patches mailing list