From 856ca6d995b8723e3fc17eef70ee738fe24219dc Mon Sep 17 00:00:00 2001 From: Dan Scott Date: Tue, 1 Nov 2011 13:29:25 -0400 Subject: [PATCH] Force Amazon AC requests to use 10-digit ISBNs Inspired by a patch submitted by Ian Bays 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 --- .../src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm index 77c6bef531..9e2c13c595 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Amazon.pm @@ -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); } -- 2.11.0