Adds Syndetics Reviews
[koha_ffzg] / C4 / External / Syndetics.pm
1 package C4::External::Syndetics;
2 # Copyright (C) 2006 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
25 use strict;
26 use warnings;
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 BEGIN {
31     require Exporter;
32     $VERSION = 0.03;
33     @ISA = qw(Exporter);
34     @EXPORT = qw(
35         &get_syndetics_summary
36         &get_syndetics_toc
37                 &get_syndetics_editions
38                 &get_syndetics_excerpt
39                 &get_syndetics_reviews
40     );
41 }
42
43 =head1 NAME
44
45 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
46
47 =head1 FUNCTIONS
48
49 This module provides facilities for retrieving Syndetics.com content in Koha
50
51 =head2 get_syndetics_summary
52
53 =over 4
54
55 my $syndetics_summary= &get_syndetics_summary( $xisbn );
56
57 =back
58
59 Get Summary data from Syndetics
60
61 =cut
62
63 sub get_syndetics_summary {
64     my ( $isbn ) = @_;
65
66     #normalize the ISBN
67     $isbn = _normalize_match_point ($isbn);
68
69     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
70     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
71
72     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/SUMMARY.XML&client=$syndetics_client_code&type=xw10";
73         my $ua = LWP::UserAgent->new;
74     $ua->timeout(10);
75     $ua->env_proxy;
76         my $response = $ua->get($url);
77         unless ($response->content_type =~ /xml/) {
78                 return;
79     }  
80
81     my $content = $response->content;
82
83     warn "could not retrieve $url" unless $content;
84     my $xmlsimple = XML::Simple->new();
85     $response = $xmlsimple->XMLin(
86         $content,
87         forcearray => [ qw(Fld520) ],
88     ) unless !$content;
89         # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
90         my $summary;
91         $summary = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
92     return $summary if $summary;
93 }
94
95 sub get_syndetics_toc {
96     my ( $isbn ) = @_;
97
98     #normalize the ISBN
99     $isbn = _normalize_match_point ($isbn);
100
101     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
102     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
103
104     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/TOC.XML&client=$syndetics_client_code&type=xw10";
105         my $ua = LWP::UserAgent->new;
106     $ua->timeout(10);
107     $ua->env_proxy;
108         
109         my $response = $ua->get($url);
110         unless ($response->content_type =~ /xml/) {
111                 return;
112         }  
113
114         my $content = $response->content;
115     warn "could not retrieve $url" unless $content;
116     my $xmlsimple = XML::Simple->new();
117     $response = $xmlsimple->XMLin(
118         $content,
119         forcearray => [ qw(Fld970) ],
120     ) unless !$content;
121     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
122     my $toc;
123         $toc = \@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld970}} if $response;
124     return $toc if $toc;
125 }
126
127 sub get_syndetics_excerpt {
128     my ( $isbn ) = @_;
129
130     #normalize the ISBN
131     $isbn = _normalize_match_point ($isbn);
132
133     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
134     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
135
136     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/DBCHAPTER.XML&client=$syndetics_client_code&type=xw10";
137         my $ua = LWP::UserAgent->new;
138     $ua->timeout(10);
139     $ua->env_proxy;
140     my $response = $ua->get($url);
141     unless ($response->content_type =~ /xml/) {
142                 return;
143         }  
144         
145         my $content = $response->content;
146     warn "could not retrieve $url" unless $content;
147     my $xmlsimple = XML::Simple->new();
148     $response = $xmlsimple->XMLin(
149         $content,
150         forcearray => [ qw(Fld520) ],
151     ) unless !$content;
152     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
153     my $excerpt;
154     $excerpt = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
155     return XMLout($excerpt) if $excerpt;
156 }
157
158 sub get_syndetics_reviews {
159     my ( $isbn ) = @_;
160
161     #normalize the ISBN
162     $isbn = _normalize_match_point ($isbn);
163
164     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
165     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
166         my @reviews;
167         my $review_sources = [
168         {title => 'Library Journal Review', file => 'LJREVIEW.XML'},
169         {title => 'Publishers Weekly Review', file => 'PWREVIEW.XML'},
170         {title => 'School Library Journal Review', file => 'SLJREVIEW.XML'},
171         {title => 'CHOICE Review', file => 'CHREVIEW.XML'},
172         {title => 'Booklist Review', file => 'BLREVIEW.XML'},
173         {title => 'Horn Book Review', file => 'HBREVIEW.XML'},
174         {title => 'Kirkus Book Review', file => 'KIRKREVIEW.XML'},
175         {title => 'Criticas Review', file => 'CRITICASREVIEW.XML'}
176         ];
177
178         for my $source (@$review_sources) {
179         my $url = "http://syndetics.com/index.aspx?isbn=$isbn/$source->{file}&client=$syndetics_client_code&type=xw10";
180
181                 my $ua = LWP::UserAgent->new;
182                 $ua->timeout(10);
183                 $ua->env_proxy;
184  
185                 my $response = $ua->get($url);
186                 unless ($response->content_type =~ /xml/) {
187                         next;
188                 }
189
190         my $content = $response->content;
191         warn "could not retrieve $url" unless $content;
192         my $xmlsimple = XML::Simple->new();
193                 eval {
194                 $response = $xmlsimple->XMLin(
195                 $content,
196                 forcearray => [ qw(Fld520) ],
197         ) unless !$content;
198                 };
199
200                 # This particular review deserializes differently
201                 if ($source->{file} =~ /BLREVIEW.XML/) {
202                         for my $subfield_a (@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}}) {
203                                 my @content;
204                                 for my $content (@{$subfield_a->{a}->{content}}) {
205                                         push @content, {content => $content};
206                                 }
207                                 push @reviews, {title => $source->{title}, reviews => \@content}; #[ {content => $content} ]};
208                         }
209                 }
210                 else {
211                         push @reviews, {title => $source->{title}, reviews => \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}}} unless $@;
212                 }
213
214         }
215         return \@reviews;
216 }
217
218 sub get_syndetics_editions {
219     my ( $isbn ) = @_;
220
221     #normalize the ISBN
222     $isbn = _normalize_match_point ($isbn);
223
224     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
225     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
226
227     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/FICTION.XML&client=$syndetics_client_code&type=xw10";
228         my $ua = LWP::UserAgent->new;
229     $ua->timeout(10);
230     $ua->env_proxy;
231
232     my $response = $ua->get($url);
233     unless ($response->content_type =~ /xml/) {
234         return;
235     }  
236
237     my $content = $response->content;
238
239     warn "could not retrieve $url" unless $content;
240     my $xmlsimple = XML::Simple->new();
241     $response = $xmlsimple->XMLin(
242         $content,
243         forcearray => [ qw(Fld020) ],
244     ) unless !$content;
245     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
246     my $similar_items;
247         $similar_items = \@{$response->{VarFlds}->{VarDFlds}->{NumbCode}->{Fld020}} if $response;
248     return $similar_items if $similar_items;
249 }
250
251 sub _normalize_match_point {
252         my $match_point = shift;
253         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
254         $normalized_match_point =~ s/-//g;
255
256         return $normalized_match_point;
257 }
258
259 1;
260 __END__
261
262 =head1 NOTES
263
264 =head1 AUTHOR
265
266 Joshua Ferraro <jmf@liblime.com>
267
268 =cut