fix POD error introduced in last patch
[koha_gimpoz] / C4 / Amazon.pm
old mode 100755 (executable)
new mode 100644 (file)
index f99ff34..a9cb339
@@ -1,6 +1,6 @@
-
 package C4::Amazon;
-# Copyright 2004-2005 Joshua Ferraro (jmf at kados dot org)
+# Copyright (C) 2006 LibLime
+# <jmf at liblime dot com>
 #
 # This file is part of Koha.
 #
@@ -16,32 +16,27 @@ package C4::Amazon;
 # You should have received a copy of the GNU General Public License along with
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
-#
-# This module dynamically pulls amazon content into Koha.  It does not
-# store the data in Koha's database.  You'll need to get a developer's key
-# as well as an associate's tag to use it.
-# FIXME: need to write up more docs.
-#
-# To use this module you need to do three things:
-# 1. get a dev key and associate tag from Amazon
-# 2. uncomment the Amazon stuff in opac-detail.pl
-# 3. add the template variables to opac-detail.tmpl
-#    here's what's available: 
-#    ProductDescription
-#    ImageUrlMedium
-#    ListPrice
-#    url
-#    loop SimilarProducts (Product)
-#    loop Reviews (rating, Summary)
-#
+
 use XML::Simple;
 use LWP::Simple;
+use LWP::UserAgent;
+use HTTP::Request::Common;
+
 use strict;
-require Exporter;
+use warnings;
 
 use vars qw($VERSION @ISA @EXPORT);
 
-$VERSION = 0.02;
+BEGIN {
+    require Exporter;
+    $VERSION = 0.03;
+    @ISA = qw(Exporter);
+    @EXPORT = qw(
+        &get_amazon_details
+        &check_search_inside
+    );
+}
+
 =head1 NAME
 
 C4::Amazon - Functions for retrieving Amazon.com content in Koha
@@ -50,54 +45,181 @@ C4::Amazon - Functions for retrieving Amazon.com content in Koha
 
 This module provides facilities for retrieving Amazon.com content in Koha
 
-=cut
+=head2 get_amazon_details
 
-@ISA = qw(Exporter);
+=over 4
 
-@EXPORT = qw(
-  &get_amazon_details
-);
+my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
 
-=head1 get_amazon_details($isbn);
+=back
 
-=head2 $isbn is a isbn string
+Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
 
 =cut
 
 sub get_amazon_details {
+    my ( $isbn, $record, $marcflavour ) = @_;
+
+    #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 ( $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;
+    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) ],
+    ) unless !$content;
+    return $response;
+}
 
-my ( $isbn ) = @_;
-
-# insert your dev key here
-       $isbn =~ s/(p|-)//g;
-
-# insert your associates tag here
-       my $dev_key=C4::Context->preference('AmazonDevKey');
+sub check_search_inside {
+        my $isbn = shift;
+        my $ua = LWP::UserAgent->new(
+        agent => "Mozilla/4.76 [en] (Win98; U)",
+        keep_alive => 1,
+        env_proxy => 1,
+        );
+        my $available = 1;
+        my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
+        my $req = HTTP::Request->new(GET => $uri);
+        $req->header (
+                'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
+                'Accept-Charset' => 'iso-8859-1,*,utf-8',
+                'Accept-Language' => 'en-US' );
+        my $res = $ua->request($req);
+        my $content = $res->content();
+        if ($content =~ m/This book is temporarily unavailable/) {
+            undef $available;
+        }
+        return $available;
+}
 
-       #grab the associates tag: mine is '0ZRY7YASKJS280T7YB02'
-       my $af_tag=C4::Context->preference('AmazonAssocTag');
+sub _get_amazon_upc {
+       my ($record,$marcflavour) = @_;
+       my (@fields,$upc);
+
+       if ($marcflavour eq 'MARC21') {
+               @fields = $record->field('024');
+               foreach my $field (@fields) {
+                       my $indicator = $field->indicator(1);
+                       my $upc = _normalize_match_point($field->subfield('a'));
+                       if ($indicator == 1 and $upc ne '') {
+                               return $upc;
+                       }
+               }
+       }
+       else { # assume unimarc if not marc21
+               @fields = $record->field('072');
+               foreach my $field (@fields) {
+                       my $upc = _normalize_match_point($field->subfield('a'));
+                       if ($upc ne '') {
+                               return $upc;
+                       }
+               }
+       }
+}
 
-my $asin=$isbn;
+sub _get_amazon_ean {
+       my ($record,$marcflavour) = @_;
+       my (@fields,$ean);
+
+       if ($marcflavour eq 'MARC21') {
+               @fields = $record->field('024');
+               foreach my $field (@fields) {
+                       my $indicator = $field->indicator(1);
+                       my $upc = _normalize_match_point($field->subfield('a'));
+                       if ($indicator == 3 and $upc ne '') {
+                               return $upc;
+                       }
+               }
+       }
+       else { # assume unimarc if not marc21
+               @fields = $record->field('073');
+               foreach my $field (@fields) {
+                       my $upc = _normalize_match_point($field->subfield('a'));
+                       if ($upc ne '') {
+                               return $upc;
+                       }
+               }
+       }
+}
 
-# old way from command line: shift @ARGV or die "Usage:perl amazon_http.ol <asin>\n";
+sub _normalize_match_point {
+       my $match_point = shift;
+       (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
+       $normalized_match_point =~ s/-//g;
 
-#my $url = "http://xml.amazon.com/onca/xml3?t=" . $af_tag .
-#      "&dev-t=" . $dev_key .
-#      "&type=heavy&f=xml&" .
-#      "AsinSearch=" . $asin;
-       my $url = "http://xml.amazon.com/onca/xml3?t=$af_tag&dev-t=$dev_key&type=heavy&f=xml&AsinSearch=" . $asin;
-my $content = get($url);
-       warn "could not retrieve $url" unless $content;
-my $xmlsimple = XML::Simple->new();
-my $response = $xmlsimple->XMLin($content,
-  forcearray => [ qw(Details Product AvgCustomerRating CustomerReview) ],
-);
-return $response;
+       return $normalized_match_point;
 }
 
+1;
+__END__
+
 =head1 NOTES
 
 =head1 AUTHOR
 
 Joshua Ferraro <jmf@liblime.com>
+
 =cut