(bug #3025) multiple check-in of a multiple times reserved item go wrong
[koha.git] / C4 / Amazon.pm
1 package C4::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
25 use strict;
26 use warnings;
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 BEGIN {
31     require Exporter;
32     $VERSION = 0.03;
33     @ISA = qw(Exporter);
34     @EXPORT = qw(
35         &get_amazon_details
36         &check_search_inside
37     );
38 }
39
40 =head1 NAME
41
42 C4::Amazon - Functions for retrieving Amazon.com content in Koha
43
44 =head1 FUNCTIONS
45
46 This module provides facilities for retrieving Amazon.com content in Koha
47
48 =head2 get_amazon_details
49
50 =over 4
51
52 my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
53
54 =back
55
56 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
57
58 =cut
59
60 sub get_amazon_details {
61     my ( $isbn, $record, $marcflavour ) = @_;
62
63     #normalize the ISBN
64     $isbn = _normalize_match_point ($isbn);
65
66     my $upc = _get_amazon_upc($record,$marcflavour);
67     my $ean = _get_amazon_ean($record,$marcflavour);
68
69     # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
70
71     my ( $id_type, $item_id);
72     if (length($isbn) eq 13) { # if the isbn is 13-digit, search Amazon using EAN
73         $id_type = 'EAN';
74         $item_id = $isbn;
75     }
76     elsif ($isbn) {
77         $id_type = 'ASIN';
78         $item_id = $isbn;
79     }
80     elsif ($upc) {
81         $id_type = 'UPC';
82         $item_id = $upc;
83     }
84     elsif ($ean) {
85         $id_type = 'EAN';
86         $item_id = $upc;
87     }
88     else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
89         return undef;
90     }
91
92     my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
93     my $formats;
94     $formats->{'a'} = 'Books';
95     $formats->{'g'} = 'Video';
96     $formats->{'j'} = 'Music';
97
98     my $search_index = $formats->{$format};
99
100     # Determine which content to grab in the request
101
102     # Determine correct locale
103     my $locale_hashref = {
104         CA => '.ca',
105         DE => '.de',
106         FR => '.fr',
107         JP => '.jp',
108         UK => '.co.uk',
109         US => '.com',
110     };
111
112     my $amazon_locale_syspref = C4::Context->preference('AmazonLocale');
113     my $tld = $locale_hashref->{$amazon_locale_syspref} || '.com'; # default top level domain is .com
114
115     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
116     my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
117
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";
124     }
125     # warn $url;
126     my $content = get($url);
127     warn "could not retrieve $url" unless $content;
128     my $xmlsimple = XML::Simple->new();
129     my $response = $xmlsimple->XMLin(
130         $content,
131         forcearray => [ qw(SimilarProduct EditorialReview Review) ],
132     ) unless !$content;
133     return $response;
134 }
135
136 sub check_search_inside {
137         my $isbn = shift;
138         my $ua = LWP::UserAgent->new(
139         agent => "Mozilla/4.76 [en] (Win98; U)",
140         keep_alive => 1,
141         env_proxy => 1,
142         );
143         my $available = 1;
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);
146         $req->header (
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/) {
153             undef $available;
154         }
155         return $available;
156 }
157
158 sub _get_amazon_upc {
159         my ($record,$marcflavour) = @_;
160         my (@fields,$upc);
161
162         if ($marcflavour eq 'MARC21') {
163                 @fields = $record->field('024');
164                 foreach my $field (@fields) {
165                         my $indicator = $field->indicator(1);
166                         my $upc = _normalize_match_point($field->subfield('a'));
167                         if ($indicator == 1 and $upc ne '') {
168                                 return $upc;
169                         }
170                 }
171         }
172         else { # assume unimarc if not marc21
173                 @fields = $record->field('072');
174                 foreach my $field (@fields) {
175                         my $upc = _normalize_match_point($field->subfield('a'));
176                         if ($upc ne '') {
177                                 return $upc;
178                         }
179                 }
180         }
181 }
182
183 sub _get_amazon_ean {
184         my ($record,$marcflavour) = @_;
185         my (@fields,$ean);
186
187         if ($marcflavour eq 'MARC21') {
188                 @fields = $record->field('024');
189                 foreach my $field (@fields) {
190                         my $indicator = $field->indicator(1);
191                         my $upc = _normalize_match_point($field->subfield('a'));
192                         if ($indicator == 3 and $upc ne '') {
193                                 return $upc;
194                         }
195                 }
196         }
197         else { # assume unimarc if not marc21
198                 @fields = $record->field('073');
199                 foreach my $field (@fields) {
200                         my $upc = _normalize_match_point($field->subfield('a'));
201                         if ($upc ne '') {
202                                 return $upc;
203                         }
204                 }
205         }
206 }
207
208 sub _normalize_match_point {
209         my $match_point = shift;
210         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
211         $normalized_match_point =~ s/-//g;
212
213         return $normalized_match_point;
214 }
215
216 1;
217 __END__
218
219 =head1 NOTES
220
221 =head1 AUTHOR
222
223 Joshua Ferraro <jmf@liblime.com>
224
225 =cut