Adds support for Syndetics Excerpts and Editions
[koha.git] / 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     );
40 }
41
42 =head1 NAME
43
44 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
45
46 =head1 FUNCTIONS
47
48 This module provides facilities for retrieving Syndetics.com content in Koha
49
50 =head2 get_syndetics_summary
51
52 =over 4
53
54 my $syndetics_summary= &get_syndetics_summary( $xisbn );
55
56 =back
57
58 Get Summary data from Syndetics
59
60 =cut
61
62 sub get_syndetics_summary {
63     my ( $isbn ) = @_;
64
65     #normalize the ISBN
66     $isbn = _normalize_match_point ($isbn);
67
68     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
69     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
70
71     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/SUMMARY.XML&client=$syndetics_client_code&type=xw10";
72     # warn $url;
73     my $content = get($url);
74     warn "could not retrieve $url" unless $content;
75     my $xmlsimple = XML::Simple->new();
76     my $response = $xmlsimple->XMLin(
77         $content,
78         forcearray => [ qw(Fld520) ],
79     ) unless !$content;
80         # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
81         my $summary;
82         $summary = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
83     return $summary if $summary;
84 }
85
86 sub get_syndetics_toc {
87     my ( $isbn ) = @_;
88
89     #normalize the ISBN
90     $isbn = _normalize_match_point ($isbn);
91
92     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
93     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
94
95     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/TOC.XML&client=$syndetics_client_code&type=xw10";
96     #warn $url;
97     my $content = get($url);
98     warn "could not retrieve $url" unless $content;
99     my $xmlsimple = XML::Simple->new();
100     my $response = $xmlsimple->XMLin(
101         $content,
102         forcearray => [ qw(Fld970) ],
103     ) unless !$content;
104     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
105     my $toc;
106         $toc = \@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld970}} if $response;
107     return $toc if $toc;
108 }
109
110 sub get_syndetics_excerpt {
111     my ( $isbn ) = @_;
112
113     #normalize the ISBN
114     $isbn = _normalize_match_point ($isbn);
115
116     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
117     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
118
119     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/DBCHAPTER.XML&client=$syndetics_client_code&type=xw10";
120     #warn $url;
121     my $content = get($url);
122     warn "could not retrieve $url" unless $content;
123     my $xmlsimple = XML::Simple->new();
124     my $response = $xmlsimple->XMLin(
125         $content,
126         forcearray => [ qw(Fld520) ],
127     ) unless !$content;
128     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
129     my $excerpt;
130     $excerpt = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
131     return XMLout($excerpt) if $excerpt;
132 }
133
134
135 sub get_syndetics_editions {
136     my ( $isbn ) = @_;
137
138     #normalize the ISBN
139     $isbn = _normalize_match_point ($isbn);
140
141     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
142     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
143
144     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/FICTION.XML&client=$syndetics_client_code&type=xw10";
145     # warn $url;
146     my $content = get($url);
147     warn "could not retrieve $url" unless $content;
148     my $xmlsimple = XML::Simple->new();
149     my $response = $xmlsimple->XMLin(
150         $content,
151         forcearray => [ qw(Fld020) ],
152     ) unless !$content;
153     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
154     my $similar_items;
155         $similar_items = \@{$response->{VarFlds}->{VarDFlds}->{NumbCode}->{Fld020}} if $response;
156     return $similar_items if $similar_items;
157 }
158
159 sub _normalize_match_point {
160         my $match_point = shift;
161         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
162         $normalized_match_point =~ s/-//g;
163
164         return $normalized_match_point;
165 }
166
167 1;
168 __END__
169
170 =head1 NOTES
171
172 =head1 AUTHOR
173
174 Joshua Ferraro <jmf@liblime.com>
175
176 =cut