Bugfix #2920 Avoid doing unecessary calls to Amazon Web Services
[koha.git] / C4 / External / BakerTaylor.pm
1 package C4::External::BakerTaylor;
2 # Copyright (C) 2008 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::Context;
25 use C4::Debug;
26
27 use strict;
28 use warnings;
29
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 use vars qw($user $pass $agent $image_url $link_url);
32
33 BEGIN {
34         require Exporter;
35         $VERSION = 0.01;
36         @ISA = qw(Exporter);
37         @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
38         %EXPORT_TAGS = (all=>\@EXPORT_OK);
39 }
40 INIT {
41         &initialize;
42 }
43
44 sub initialize {
45         $user     = (@_ ? shift : C4::Context->preference('BakerTaylorUsername')    ) || ''; # LL17984
46         $pass     = (@_ ? shift : C4::Context->preference('BakerTaylorPassword')    ) || ''; # CC82349
47         $link_url = (@_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL'));
48         # https://ocls.mylibrarybookstore.com/MLB/actions/searchHandler.do?nextPage=bookDetails&parentNum=10923&key=
49         $image_url = "http://contentcafe2.btol.com/buynow/Jacket.aspx?UserID=$user&Password=$pass&Product=";
50         $agent = "Koha/$VERSION [en] (Linux)";
51                         #"Mozilla/4.76 [en] (Win98; U)",        #  if for some reason you want to go stealth, you might prefer this
52 }
53
54 sub image_url (;$) {
55         ($user and $pass) or return undef;
56         my $isbn = (@_ ? shift : '');
57         $isbn =~ s/(p|-)//g;    # sanitize
58         return $image_url . $isbn;
59 }
60 sub link_url (;$) {
61         my $isbn = (@_ ? shift : '');
62         $isbn =~ s/(p|-)//g;    # sanitize
63         $link_url or return undef;
64         return $link_url . $isbn;
65 }
66 sub content_cafe_url ($) {
67         ($user and $pass) or return undef;
68         my $isbn = (@_ ? shift : '');
69         $isbn =~ s/(p|-)//g;    # sanitize
70         return "http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
71 }
72 sub http_jacket_link ($) {
73         my $isbn = shift or return undef;
74         $isbn =~ s/(p|-)//g;    # sanitize
75         my $image = availability($isbn);
76         my $alt = "Buy this book";
77         $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
78         my $link = &link_url($isbn);
79         unless ($link) {return $image || '';}
80         return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
81 }
82
83 sub availability ($) {
84         my $isbn = shift or return undef;
85         ($user and $pass) or return undef;
86         $isbn =~ s/(p|-)//g;    # sanitize
87         my $url = "http://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
88         $debug and warn __PACKAGE__ . " request:\n$url\n";
89         my $content = get($url);
90         $debug and print STDERR $content, "\n";
91         warn "could not retrieve $url" unless $content;
92         my $xmlsimple = XML::Simple->new();
93         my $result = $xmlsimple->XMLin($content);
94         if ($result->{Error}) {
95                 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
96         }
97         my $avail = $result->{Availability};
98         return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
99 }
100
101 1;
102
103 __END__
104
105 =head1 NAME
106
107 C4::External::BakerTaylor - Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
108 The settings for this module are controlled by System Preferences:
109
110 These can be overridden for testing purposes using the initialize function.
111
112 =head1 FUNCTIONS
113
114 =head1 availability($isbn);
115
116 =head2 $isbn is a isbn string
117
118 =head1 NOTES
119
120 A request with failed authentication might see this back from Baker + Taylor: 
121
122  <?xml version="1.0" encoding="utf-8"?>
123  <InventoryAvailability xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DateTime="2008-03-07T22:01:25.6520429-05:00" xmlns="http://ContentCafe2.btol.com">
124    <Key Type="Undefined">string</Key>
125    <Availability>false</Availability>
126    <Error>Invalid UserID</Error>
127  </InventoryAvailability>
128
129 Such response will trigger a warning for each request (potentially many).  Point being, do not leave this module configured with incorrect username and password in production.
130
131 =head1 SEE ALSO
132
133 C4::External::Amazon
134 LWP::UserAgent
135
136 =head1 AUTHOR
137
138 Joe Atzberger
139 atz AT liblime DOT com
140
141 =cut