bug 3204: implement request signing for 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 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         get_amazon_tld
41     );
42 }
43
44
45 sub get_amazon_tld {
46     my %tld = (
47         CA => '.ca',
48         DE => '.de',
49         FR => '.fr',
50         JP => '.jp',
51         UK => '.co.uk',
52         US => '.com',
53     );
54
55     my $locale = C4::Context->preference('AmazonLocale');
56     my $tld = $tld{ $locale } || '.com'; # default top level domain is .com
57     return $tld;
58 }
59
60
61 =head1 NAME
62
63 C4::External::Amazon - Functions for retrieving Amazon.com content in Koha
64
65 =head2 FUNCTIONS
66
67 This module provides facilities for retrieving Amazon.com content in Koha
68
69 =over
70
71 =item get_amazon_detail( $isbn, $record, $marcflavour, $services )
72
73 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
74
75 Parameters:
76
77 =over
78
79 =item $isbn
80
81 Biblio record isbn
82
83 =item $record
84
85 Biblio MARC record
86
87 =item $marcflavour
88
89 MARC flavor, MARC21 or UNIMARC
90
91 =item $services
92
93 Requested Amazon services: A ref to an array. For example,
94 [ 'Similarities', 'EditorialReviews', 'Reviews' ].
95 No other service will be accepted. Services must be spelled exactly.
96 If no sercice is requested, AWS isn't called.
97
98 =back
99
100 =item get_amazon_tld()
101
102 Get Amazon Top Level Domain depending on Amazon local preference: AmazonLocal.
103 For example, if AmazonLocal is 'UK', returns '.co.uk'.
104
105 =back
106
107 =cut
108
109
110 sub get_amazon_details {
111     my ( $isbn, $record, $marcflavour, $aws_ref ) = @_;
112
113     return unless defined $aws_ref;
114     my @aws = @$aws_ref;
115     return if $#aws == -1;
116
117     # Normalize the fields
118     $isbn = GetNormalizedISBN($isbn);
119     my $upc = GetNormalizedUPC($record,$marcflavour);
120     my $ean = GetNormalizedEAN($record,$marcflavour);
121     # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
122
123     # Choose the appropriate and available item identifier
124     my ( $id_type, $item_id ) =
125         defined($isbn) && length($isbn) == 13 ? ( 'EAN',  $isbn ) :
126         $isbn                                 ? ( 'ASIN', $isbn ) :
127         $upc                                  ? ( 'UPC',  $upc  ) :
128         $ean                                  ? ( 'EAN',  $upc  ) : ( undef, undef );
129     return unless defined($id_type);
130
131     # grab the item format to determine Amazon search index
132     my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
133     my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
134
135         my $parameters={Service=>"AWSECommerceService" ,
136         "AWSAccessKeyId"=> C4::Context->preference('AWSAccessKeyID') ,
137         "Operation"=>"ItemLookup", 
138         "AssociateTag"=>  C4::Context->preference('AmazonAssocTag') ,
139         "Version"=>"2009-06-01",
140         "ItemId"=>$item_id,
141         "IdType"=>$id_type,
142         "ResponseGroup"=>  join( ',',  @aws ),
143         "Timestamp"=>strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
144                 };
145         $$parameters{"SearchIndex"} = $search_index if $id_type ne 'ASIN';
146         my @params;
147         while (my ($key,$value)=each %$parameters){
148                 push @params, qq{$key=}.uri_escape($value, "^A-Za-z0-9\-_.~" );
149         }
150
151     my $url =qq{http://webservices.amazon}.  get_amazon_tld(). 
152         "/onca/xml?".join("&",sort @params).qq{&Signature=}.uri_escape(SignRequest(@params),"^A-Za-z0-9\-_.~" );
153
154     my $content = get($url);
155     warn "could not retrieve $url" unless $content;
156     my $xmlsimple = XML::Simple->new();
157     my $response = $xmlsimple->XMLin(
158         $content,
159         forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
160     ) unless !$content;
161     return $response;
162 }
163
164 sub SignRequest{
165         my @params=@_;
166     my $tld=get_amazon_tld(); 
167     my $string = qq{ 
168 GET 
169 webservices.amazon$tld 
170 /onca/xml 
171 }.join("&",sort @params);
172         return hmac_sha256_base64($string,C4::Context->preference('AWSPrivateKey'));
173 }
174
175 sub check_search_inside {
176         my $isbn = shift;
177         my $ua = LWP::UserAgent->new(
178         agent => "Mozilla/4.76 [en] (Win98; U)",
179         keep_alive => 1,
180         env_proxy => 1,
181         );
182         my $available = 1;
183         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
184         my $req = HTTP::Request->new(GET => $uri);
185         $req->header (
186                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
187                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
188                 'Accept-Language' => 'en-US' );
189         my $res = $ua->request($req);
190         my $content = $res->content();
191         if ($content =~ m/This book is temporarily unavailable/) {
192             undef $available;
193         }
194         return $available;
195 }
196
197 1;
198 __END__
199
200 =head1 NOTES
201
202 =head1 AUTHOR
203
204 Joshua Ferraro <jmf@liblime.com>
205
206 =cut