Bug Fixing.
[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
23 use LWP::UserAgent;
24 use HTTP::Request::Common;
25
26 use strict;
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 $VERSION = 0.02;
32
33 =head1 NAME
34
35 C4::Amazon - Functions for retrieving Amazon.com content in Koha
36
37 =head1 FUNCTIONS
38
39 This module provides facilities for retrieving Amazon.com content in Koha
40
41 =cut
42
43 @ISA = qw(Exporter);
44
45 @EXPORT = qw(
46   &get_amazon_details
47   &check_search_inside
48 );
49
50 =head1 get_amazon_details($isbn);
51
52 =head2 $isbn is a isbn string
53
54 =cut
55
56 sub get_amazon_details {
57     my ( $isbn ) = @_;
58
59     #get rid of MARC cataloger's nonsense
60     $isbn =~ s/(p|-)//g;
61
62     # grab the developer's key: mine is 'ektostoukadou-20'
63     my $dev_key=C4::Context->preference('AmazonDevKey');
64
65     #grab the associates tag: mine is '0ZRY7YASKJS280T7YB02'
66     my $af_tag=C4::Context->preference('AmazonAssocTag');
67
68     my $asin=$isbn;
69     my $url = "http://xml.amazon.com/onca/xml3?t=$af_tag&dev-t=$dev_key&type=heavy&f=xml&AsinSearch=" . $asin;
70     my $content = get($url);
71     #warn $content;
72     warn "could not retrieve $url" unless $content;
73     my $xmlsimple = XML::Simple->new();
74     my $response = $xmlsimple->XMLin($content,
75     forcearray => [ qw(Details Product AvgCustomerRating CustomerReview) ],
76 );
77     return $response;
78 }
79
80 sub check_search_inside {
81         my $isbn = shift;
82         my $ua = LWP::UserAgent->new(
83         agent => "Mozilla/4.76 [en] (Win98; U)",
84         keep_alive => 1,
85         env_proxy => 1,
86         );
87         my $available = 1;
88         my $uri = "http://www.amazon.com/gp/reader/$isbn/ref=sib_dp_pt/002-7879865-0184864#reader-link";
89         my $req = HTTP::Request->new(GET => $uri);
90         $req->header (
91                 'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
92                 'Accept-Charset' => 'iso-8859-1,*,utf-8',
93                 'Accept-Language' => 'en-US' );
94         my $res = $ua->request($req);
95         my $content = $res->content();
96         if ($content =~ m/This book is temporarily unavailable/) {
97             undef $available;
98         }
99         return $available;
100 }
101
102 =head1 NOTES
103
104 =head1 AUTHOR
105
106 Joshua Ferraro <jmf@liblime.com>
107
108 =cut
109 1;