Bugfix #2920 Avoid doing unecessary calls to Amazon Web Services
[koha.git] / C4 / External / Amazon.pm
1 package C4::External::Amazon;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use XML::Simple;
21 use LWP::Simple;
22 use LWP::UserAgent;
23 use HTTP::Request::Common;
24 use C4::Koha;
25
26 use strict;
27 use warnings;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 BEGIN {
32     require Exporter;
33     $VERSION = 0.03;
34     @ISA = qw(Exporter);
35     @EXPORT = qw(
36         get_amazon_details
37         get_amazon_tld
38     );
39 }
40
41
42 sub get_amazon_tld {
43     my %tld = (
44         CA => '.ca',
45         DE => '.de',
46         FR => '.fr',
47         JP => '.jp',
48         UK => '.co.uk',
49         US => '.com',
50     );
51
52     my $locale = C4::Context->preference('AmazonLocale');
53     my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
54     return $tld;
55 }
56
57
58 =head1 NAME
59
60 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
61
62 =head2 FUNCTIONS
63
64 This module provides facilities for retrieving Amazon.com content in Koha
65
66 =over
67
68 =item get_amazon_detail( $isbn, $record, $marcflavour, $services )
69
70 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
71
72 Parameters:
73
74 =over
75
76 =item $isbn
77
78 Biblio record isbn
79
80 =item $record
81
82 Biblio MARC record
83
84 =item $marcflavour
85
86 MARC flavor, MARC21 or UNIMARC
87
88 =item $services
89
90 Requested Amazon services: A ref to an array. For example,
91 [ 'Similarities', 'EditorialReviews', 'Reviews' ].
92 No other service will be accepted. Services must be spelled exactly.
93 If no sercice is requested, AWS isn't called.
94
95 =back
96
97 =item get_amazon_tld()
98
99 Get Amazon Top Level Domain depending on Amazon local preference: AmazonLocal.
100 For example, if AmazonLocal is 'UK', returns '.co.uk'.
101
102 =back
103
104 =cut
105
106
107 sub get_amazon_details {
108     my ( $isbn, $record, $marcflavour, $aws_ref ) = @_;
109
110     return unless defined $aws_ref;
111     my @aws = @$aws_ref;
112     return if $#aws == -1;
113
114     # Normalize the fields
115     $isbn = GetNormalizedISBN($isbn);
116     my $upc = GetNormalizedUPC($record,$marcflavour);
117     my $ean = GetNormalizedEAN($record,$marcflavour);
118
119     # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
120
121     my ( $id_type, $item_id);
122     if (defined($isbn) && length($isbn) == 13) { # if the isbn is 13-digit, search Amazon using EAN
123         $id_type = 'EAN';
124         $item_id = $isbn;
125     }
126     elsif ($isbn) {
127         $id_type = 'ASIN';
128         $item_id = $isbn;
129     }
130     elsif ($upc) {
131         $id_type = 'UPC';
132         $item_id = $upc;
133     }
134     elsif ($ean) {
135         $id_type = 'EAN';
136         $item_id = $upc;
137     }
138     else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
139         return undef;
140     }
141
142     # grab the item format to determine Amazon search index
143     # FIXME: This is MARC21 specific
144     my $format = substr $record->leader(), 6, 1; 
145     my $formats;
146     $formats->{'a'} = 'Books';
147     $formats->{'g'} = 'Video';
148     $formats->{'j'} = 'Music';
149
150     my $search_index = $formats->{$format};
151
152     # Determine which content to grab in the request
153
154     # Determine correct locale
155     my $tld = get_amazon_tld();
156
157     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
158     my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
159
160     #grab the associates tag: mine is 'kadabox-20'
161     my $af_tag=C4::Context->preference('AmazonAssocTag');
162     my $response_group = join( ',',  @aws );
163     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";
164     if ($id_type ne 'ASIN') {
165         $url .= "&SearchIndex=$search_index";
166     }
167     #warn $url;
168     my $content = get($url);
169     warn "could not retrieve $url" unless $content;
170     my $xmlsimple = XML::Simple->new();
171     my $response = $xmlsimple->XMLin(
172         $content,
173         forcearray => [ qw(SimilarProduct EditorialReview Review) ],
174     ) unless !$content;
175     return $response;
176 }
177
178 sub check_search_inside {
179         my $isbn = shift;
180         my $ua = LWP::UserAgent->new(
181         agent => "Mozilla/4.76 [en] (Win98; U)",
182         keep_alive => 1,
183         env_proxy => 1,
184         );
185         my $available = 1;
186         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
187         my $req = HTTP::Request->new(GET => $uri);
188         $req->header (
189                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
190                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
191                 'Accept-Language' => 'en-US' );
192         my $res = $ua->request($req);
193         my $content = $res->content();
194         if ($content =~ m/This book is temporarily unavailable/) {
195             undef $available;
196         }
197         return $available;
198 }
199
200 1;
201 __END__
202
203 =head1 NOTES
204
205 =head1 AUTHOR
206
207 Joshua Ferraro <jmf@liblime.com>
208
209 =cut