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);
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 my $response = _fetch_syndetics_content('INDEX.XML', $isbn, $upc, $oclc);
70 my $content = $response->content;
71 my $xmlsimple = XML::Simple->new();
72 $response = $xmlsimple->XMLin(
76 my $syndetics_elements;
77 for my $available_type ('SUMMARY','TOC','FICTION','AWARDS1','SERIES1','SPSUMMARY','SPREVIEW', 'AVPROFILE', 'AVSUMMARY','DBCHAPTER','LJREVIEW','PWREVIEW','SLJREVIEW','CHREVIEW','BLREVIEW','HBREVIEW','KIREVIEW','CRITICASREVIEW','ANOTES') {
78 if (exists $response->{$available_type} && $response->{$available_type} =~ /$available_type/) {
79 $syndetics_elements->{$available_type} = $available_type;
80 #warn "RESPONSE: $available_type : $response->{$available_type}";
83 return $syndetics_elements if $syndetics_elements;
86 sub get_syndetics_summary {
87 my ( $isbn, $upc, $oclc, $syndetics_elements ) = @_;
89 my $summary_type = exists($syndetics_elements->{'AVSUMMARY'}) ? 'AVSUMMARY.XML' : 'SUMMARY.XML';
90 my $response = _fetch_syndetics_content($summary_type, $isbn, $upc, $oclc);
91 unless ($response->content_type =~ /xml/) {
95 my $content = $response->content;
99 my $doc = $parser->parse_string($content);
100 $summary = $doc->findvalue('//Fld520');
103 warn "Error parsing Syndetics $summary_type";
105 return $summary if $summary;
108 sub get_syndetics_toc {
109 my ( $isbn,$upc,$oclc ) = @_;
111 my $response = _fetch_syndetics_content('TOC.XML', $isbn, $upc, $oclc);
112 unless ($response->content_type =~ /xml/) {
116 my $content = $response->content;
117 my $xmlsimple = XML::Simple->new();
118 $response = $xmlsimple->XMLin(
120 forcearray => [ qw(Fld970) ],
122 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
124 $toc = \@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld970}} if $response;
128 sub get_syndetics_excerpt {
129 my ( $isbn,$upc,$oclc ) = @_;
131 my $response = _fetch_syndetics_content('DBCHAPTER.XML', $isbn, $upc, $oclc);
132 unless ($response->content_type =~ /xml/) {
136 my $content = $response->content;
137 my $xmlsimple = XML::Simple->new();
138 $response = $xmlsimple->XMLin(
140 forcearray => [ qw(Fld520) ],
142 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
144 $excerpt = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
145 return XMLout($excerpt, NoEscape => 1) if $excerpt;
148 sub get_syndetics_reviews {
149 my ( $isbn,$upc,$oclc,$syndetics_elements ) = @_;
152 my $review_sources = [
153 {title => 'Library Journal Review', file => 'LJREVIEW.XML', element => 'LJREVIEW'},
154 {title => 'Publishers Weekly Review', file => 'PWREVIEW.XML', element => 'PWREVIEW'},
155 {title => 'School Library Journal Review', file => 'SLJREVIEW.XML', element => 'SLJREVIEW'},
156 {title => 'CHOICE Review', file => 'CHREVIEW.XML', element => 'CHREVIEW'},
157 {title => 'Booklist Review', file => 'BLREVIEW.XML', element => 'BLREVIEW'},
158 {title => 'Horn Book Review', file => 'HBREVIEW.XML', element => 'HBREVIEW'},
159 {title => 'Kirkus Book Review', file => 'KIREVIEW.XML', element => 'KIREVIEW'},
160 {title => 'Criticas Review', file => 'CRITICASREVIEW.XML', element => 'CRITICASREVIEW'},
161 {title => 'Spanish Review', file => 'SPREVIEW.XML', element => 'SPREVIEW'},
164 for my $source (@$review_sources) {
165 if ($syndetics_elements->{$source->{element}} and $source->{element} =~ $syndetics_elements->{$source->{element}}) {
168 #warn "Skipping $source->{element} doesn't match $syndetics_elements->{$source->{element}} \n";
171 my $response = _fetch_syndetics_content($source->{file}, $isbn, $upc, $oclc);
172 unless ($response->content_type =~ /xml/) {
176 my $content = $response->content;
179 my $doc = $parser->parse_string($content);
181 # note that using findvalue strips any HTML elements embedded
182 # in that review. That helps us handle slight differences
183 # in the output provided by Syndetics 'old' and 'new' versions
184 # of their service and cleans any questionable HTML that
185 # may be present in the reviews, but does mean that any
186 # <B> and <I> tags used to format the review are also gone.
187 my $result = $doc->findvalue('//Fld520');
188 push @reviews, {title => $source->{title}, reviews => [ { content => $result } ]} if $result;
191 warn "Error parsing Syndetics $source->{title} review";
197 sub get_syndetics_editions {
198 my ( $isbn,$upc,$oclc ) = @_;
200 my $response = _fetch_syndetics_content('FICTION.XML', $isbn, $upc, $oclc);
201 unless ($response->content_type =~ /xml/) {
205 my $content = $response->content;
207 my $xmlsimple = XML::Simple->new();
208 $response = $xmlsimple->XMLin(
210 forcearray => [ qw(Fld020) ],
212 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
214 $similar_items = \@{$response->{VarFlds}->{VarDFlds}->{NumbCode}->{Fld020}} if $response;
215 return $similar_items if $similar_items;
218 sub get_syndetics_anotes {
219 my ( $isbn,$upc,$oclc) = @_;
221 my $response = _fetch_syndetics_content('ANOTES.XML', $isbn, $upc, $oclc);
222 unless ($response->content_type =~ /xml/) {
226 my $content = $response->content;
228 my $xmlsimple = XML::Simple->new();
229 $response = $xmlsimple->XMLin(
231 forcearray => [ qw(Fld980) ],
235 for my $fld980 (@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld980}}) {
236 # this is absurd, but sometimes this data serializes differently
237 if(ref($fld980->{a}->{content}) eq 'ARRAY') {
238 for my $content (@{$fld980->{a}->{content}}) {
239 push @anotes, {content => $content};
244 push @anotes, {content => $fld980->{a}->{content}};
250 sub _fetch_syndetics_content {
251 my ( $element, $isbn, $upc, $oclc ) = @_;
253 $isbn = '' unless defined $isbn;
254 $upc = '' unless defined $upc;
255 $oclc = '' unless defined $oclc;
257 my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
259 my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$element&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
260 my $ua = LWP::UserAgent->new;
263 my $response = $ua->get($url);
265 warn "could not retrieve $url" unless $response->content;
278 Joshua Ferraro <jmf@liblime.com>