From: Joshua Ferraro <[email protected]>
Signed-off-by: Daniel Sweeney <[email protected]> --- C4/Amazon.pm | 225 -------------------------------------------- C4/External/Amazon.pm | 225 ++++++++++++++++++++++++++++++++++++++++++++ C4/External/BakerTaylor.pm | 2 +- catalogue/detail.pl | 2 +- opac/opac-ISBDdetail.pl | 2 +- opac/opac-detail.pl | 2 +- t/Amazon.t | 2 +- 7 files changed, 230 insertions(+), 230 deletions(-) delete mode 100644 C4/Amazon.pm create mode 100644 C4/External/Amazon.pm diff --git a/C4/Amazon.pm b/C4/Amazon.pm deleted file mode 100644 index a9cb339..0000000 --- a/C4/Amazon.pm +++ /dev/null @@ -1,225 +0,0 @@ -package C4::Amazon; -# Copyright (C) 2006 LibLime -# <jmf at liblime dot com> -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. -# -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# 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 - -use XML::Simple; -use LWP::Simple; -use LWP::UserAgent; -use HTTP::Request::Common; - -use strict; -use warnings; - -use vars qw($VERSION @ISA @EXPORT); - -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 - -=head1 FUNCTIONS - -This module provides facilities for retrieving Amazon.com content in Koha - -=head2 get_amazon_details - -=over 4 - -my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour ); - -=back - -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; -} - -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; -} - -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 - -Joshua Ferraro <[email protected]> - -=cut diff --git a/C4/External/Amazon.pm b/C4/External/Amazon.pm new file mode 100644 index 0000000..f86e197 --- /dev/null +++ b/C4/External/Amazon.pm @@ -0,0 +1,225 @@ +package C4::External::Amazon; +# Copyright (C) 2006 LibLime +# <jmf at liblime dot com> +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# 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 + +use XML::Simple; +use LWP::Simple; +use LWP::UserAgent; +use HTTP::Request::Common; + +use strict; +use warnings; + +use vars qw($VERSION @ISA @EXPORT); + +BEGIN { + require Exporter; + $VERSION = 0.03; + @ISA = qw(Exporter); + @EXPORT = qw( + &get_amazon_details + &check_search_inside + ); +} + +=head1 NAME + +C4::External::Amazon - Functions for retrieving Amazon.com content in Koha + +=head1 FUNCTIONS + +This module provides facilities for retrieving Amazon.com content in Koha + +=head2 get_amazon_details + +=over 4 + +my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour ); + +=back + +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; +} + +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; +} + +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 + +Joshua Ferraro <[email protected]> + +=cut diff --git a/C4/External/BakerTaylor.pm b/C4/External/BakerTaylor.pm index 39005e4..7774073 100644 --- a/C4/External/BakerTaylor.pm +++ b/C4/External/BakerTaylor.pm @@ -130,7 +130,7 @@ Such response will trigger a warning for each request (potentially many). Point =head1 SEE ALSO -C4::Amazon +C4::External::Amazon LWP::UserAgent =head1 AUTHOR diff --git a/catalogue/detail.pl b/catalogue/detail.pl index 393ec16..363f2dc 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -33,7 +33,7 @@ use C4::Reserves; use C4::Members; use C4::Serials; use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn); -use C4::Amazon; +use C4::External::Amazon; # use Smart::Comments; diff --git a/opac/opac-ISBDdetail.pl b/opac/opac-ISBDdetail.pl index bb3b570..baaef43 100755 --- a/opac/opac-ISBDdetail.pl +++ b/opac/opac-ISBDdetail.pl @@ -229,7 +229,7 @@ $template->param( ## Amazon.com stuff #not used unless preference set if ( C4::Context->preference("AmazonContent") == 1 ) { - use C4::Amazon; + use C4::External::Amazon; $dat->{'amazonisbn'} = $dat->{'isbn'}; $dat->{'amazonisbn'} =~ s|-||g; diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 24f59b0..01c272f 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -32,7 +32,7 @@ use C4::Circulation; use C4::Tags qw(get_tags); use C4::Dates qw/format_date/; use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn); -use C4::Amazon; +use C4::External::Amazon; use C4::Review; use C4::Serials; use C4::Members; diff --git a/t/Amazon.t b/t/Amazon.t index 2d085a7..a0ca21b 100755 --- a/t/Amazon.t +++ b/t/Amazon.t @@ -9,6 +9,6 @@ use warnings; use Test::More tests => 1; BEGIN { - use_ok('C4::Amazon'); + use_ok('C4::External::Amazon'); } -- 1.5.6.5 _______________________________________________ Koha-patches mailing list [email protected] http://lists.koha.org/mailman/listinfo/koha-patches
