Test for LCCN splitting.
[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         &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::Amazon - Functions for retrieving Amazon.com content in Koha
61
62 =head1 FUNCTIONS
63
64 This module provides facilities for retrieving Amazon.com content in Koha
65
66 =head2 get_amazon_details
67
68 =over 4
69
70 my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
71
72 =back
73
74 Get editorial reviews, customer reviews, and similar products using Amazon Web Services.
75
76 =cut
77
78 sub get_amazon_details {
79     my ( $isbn, $record, $marcflavour ) = @_;
80
81     #normalize the ISBN
82     $isbn = _normalize_match_point ($isbn);
83
84     my $upc = _get_amazon_upc($record,$marcflavour);
85     my $ean = _get_amazon_ean($record,$marcflavour);
86
87     # warn "ISBN: $isbn | UPC: $upc | EAN: $ean";
88
89     my ( $id_type, $item_id);
90     if (defined($isbn) && length($isbn) == 13) { # if the isbn is 13-digit, search Amazon using EAN
91         $id_type = 'EAN';
92         $item_id = $isbn;
93     }
94     elsif ($isbn) {
95         $id_type = 'ASIN';
96         $item_id = $isbn;
97     }
98     elsif ($upc) {
99         $id_type = 'UPC';
100         $item_id = $upc;
101     }
102     elsif ($ean) {
103         $id_type = 'EAN';
104         $item_id = $upc;
105     }
106     else { # if no ISBN, UPC, or EAN exists, do not even attempt to query Amazon
107         return undef;
108     }
109
110     my $format = substr $record->leader(), 6, 1; # grab the item format to determine Amazon search index
111     my $formats;
112     $formats->{'a'} = 'Books';
113     $formats->{'g'} = 'Video';
114     $formats->{'j'} = 'Music';
115
116     my $search_index = $formats->{$format};
117
118     # Determine which content to grab in the request
119
120     # Determine correct locale
121     my $tld = get_amazon_tld();
122
123     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
124     my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
125
126     #grab the associates tag: mine is 'kadabox-20'
127     my $af_tag=C4::Context->preference('AmazonAssocTag');
128     my $response_group = "Similarities,EditorialReview,Reviews,ItemAttributes,Images";
129     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";
130     if ($id_type ne 'ASIN') {
131         $url .= "&SearchIndex=$search_index";
132     }
133     # warn $url;
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) ],
140     ) unless !$content;
141     return $response;
142 }
143
144 sub check_search_inside {
145         my $isbn = shift;
146         my $ua = LWP::UserAgent->new(
147         agent => "Mozilla/4.76 [en] (Win98; U)",
148         keep_alive => 1,
149         env_proxy => 1,
150         );
151         my $available = 1;
152         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
153         my $req = HTTP::Request->new(GET => $uri);
154         $req->header (
155                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
156                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
157                 'Accept-Language' => 'en-US' );
158         my $res = $ua->request($req);
159         my $content = $res->content();
160         if ($content =~ m/This book is temporarily unavailable/) {
161             undef $available;
162         }
163         return $available;
164 }
165
166 sub _get_amazon_upc {
167         my ($record,$marcflavour) = @_;
168         my (@fields,$upc);
169
170         if ($marcflavour eq 'MARC21') {
171                 @fields = $record->field('024');
172                 foreach my $field (@fields) {
173                         my $indicator = $field->indicator(1);
174                         my $upc = _normalize_match_point($field->subfield('a'));
175                         if ($indicator == 1 and $upc ne '') {
176                                 return $upc;
177                         }
178                 }
179         }
180         else { # assume unimarc if not marc21
181                 @fields = $record->field('072');
182                 foreach my $field (@fields) {
183                         my $upc = _normalize_match_point($field->subfield('a'));
184                         if ($upc ne '') {
185                                 return $upc;
186                         }
187                 }
188         }
189 }
190
191 sub _get_amazon_ean {
192         my ($record,$marcflavour) = @_;
193         my (@fields,$ean);
194
195         if ($marcflavour eq 'MARC21') {
196                 @fields = $record->field('024');
197                 foreach my $field (@fields) {
198                         my $indicator = $field->indicator(1);
199                         my $upc = _normalize_match_point($field->subfield('a'));
200                         if ($indicator == 3 and $upc ne '') {
201                                 return $upc;
202                         }
203                 }
204         }
205         else { # assume unimarc if not marc21
206                 @fields = $record->field('073');
207                 foreach my $field (@fields) {
208                         my $upc = _normalize_match_point($field->subfield('a'));
209                         if ($upc ne '') {
210                                 return $upc;
211                         }
212                 }
213         }
214 }
215
216 sub _normalize_match_point {
217         my $match_point = shift;
218         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
219         $normalized_match_point =~ s/-//g;
220
221         return $normalized_match_point;
222 }
223
224 1;
225 __END__
226
227 =head1 NOTES
228
229 =head1 AUTHOR
230
231 Joshua Ferraro <jmf@liblime.com>
232
233 =cut