1 package C4
::External
::Syndetics
;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
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
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use HTTP
::Request
::Common
;
29 use vars
qw($VERSION @ISA @EXPORT);
33 $VERSION = 3.07.00.049;
37 &get_syndetics_summary
39 &get_syndetics_editions
40 &get_syndetics_excerpt
41 &get_syndetics_reviews
46 # package-level variable
47 my $parser = XML
::LibXML
->new();
51 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
55 This module provides facilities for retrieving Syndetics.com content in Koha
57 =head2 get_syndetics_summary
59 my $syndetics_summary= &get_syndetics_summary( $isbn );
61 Get Summary data from Syndetics
65 sub get_syndetics_index
{
66 my ( $isbn, $upc, $oclc ) = @_;
68 return unless ( $isbn || $upc || $oclc );
70 my $response = _fetch_syndetics_content
('INDEX.XML', $isbn, $upc, $oclc);
71 unless ($response->content_type =~ /xml/) {
75 my $content = $response->content;
76 my $xmlsimple = XML
::Simple
->new();
77 $response = $xmlsimple->XMLin(
81 my $syndetics_elements;
82 for my $available_type ('SUMMARY','TOC','FICTION','AWARDS1','SERIES1','SPSUMMARY','SPREVIEW', 'AVPROFILE', 'AVSUMMARY','DBCHAPTER','LJREVIEW','PWREVIEW','SLJREVIEW','CHREVIEW','BLREVIEW','HBREVIEW','KIREVIEW','CRITICASREVIEW','ANOTES') {
83 if (exists $response->{$available_type} && $response->{$available_type} =~ /$available_type/) {
84 $syndetics_elements->{$available_type} = $available_type;
88 return $syndetics_elements if $syndetics_elements;
91 sub get_syndetics_summary
{
92 my ( $isbn, $upc, $oclc, $syndetics_elements ) = @_;
94 my $summary_type = exists($syndetics_elements->{'AVSUMMARY'}) ?
'AVSUMMARY.XML' : 'SUMMARY.XML';
95 my $response = _fetch_syndetics_content
($summary_type, $isbn, $upc, $oclc);
96 unless ($response->content_type =~ /xml/) {
100 my $content = $response->content;
104 my $doc = $parser->parse_string($content);
105 $summary = $doc->findvalue('//Fld520');
108 warn "Error parsing Syndetics $summary_type";
110 return $summary if $summary;
113 sub get_syndetics_toc
{
114 my ( $isbn,$upc,$oclc ) = @_;
116 my $response = _fetch_syndetics_content
('TOC.XML', $isbn, $upc, $oclc);
117 unless ($response->content_type =~ /xml/) {
121 my $content = $response->content;
122 my $xmlsimple = XML
::Simple
->new();
123 $response = $xmlsimple->XMLin(
125 forcearray
=> [ qw(Fld970) ],
127 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
129 $toc = \@
{$response->{VarFlds
}->{VarDFlds
}->{SSIFlds
}->{Fld970
}} if $response;
133 sub get_syndetics_excerpt
{
134 my ( $isbn,$upc,$oclc ) = @_;
136 my $response = _fetch_syndetics_content
('DBCHAPTER.XML', $isbn, $upc, $oclc);
137 unless ($response->content_type =~ /xml/) {
141 my $content = $response->content;
142 my $xmlsimple = XML
::Simple
->new();
143 $response = $xmlsimple->XMLin(
145 forcearray
=> [ qw(Fld520) ],
147 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
149 $excerpt = \@
{$response->{VarFlds
}->{VarDFlds
}->{Notes
}->{Fld520
}} if $response;
150 return XMLout
($excerpt, NoEscape
=> 1) if $excerpt;
153 sub get_syndetics_reviews
{
154 my ( $isbn,$upc,$oclc,$syndetics_elements ) = @_;
157 my $review_sources = [
158 {title
=> 'Library Journal Review', file
=> 'LJREVIEW.XML', element
=> 'LJREVIEW'},
159 {title
=> 'Publishers Weekly Review', file
=> 'PWREVIEW.XML', element
=> 'PWREVIEW'},
160 {title
=> 'School Library Journal Review', file
=> 'SLJREVIEW.XML', element
=> 'SLJREVIEW'},
161 {title
=> 'CHOICE Review', file
=> 'CHREVIEW.XML', element
=> 'CHREVIEW'},
162 {title
=> 'Booklist Review', file
=> 'BLREVIEW.XML', element
=> 'BLREVIEW'},
163 {title
=> 'Horn Book Review', file
=> 'HBREVIEW.XML', element
=> 'HBREVIEW'},
164 {title
=> 'Kirkus Book Review', file
=> 'KIREVIEW.XML', element
=> 'KIREVIEW'},
165 {title
=> 'Criticas Review', file
=> 'CRITICASREVIEW.XML', element
=> 'CRITICASREVIEW'},
166 {title
=> 'Spanish Review', file
=> 'SPREVIEW.XML', element
=> 'SPREVIEW'},
169 for my $source (@
$review_sources) {
170 if ($syndetics_elements->{$source->{element
}} and $source->{element
} =~ $syndetics_elements->{$source->{element
}}) {
173 #warn "Skipping $source->{element} doesn't match $syndetics_elements->{$source->{element}} \n";
176 my $response = _fetch_syndetics_content
($source->{file
}, $isbn, $upc, $oclc);
177 unless ($response->content_type =~ /xml/) {
181 my $content = $response->content;
184 my $doc = $parser->parse_string($content);
186 # note that using findvalue strips any HTML elements embedded
187 # in that review. That helps us handle slight differences
188 # in the output provided by Syndetics 'old' and 'new' versions
189 # of their service and cleans any questionable HTML that
190 # may be present in the reviews, but does mean that any
191 # <B> and <I> tags used to format the review are also gone.
192 my $result = $doc->findvalue('//Fld520');
193 push @reviews, {title
=> $source->{title
}, reviews
=> [ { content
=> $result } ]} if $result;
196 warn "Error parsing Syndetics $source->{title} review";
202 sub get_syndetics_editions
{
203 my ( $isbn,$upc,$oclc ) = @_;
205 my $response = _fetch_syndetics_content
('FICTION.XML', $isbn, $upc, $oclc);
206 unless ($response->content_type =~ /xml/) {
210 my $content = $response->content;
212 my $xmlsimple = XML
::Simple
->new();
213 $response = $xmlsimple->XMLin(
215 forcearray
=> [ qw(Fld020) ],
217 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
219 $similar_items = \@
{$response->{VarFlds
}->{VarDFlds
}->{NumbCode
}->{Fld020
}} if $response;
220 return $similar_items if $similar_items;
223 sub get_syndetics_anotes
{
224 my ( $isbn,$upc,$oclc) = @_;
226 my $response = _fetch_syndetics_content
('ANOTES.XML', $isbn, $upc, $oclc);
227 unless ($response->content_type =~ /xml/) {
231 my $content = $response->content;
233 my $xmlsimple = XML
::Simple
->new();
234 $response = $xmlsimple->XMLin(
236 forcearray
=> [ qw(Fld980) ],
240 for my $fld980 (@
{$response->{VarFlds
}->{VarDFlds
}->{SSIFlds
}->{Fld980
}}) {
241 # this is absurd, but sometimes this data serializes differently
242 if(ref($fld980->{a
}->{content
}) eq 'ARRAY') {
243 for my $content (@
{$fld980->{a
}->{content
}}) {
244 push @anotes, {content
=> $content};
249 push @anotes, {content
=> $fld980->{a
}->{content
}};
255 sub _fetch_syndetics_content
{
256 my ( $element, $isbn, $upc, $oclc ) = @_;
258 $isbn = '' unless defined $isbn;
259 $upc = '' unless defined $upc;
260 $oclc = '' unless defined $oclc;
262 my $syndetics_client_code = C4
::Context
->preference('SyndeticsClientCode');
264 my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$element&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
265 my $ua = LWP
::UserAgent
->new;
268 my $response = $ua->get($url);
270 warn "could not retrieve $url" unless $response->content;
283 Joshua Ferraro <jmf@liblime.com>