[followup](bug #3348) fix spent values and spent resume
[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 use URI::Escape;
26 use POSIX;
27 use Digest::SHA qw(hmac_sha256_base64);
28
29 use strict;
30 use warnings;
31
32 use vars qw($VERSION @ISA @EXPORT);
33
34 BEGIN {
35     require Exporter;
36     $VERSION = 0.03;
37     @ISA = qw(Exporter);
38     @EXPORT = qw(
39         &get_amazon_details
40         &check_search_inside
41         &get_amazon_tld
42     );
43 }
44
45
46 sub get_amazon_tld {
47     my %tld = (
48         CA => '.ca',
49         DE => '.de',
50         FR => '.fr',
51         JP => '.jp',
52         UK => '.co.uk',
53         US => '.com',
54     );
55
56     my $locale = C4::Context->preference('AmazonLocale');
57     my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
58     return $tld;
59 }
60
61
62 =head1 NAME
63
64 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
65
66 =head1 FUNCTIONS
67
68 This module provides facilities for retrieving Amazon.com content in Koha
69
70 =head2 get_amazon_details
71
72 =over 4
73
74 my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
75
76 =back
77
78 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
79
80 =cut
81
82 sub get_amazon_details {
83     my ( $isbn, $record, $marcflavour,$awsref ) = @_;
84
85    return unless defined $awsref;
86    my @aws = @$awsref;
87    return if $#aws == -1;
88
89     #normalize the ISBN
90     $isbn = _normalize_match_point ($isbn);
91
92     my $upc = _get_amazon_upc($record,$marcflavour);
93     my $ean = _get_amazon_ean($record,$marcflavour);
94
95     # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
96
97     # Choose the appropriate and available item identifier
98     my ( $id_type, $item_id ) =
99         defined($isbn) && length($isbn) == 13 ? ( 'EAN',  $isbn ) :
100         $isbn                                 ? ( 'ASIN', $isbn ) :
101         $upc                                  ? ( 'UPC',  $upc  ) :
102         $ean                                  ? ( 'EAN',  $upc  ) : ( undef, undef );
103     return unless defined($id_type);
104
105     # grab the item format to determine Amazon search index
106     my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
107     my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
108
109     my $parameters={Service=>"AWSECommerceService" ,
110         "AWSAccessKeyId"=> C4::Context->preference('AWSAccessKeyID') ,
111         "Operation"=>"ItemLookup", 
112         "AssociateTag"=>  C4::Context->preference('AmazonAssocTag') ,
113         "Version"=>"2009-06-01",
114         "ItemId"=>$item_id,
115         "IdType"=>$id_type,
116         "ResponseGroup"=>  join( ',',  @aws ),
117         "Timestamp"=>strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
118     };
119     $$parameters{"SearchIndex"} = $search_index if $id_type ne 'ASIN';
120     my @params;
121     while (my ($key,$value)=each %$parameters){
122         push @params, qq{$key=}.uri_escape($value, "^A-Za-z0-9\-_.~" );
123     }
124
125     my $url;
126     if (C4::Context->preference('AWSPrivateKey')) {
127         $url = qq{http://webservices.amazon} . get_amazon_tld() . 
128                "/onca/xml?" . join("&",sort @params) . qq{&Signature=} . uri_escape(SignRequest(@params),"^A-Za-z0-9\-_.~" );
129     } else {
130         $url = qq{http://webservices.amazon} . get_amazon_tld() .  "/onca/xml?" .join("&",sort @params);
131         warn "MUST set AWSPrivateKey syspref after 2009-08-15 in order to access Amazon web services";
132     }
133
134     my $content = get($url);
135     warn "could not retrieve $url" unless $content;
136     my $xmlsimple = XML::Simple->new();
137     my $response = $xmlsimple->XMLin(
138         $content,
139         forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
140     ) unless !$content;
141     return $response;
142 }
143
144 sub SignRequest{
145     my @params=@_;
146     my $tld=get_amazon_tld(); 
147     my $string = qq{GET\nwebservices.amazon$tld\n/onca/xml\n} . join("&",sort @params);
148     return hmac_sha256_base64($string,C4::Context->preference('AWSPrivateKey')) . '=';
149 }
150
151 sub check_search_inside {
152         my $isbn = shift;
153         my $ua = LWP::UserAgent->new(
154         agent => "Mozilla/4.76 [en] (Win98; U)",
155         keep_alive => 1,
156         env_proxy => 1,
157         );
158         my $available = 1;
159         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
160         my $req = HTTP::Request->new(GET => $uri);
161         $req->header (
162                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
163                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
164                 'Accept-Language' => 'en-US' );
165         my $res = $ua->request($req);
166         my $content = $res->content();
167         if ($content =~ m/This book is temporarily unavailable/) {
168             undef $available;
169         }
170         return $available;
171 }
172
173 sub _get_amazon_upc {
174         my ($record,$marcflavour) = @_;
175         my (@fields,$upc);
176
177         if ($marcflavour eq 'MARC21') {
178                 @fields = $record->field('024');
179                 foreach my $field (@fields) {
180                         my $indicator = $field->indicator(1);
181                         my $upc = _normalize_match_point($field->subfield('a'));
182                         if ($indicator == 1 and $upc ne '') {
183                                 return $upc;
184                         }
185                 }
186         }
187         else { # assume unimarc if not marc21
188                 @fields = $record->field('072');
189                 foreach my $field (@fields) {
190                         my $upc = _normalize_match_point($field->subfield('a'));
191                         if ($upc ne '') {
192                                 return $upc;
193                         }
194                 }
195         }
196 }
197
198 sub _get_amazon_ean {
199         my ($record,$marcflavour) = @_;
200         my (@fields,$ean);
201
202         if ($marcflavour eq 'MARC21') {
203                 @fields = $record->field('024');
204                 foreach my $field (@fields) {
205                         my $indicator = $field->indicator(1);
206                         my $upc = _normalize_match_point($field->subfield('a'));
207                         if ($indicator == 3 and $upc ne '') {
208                                 return $upc;
209                         }
210                 }
211         }
212         else { # assume unimarc if not marc21
213                 @fields = $record->field('073');
214                 foreach my $field (@fields) {
215                         my $upc = _normalize_match_point($field->subfield('a'));
216                         if ($upc ne '') {
217                                 return $upc;
218                         }
219                 }
220         }
221 }
222
223 sub _normalize_match_point {
224         my $match_point = shift;
225         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
226         $normalized_match_point =~ s/-//g;
227
228         return $normalized_match_point;
229 }
230
231 1;
232 __END__
233
234 =head1 NOTES
235
236 =head1 AUTHOR
237
238 Joshua Ferraro <jmf@liblime.com>
239
240 =cut