fix for 2322: Failure to reach amazon.com to retrieve enhanced content causes fatal...
[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
27 use vars qw($VERSION @ISA @EXPORT);
28
29 BEGIN {
30     require Exporter;
31     $VERSION = 0.03;
32     @ISA = qw(Exporter);
33     @EXPORT = qw(
34         &get_amazon_details
35         &check_search_inside
36     );
37 }
38
39 =head1 NAME
40
41 C4::Amazon - Functions for retrieving Amazon.com content in Koha
42
43 =head1 FUNCTIONS
44
45 This module provides facilities for retrieving Amazon.com content in Koha
46
47 =head1 get_amazon_details($isbn);
48
49 =head2 $isbn is a isbn string
50
51 =cut
52
53 sub get_amazon_details {
54     my ( $isbn ) = @_;
55     #normalize the ISBN
56     $isbn =~ /(\d*[X]*)/;
57     $isbn = $1;
58
59     # Determine which content to grab in the request
60
61     # Determine correct locale
62     my $locale_hashref = {
63         CA => '.ca',
64         DE => '.de',
65         FR => '.fr',
66         JP => '.jp',
67         UK => '.co.uk',
68         US => '.com',
69     };
70
71     my $amazon_locale_syspref = C4::Context->preference('AmazonLocale');
72     my $tld = $locale_hashref->{$amazon_locale_syspref} || '.com'; # default top level domain is .com
73
74     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
75     my $aws_access_key_id = C4::Context->preference('AWSAccessKeyID');
76
77     #grab the associates tag: mine is 'kadabox-20'
78     my $af_tag=C4::Context->preference('AmazonAssocTag');
79     my $response_group = "Similarities,EditorialReview,Reviews,ItemAttributes";
80     my $asin=$isbn;
81     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=$asin&ResponseGroup=$response_group";
82     # warn $url;
83     my $content = get($url);
84     warn "could not retrieve $url" unless $content;
85     my $xmlsimple = XML::Simple->new();
86     my $response = $xmlsimple->XMLin(
87         $content,
88         forcearray => [ qw(SimilarProduct EditorialReview Review) ],
89     ) unless !$content;
90     return $response;
91 }
92
93 sub check_search_inside {
94         my $isbn = shift;
95         my $ua = LWP::UserAgent->new(
96         agent => "Mozilla/4.76 [en] (Win98; U)",
97         keep_alive => 1,
98         env_proxy => 1,
99         );
100         my $available = 1;
101         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
102         my $req = HTTP::Request->new(GET => $uri);
103         $req->header (
104                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
105                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
106                 'Accept-Language' => 'en-US' );
107         my $res = $ua->request($req);
108         my $content = $res->content();
109         if ($content =~ m/This book is temporarily unavailable/) {
110             undef $available;
111         }
112         return $available;
113 }
114
115 1;
116 __END__
117
118 =head1 NOTES
119
120 =head1 AUTHOR
121
122 Joshua Ferraro <jmf@liblime.com>
123
124 =cut