Bug 21738: check items count in C4:ILSDI::HoldTitle
[koha.git] / C4 / External / Syndetics.pm
index c6d7ecf..0570d0f 100644 (file)
@@ -4,18 +4,18 @@ package C4::External::Syndetics;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use XML::Simple;
 use XML::LibXML;
@@ -26,11 +26,10 @@ use HTTP::Request::Common;
 use strict;
 use warnings;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
 BEGIN {
     require Exporter;
-    $VERSION = 0.03;
     @ISA = qw(Exporter);
     @EXPORT = qw(
         &get_syndetics_index
@@ -56,34 +55,23 @@ This module provides facilities for retrieving Syndetics.com content in Koha
 
 =head2 get_syndetics_summary
 
-=over 4
-
-my $syndetics_summary= &get_syndetics_summary( $isbn );
-
-=back
+  my $syndetics_summary= &get_syndetics_summary( $isbn );
 
 Get Summary data from Syndetics
 
 =cut
 
 sub get_syndetics_index {
-    my ( $isbn,$upc,$oclc ) = @_;
-
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
+    my ( $isbn, $upc, $oclc ) = @_;
 
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/INDEX.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
+    return unless ( $isbn || $upc || $oclc );
 
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-    my $response = $ua->get($url);
+    my $response = _fetch_syndetics_content('INDEX.XML', $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }
 
     my $content = $response->content;
-    warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
     $response = $xmlsimple->XMLin(
         $content,
@@ -93,38 +81,30 @@ sub get_syndetics_index {
     for my $available_type ('SUMMARY','TOC','FICTION','AWARDS1','SERIES1','SPSUMMARY','SPREVIEW', 'AVPROFILE', 'AVSUMMARY','DBCHAPTER','LJREVIEW','PWREVIEW','SLJREVIEW','CHREVIEW','BLREVIEW','HBREVIEW','KIREVIEW','CRITICASREVIEW','ANOTES') {
         if (exists $response->{$available_type} && $response->{$available_type} =~ /$available_type/) {
             $syndetics_elements->{$available_type} = $available_type;
-            #warn "RESPONSE: $available_type : $response->{$available_type}";
         }
     }
+
     return $syndetics_elements if $syndetics_elements;
 }
 
 sub get_syndetics_summary {
     my ( $isbn, $upc, $oclc, $syndetics_elements ) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
-
-    my $summary_type = exists($syndetics_elements->{'AVSUMMARY'}) ? 'AVSUMMARY' : 'SUMMARY';
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$summary_type.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-    my $response = $ua->get($url);
+    my $summary_type = exists($syndetics_elements->{'AVSUMMARY'}) ? 'AVSUMMARY.XML' : 'SUMMARY.XML';
+    my $response = _fetch_syndetics_content($summary_type, $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }  
 
     my $content = $response->content;
 
-    warn "could not retrieve $url" unless $content;
     my $summary;
     eval { 
         my $doc = $parser->parse_string($content);
         $summary = $doc->findvalue('//Fld520');
     };
     if ($@) {
-        warn "Error parsing response from $url";
+        warn "Error parsing Syndetics $summary_type";
     }
     return $summary if $summary;
 }
@@ -132,21 +112,12 @@ sub get_syndetics_summary {
 sub get_syndetics_toc {
     my ( $isbn,$upc,$oclc ) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
-
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/TOC.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-        
-    my $response = $ua->get($url);
+    my $response = _fetch_syndetics_content('TOC.XML', $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }  
 
     my $content = $response->content;
-    warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
     $response = $xmlsimple->XMLin(
         $content,
@@ -161,20 +132,12 @@ sub get_syndetics_toc {
 sub get_syndetics_excerpt {
     my ( $isbn,$upc,$oclc ) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
-
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/DBCHAPTER.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-    my $response = $ua->get($url);
+    my $response = _fetch_syndetics_content('DBCHAPTER.XML', $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }  
         
     my $content = $response->content;
-    warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
     $response = $xmlsimple->XMLin(
         $content,
@@ -189,8 +152,6 @@ sub get_syndetics_excerpt {
 sub get_syndetics_reviews {
     my ( $isbn,$upc,$oclc,$syndetics_elements ) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
     my @reviews;
     my $review_sources = [
     {title => 'Library Journal Review', file => 'LJREVIEW.XML', element => 'LJREVIEW'},
@@ -211,19 +172,12 @@ sub get_syndetics_reviews {
             #warn "Skipping $source->{element} doesn't match $syndetics_elements->{$source->{element}} \n";
             next;
         }
-        my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$source->{file}&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-
-        my $ua = LWP::UserAgent->new;
-        $ua->timeout(10);
-        $ua->env_proxy;
-        my $response = $ua->get($url);
+        my $response = _fetch_syndetics_content($source->{file}, $isbn, $upc, $oclc);
         unless ($response->content_type =~ /xml/) {
             next;
         }
 
         my $content = $response->content;
-        warn "could not retrieve $url" unless $content;
        
         eval { 
             my $doc = $parser->parse_string($content);
@@ -238,7 +192,7 @@ sub get_syndetics_reviews {
             push @reviews, {title => $source->{title}, reviews => [ { content => $result } ]} if $result;
         };
         if ($@) {
-            warn "Error parsing response from $url";
+            warn "Error parsing Syndetics $source->{title} review";
         }
     }
     return \@reviews;
@@ -247,22 +201,13 @@ sub get_syndetics_reviews {
 sub get_syndetics_editions {
     my ( $isbn,$upc,$oclc ) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
-
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/FICTION.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-
-    my $response = $ua->get($url);
+    my $response = _fetch_syndetics_content('FICTION.XML', $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }  
 
     my $content = $response->content;
 
-    warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
     $response = $xmlsimple->XMLin(
         $content,
@@ -277,22 +222,13 @@ sub get_syndetics_editions {
 sub get_syndetics_anotes {
     my ( $isbn,$upc,$oclc) = @_;
 
-    # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
-    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
-
-    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/ANOTES.XML&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
-    my $ua = LWP::UserAgent->new;
-    $ua->timeout(10);
-    $ua->env_proxy;
-
-    my $response = $ua->get($url);
+    my $response = _fetch_syndetics_content('ANOTES.XML', $isbn, $upc, $oclc);
     unless ($response->content_type =~ /xml/) {
         return;
     }
 
     my $content = $response->content;
 
-    warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
     $response = $xmlsimple->XMLin(
         $content,
@@ -315,11 +251,32 @@ sub get_syndetics_anotes {
     return \@anotes;
 }
 
+sub _fetch_syndetics_content {
+    my ( $element, $isbn, $upc, $oclc ) = @_;
+
+    $isbn = '' unless defined $isbn;
+    $upc  = '' unless defined $upc;
+    $oclc = '' unless defined $oclc;
+
+    my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
+
+    my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$element&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
+    my $ua = LWP::UserAgent->new;
+    $ua->timeout(10);
+    $ua->env_proxy;
+    my $response = $ua->get($url);
+
+    warn "could not retrieve $url" unless $response->content;
+    return $response;
+
+}
 1;
 __END__
 
 =head1 NOTES
 
+=cut
+
 =head1 AUTHOR
 
 Joshua Ferraro <jmf@liblime.com>