|
Lines 1-169
Link Here
|
| 1 |
package C4::External::BakerTaylor; |
|
|
| 2 |
|
| 3 |
# Copyright (C) 2008 LibLime |
| 4 |
# <jmf at liblime dot com> |
| 5 |
# |
| 6 |
# This file is part of Koha. |
| 7 |
# |
| 8 |
# Koha is free software; you can redistribute it and/or modify it |
| 9 |
# under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation; either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# Koha is distributed in the hope that it will be useful, but |
| 14 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 20 |
|
| 21 |
use Modern::Perl; |
| 22 |
use base 'Exporter'; |
| 23 |
|
| 24 |
BEGIN { |
| 25 |
our @EXPORT_OK = qw(availability content_cafe_url image_url link_url http_jacket_link); |
| 26 |
} |
| 27 |
|
| 28 |
use XML::Simple; |
| 29 |
use LWP::Simple qw( get ); |
| 30 |
|
| 31 |
use C4::Context; |
| 32 |
|
| 33 |
use vars qw(%EXPORT_TAGS $VERSION); |
| 34 |
|
| 35 |
BEGIN { |
| 36 |
$VERSION = 3.07.00.049; |
| 37 |
} |
| 38 |
|
| 39 |
# These variables are plack safe: they are initialized each time |
| 40 |
my ( $user, $pass, $agent, $image_url, $link_url ); |
| 41 |
|
| 42 |
sub _initialize { |
| 43 |
$user = ( @_ ? shift : C4::Context->preference('BakerTaylorUsername') ) || ''; # LL17984 |
| 44 |
$pass = ( @_ ? shift : C4::Context->preference('BakerTaylorPassword') ) || ''; # CC82349 |
| 45 |
$link_url = ( @_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL') ); |
| 46 |
$image_url = |
| 47 |
"https://contentcafe2.btol.com/ContentCafe/Jacket.aspx?UserID=$user&Password=$pass&Options=Y&Return=T&Type=S&Value="; |
| 48 |
$agent = "Koha/$VERSION [en] (Linux)"; |
| 49 |
|
| 50 |
#"Mozilla/4.76 [en] (Win98; U)", # if for some reason you want to go stealth, you might prefer this |
| 51 |
} |
| 52 |
|
| 53 |
sub image_url { |
| 54 |
_initialize(); |
| 55 |
( $user and $pass ) or return; |
| 56 |
my $isbn = ( @_ ? shift : '' ); |
| 57 |
$isbn =~ s/(p|-)//g; # sanitize |
| 58 |
return $image_url . $isbn; |
| 59 |
} |
| 60 |
|
| 61 |
sub link_url { |
| 62 |
_initialize(); |
| 63 |
my $isbn = ( @_ ? shift : '' ); |
| 64 |
$isbn =~ s/(p|-)//g; # sanitize |
| 65 |
$link_url or return; |
| 66 |
return $link_url . $isbn; |
| 67 |
} |
| 68 |
|
| 69 |
sub content_cafe_url { |
| 70 |
_initialize(); |
| 71 |
( $user and $pass ) or return; |
| 72 |
my $isbn = ( @_ ? shift : '' ); |
| 73 |
$isbn =~ s/(p|-)//g; # sanitize |
| 74 |
return |
| 75 |
"https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn"; |
| 76 |
} |
| 77 |
|
| 78 |
sub http_jacket_link { |
| 79 |
_initialize(); |
| 80 |
my $isbn = shift or return; |
| 81 |
$isbn =~ s/(p|-)//g; # sanitize |
| 82 |
my $image = availability($isbn); |
| 83 |
my $alt = "Buy this book"; |
| 84 |
$image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />); |
| 85 |
my $link = &link_url($isbn); |
| 86 |
unless ($link) { return $image || ''; } |
| 87 |
return sprintf qq(<a class="btlink" href="%s">%s</a>), $link, ( $image || $alt ); |
| 88 |
} |
| 89 |
|
| 90 |
sub availability { |
| 91 |
_initialize(); |
| 92 |
my $isbn = shift or return; |
| 93 |
( $user and $pass ) or return; |
| 94 |
$isbn =~ s/(p|-)//g; # sanitize |
| 95 |
my $url = |
| 96 |
"https://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn"; |
| 97 |
my $content = get($url); |
| 98 |
warn "could not retrieve $url" unless $content; |
| 99 |
my $xmlsimple = XML::Simple->new(); |
| 100 |
my $result = $xmlsimple->XMLin($content); |
| 101 |
|
| 102 |
if ( $result->{Error} ) { |
| 103 |
warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error}; |
| 104 |
} |
| 105 |
my $avail = $result->{Availability}; |
| 106 |
return ( $avail and $avail !~ /^false$/i ) ? &image_url($isbn) : 0; |
| 107 |
} |
| 108 |
|
| 109 |
1; |
| 110 |
|
| 111 |
__END__ |
| 112 |
|
| 113 |
=head1 NAME |
| 114 |
|
| 115 |
C4::External::BakerTaylor |
| 116 |
|
| 117 |
=head1 DESCRIPTION |
| 118 |
|
| 119 |
Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe". |
| 120 |
|
| 121 |
The settings for this module are controlled by System Preferences: |
| 122 |
|
| 123 |
These can be overridden for testing purposes using the initialize function. |
| 124 |
|
| 125 |
=head1 FUNCTIONS |
| 126 |
|
| 127 |
=head2 image_url |
| 128 |
|
| 129 |
Missing POD for image_url. |
| 130 |
|
| 131 |
=head2 link_url |
| 132 |
|
| 133 |
Missing POD for link_url. |
| 134 |
|
| 135 |
=head2 content_cafe_url |
| 136 |
|
| 137 |
Missing POD for content_cafe_url. |
| 138 |
|
| 139 |
=head2 http_jacket_link |
| 140 |
|
| 141 |
Missing POD for http_jacket_link. |
| 142 |
|
| 143 |
=head2 availability($isbn); |
| 144 |
|
| 145 |
$isbn is a isbn string |
| 146 |
|
| 147 |
=head1 NOTES |
| 148 |
|
| 149 |
A request with failed authentication might see this back from Baker + Taylor: |
| 150 |
|
| 151 |
<?xml version="1.0" encoding="utf-8"?> |
| 152 |
<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"> |
| 153 |
<Key Type="Undefined">string</Key> |
| 154 |
<Availability>false</Availability> |
| 155 |
<Error>Invalid UserID</Error> |
| 156 |
</InventoryAvailability> |
| 157 |
|
| 158 |
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. |
| 159 |
|
| 160 |
=head1 SEE ALSO |
| 161 |
|
| 162 |
LWP::UserAgent |
| 163 |
|
| 164 |
=head1 AUTHOR |
| 165 |
|
| 166 |
Joe Atzberger |
| 167 |
atz AT liblime DOT com |
| 168 |
|
| 169 |
=cut |