1 package C4::External::Amazon;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use HTTP::Request::Common;
29 use vars qw($VERSION @ISA @EXPORT);
43 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
47 This module provides facilities for retrieving Amazon.com content in Koha
49 =head2 get_amazon_details
53 my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
57 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
61 sub get_amazon_details {
62 my ( $isbn, $record, $marcflavour ) = @_;
64 # Normalize the fields
65 $isbn = GetNormalizedISBN($isbn);
66 my $upc = GetNormalizedUPC($record,$marcflavour);
67 my $ean = GetNormalizedEAN($record,$marcflavour);
69 # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
71 my ( $id_type, $item_id);
72 if (length($isbn) eq 13) { # if the isbn is 13-digit, search Amazon using EAN
88 else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
92 my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
94 $formats->{'a'} = 'Books';
95 $formats->{'g'} = 'Video';
96 $formats->{'j'} = 'Music';
98 my $search_index = $formats->{$format};
100 # Determine which content to grab in the request
102 # Determine correct locale
103 my $locale_hashref = {
112 my $amazon_locale_syspref = C4::Context->preference('AmazonLocale');
113 my $tld = $locale_hashref->{$amazon_locale_syspref} || '.com'; # default top level domain is .com
115 # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
116 my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
118 #grab the associates tag: mine is 'kadabox-20'
119 my $af_tag=C4::Context->preference('AmazonAssocTag');
120 my $response_group = "Similarities,EditorialReview,Reviews,ItemAttributes,Images";
121 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";
122 if ($id_type ne 'ASIN') {
123 $url .= "&SearchIndex=$search_index";
126 my $content = get($url);
127 warn "could not retrieve $url" unless $content;
128 my $xmlsimple = XML::Simple->new();
129 my $response = $xmlsimple->XMLin(
131 forcearray => [ qw(SimilarProduct EditorialReview Review) ],
136 sub check_search_inside {
138 my $ua = LWP::UserAgent->new(
139 agent => "Mozilla/4.76 [en] (Win98; U)",
144 my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
145 my $req = HTTP::Request->new(GET => $uri);
147 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
148 'Accept-Charset' => 'iso-8859-1,*,utf-8',
149 'Accept-Language' => 'en-US' );
150 my $res = $ua->request($req);
151 my $content = $res->content();
152 if ($content =~ m/This book is temporarily unavailable/) {
165 Joshua Ferraro <jmf@liblime.com>