X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FAmazon.pm;h=34f98c2e31762c4784204f2053955717411a14f6;hb=4c771727b70ff5f2cb54cdbccd5186f92d1577ef;hp=ae7573fe9e27aed8718f373e5c358597a306ec88;hpb=0d7b552dec8fe611c70045db841317382b67e674;p=koha.git diff --git a/C4/Amazon.pm b/C4/Amazon.pm old mode 100755 new mode 100644 index ae7573fe9e..34f98c2e31 --- a/C4/Amazon.pm +++ b/C4/Amazon.pm @@ -19,16 +19,41 @@ package C4::Amazon; 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 + &get_amazon_tld + ); +} + + +sub get_amazon_tld { + my %tld = ( + CA => '.ca', + DE => '.de', + FR => '.fr', + JP => '.jp', + UK => '.co.uk', + US => '.com', + ); + + my $locale = C4::Context->preference('AmazonLocale'); + my $tld = $tld{ $locale } || '.com'; # default top level domain is .com + return $tld; +} + =head1 NAME @@ -38,42 +63,81 @@ 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 - &check_search_inside -); +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 ) = @_; - - #get rid of MARC cataloger's nonsense - $isbn =~ s/(p|-)//g; - - # grab the developer's key: mine is 'ektostoukadou-20' - my $dev_key=C4::Context->preference('AmazonDevKey'); - - #grab the associates tag: mine is '0ZRY7YASKJS280T7YB02' + 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 (defined($isbn) && 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; + } + 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 $tld = get_amazon_tld(); + + # 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 $asin=$isbn; - my $url = "http://xml.amazon.com/onca/xml3?t=$af_tag&dev-t=$dev_key&type=heavy&f=xml&AsinSearch=" . $asin; + 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 $content; warn "could not retrieve $url" unless $content; my $xmlsimple = XML::Simple->new(); - my $response = $xmlsimple->XMLin($content, - forcearray => [ qw(Details Product AvgCustomerRating CustomerReview) ], -); + my $response = $xmlsimple->XMLin( + $content, + forcearray => [ qw(SimilarProduct EditorialReview Review) ], + ) unless !$content; return $response; } @@ -99,6 +163,67 @@ sub check_search_inside { return $available; } +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; + } + } + } +} + +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; + } + } + } +} + +sub _normalize_match_point { + my $match_point = shift; + (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/; + $normalized_match_point =~ s/-//g; + + return $normalized_match_point; +} + +1; +__END__ + =head1 NOTES =head1 AUTHOR @@ -106,4 +231,3 @@ sub check_search_inside { Joshua Ferraro =cut -1;