Force Amazon AC requests to use 10-digit ISBNs
authorDan Scott <dscott@laurentian.ca>
Tue, 1 Nov 2011 17:29:25 +0000 (13:29 -0400)
committerDan Scott <dscott@laurentian.ca>
Tue, 1 Nov 2011 17:56:18 +0000 (13:56 -0400)
Inspired by a patch submitted by Ian Bays <ian.bays@ptfs-europe.com>
via https://bugs.launchpad.net/evergreen/+bug/870171, this patch takes
the incoming key and, if it is longer than 10 characters (a 10-digit
ISBN or Amazon ID), attempts to normalize it and return a 10-digit ISBN.

We don't normalize all incoming keys because some sites are horribly
abusing the 020 MARC field to include an Amazon ID instead of an ISBN -
but that works in practice and we'd rather not break working
installations if we can avoid it.

Signed-off-by: Dan Scott <dscott@laurentian.ca>

Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm

index 77c6bef..9e2c13c 100644 (file)
@@ -6,6 +6,7 @@ use OpenILS::WWW::AddedContent;
 use OpenSRF::Utils::JSON;
 use OpenSRF::EX qw/:try/;
 use XML::LibXML;
+use Business::ISBN;
 
 my $AC = 'OpenILS::WWW::AddedContent';
 
@@ -66,6 +67,22 @@ sub fetch_content {
 sub fetch_response {
     my( $self, $page, $key ) = @_;
     my $uname = $self->userid;
+
+    # Some sites have entered Amazon IDs in 020 $a, thus we cannot
+    # simply pass all incoming keys to Business::ISBN for normalization
+    if (length($key) > 10) {
+        # Use Business::ISBN to normalize the incoming ISBN
+        my $isbn = Business::ISBN->new( $key );
+        if (!defined $isbn) {
+            $logger->warning("'$key' is not a valid ISBN");
+            return 0;
+        }
+
+        # Amazon prefers ISBN10
+        $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';
+        $key = $isbn->as_string([]);
+    }
+
     my $url = $self->base_url . "$key.01.$page";
     return $AC->get_url($url);
 }