[Koha-patches] [PATCH] Bugfix #2920 Avoid doing unecessary calls to Amazon Web Services

Frédéric Demians f.demians at tamil.fr
Sat Apr 25 09:24:54 CEST 2009


This patch modifies how AWS is called. AWS is now called
depending on syspref. It works completly for OPAC; it
has to be refined for intranet.

For OPAC:

  * If OPACAmazonReviews is set, AWS EditorialReview and
    Reviews (users) are grabed.
  * If OPACAmazonSimilarItems is set, AWS Similarities info
    are grabed.
  * If nothing is asked, AWS is not called anymore,
    sparing server bandwidth.

For intranet:

  It works as it used to work. AWS is called if AmazonEnabled
  is set whatever how other syspref are set.

  TODO:

    * Add a AmazonReviews syspref
    * Request Amazon content depending on AmazonSimilarities
      and AmazonReviews syspref

DOCUMENTATION:

  It should be explained that Amazon services related syspref
  have two levels:

  * AmazonEnable / OPACAmazonEnable
  * Other: OPACAmazonReviews (new), OPACAmazonSimilarProduct,
    OPACAmazonCover
---
 C4/External/Amazon.pm |   58 ++++++++++++++++++++++++++++++++++++++----------
 catalogue/detail.pl   |    4 +-
 opac/opac-detail.pl   |   59 +++++++++++++++++++++++++++++-------------------
 3 files changed, 84 insertions(+), 37 deletions(-)

diff --git a/C4/External/Amazon.pm b/C4/External/Amazon.pm
index 4c64e9d..78cdadf 100644
--- a/C4/External/Amazon.pm
+++ b/C4/External/Amazon.pm
@@ -33,9 +33,8 @@ BEGIN {
     $VERSION = 0.03;
     @ISA = qw(Exporter);
     @EXPORT = qw(
-        &get_amazon_details
-        &check_search_inside
-        &get_amazon_tld
+        get_amazon_details
+        get_amazon_tld
     );
 }
 
@@ -60,24 +59,57 @@ sub get_amazon_tld {
 
 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
 
-=over 4
+=item get_amazon_detail( $isbn, $record, $marcflavour, $services )
 
-my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
+Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
+
+Parameters:
+
+=over
+
+=item $isbn
+
+Biblio record isbn
+
+=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);
@@ -107,7 +139,9 @@ sub get_amazon_details {
 	return undef;
     }
 
-    my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
+    # grab the item format to determine Amazon search index
+    # FIXME: This is MARC21 specific
+    my $format = substr $record->leader(), 6, 1; 
     my $formats;
     $formats->{'a'} = 'Books';
     $formats->{'g'} = 'Video';
@@ -125,12 +159,12 @@ sub get_amazon_details {
 
     #grab the associates tag: mine is 'kadabox-20'
     my $af_tag=C4::Context->preference('AmazonAssocTag');
-    my $response_group = "Similarities,EditorialReview,Reviews,ItemAttributes,Images";
+    my $response_group = join( ',',  @aws );
     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;
+    #warn $url;
     my $content = get($url);
     warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index 663f770..aaf07c7 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -224,8 +224,8 @@ if (C4::Context->preference("FRBRizeEditions")==1) {
 }
 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
     my $similar_products_exist;
-    my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
-    my $item_attributes   = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
+    my @aws = qw( Similarities EditorialReview Reviews );
+    my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@aws );
     my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
     my @similar_products;
     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index ae6bc41..f98f30e 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -276,33 +276,46 @@ if (C4::Context->preference("OPACFRBRizeEditions")==1) {
 # Amazon.com Stuff
 if ( C4::Context->preference("OPACAmazonEnabled") ) {
     $template->param( AmazonTld => get_amazon_tld() );
-    $template->param( OPACAmazonReviews => C4::Context->preference("OPACAmazonReviews") );
-}
-if ( C4::Context->preference("OPACAmazonEnabled") && C4::Context->preference("OPACAmazonSimilarItems") ) {
+    my $amazon_reviews  = C4::Context->preference("OPACAmazonReviews");
+    my $amazon_similars = C4::Context->preference("OPACAmazonSimilarItems");
+    my @services;
+    if ( $amazon_reviews ) {
+        $template->param( OPACAmazonReviews => 1 );
+        push( @services, 'EditorialReview', 'Reviews' );
+    }
+    if ( $amazon_similars ) {
+        $template->param( OPACAmazonSimilarItems => 1 );
+        push( @services, 'Similarities' );
+    }
+    my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
     my $similar_products_exist;
-    my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
-    my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
-    my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
-    for my $one_review (@$customer_reviews) {
-        $one_review->{Date} = format_date($one_review->{Date});
+    if ( $amazon_reviews ) {
+        my $item = $amazon_details->{Items}->{Item};
+        my $customer_reviews = \@{ $item->{CustomerReviews}->{Review} };
+        for my $one_review ( @$customer_reviews ) {
+            $one_review->{Date} = format_date($one_review->{Date});
+        }
+        my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
+        my $average_rating = $item->{CustomerReviews}->{AverageRating} || 0;
+        $template->param( amazon_average_rating    => $average_rating * 20);
+        $template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews );
+        $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
     }
-    my @similar_products;
-    for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
-        # do we have any of these isbns in our collection?
-        my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
-        # verify that there is at least one similar item
-        if (scalar(@$similar_biblionumbers)){
-            $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
-            push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
+    if ( $amazon_similars ) {
+        my $item = $amazon_details->{Items}->{Item};
+        my @similar_products;
+        for my $similar_product (@{ $item->{SimilarProducts}->{SimilarProduct} }) {
+            # do we have any of these isbns in our collection?
+            my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
+            # verify that there is at least one similar item
+            if (scalar(@$similar_biblionumbers)){
+                $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
+                push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
+            }
         }
+        $template->param( OPACAmazonSimilarItems => $similar_products_exist );
+        $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
     }
-    my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
-    my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0;
-    $template->param( OPACAmazonSimilarItems => $similar_products_exist );
-    $template->param( amazon_average_rating => $average_rating * 20);
-    $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
-    $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
-    $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
 }
 
 my $syndetics_elements;
-- 
1.5.6.5




More information about the Koha-patches mailing list