Add Syndetics Summaries and TOC
[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     );
38 }
39
40 =head1 NAME
41
42 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
43
44 =head1 FUNCTIONS
45
46 This module provides facilities for retrieving Syndetics.com content in Koha
47
48 =head2 get_syndetics_summary
49
50 =over 4
51
52 my $syndetics_summary= &get_syndetics_summary( $xisbn );
53
54 =back
55
56 Get Summary data from Syndetics
57
58 =cut
59
60 sub get_syndetics_summary {
61     my ( $isbn ) = @_;
62
63     #normalize the ISBN
64     $isbn = _normalize_match_point ($isbn);
65
66     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
67     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
68
69     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/SUMMARY.XML&client=$syndetics_client_code&type=xw10";
70     warn $url;
71     my $content = get($url);
72     warn "could not retrieve $url" unless $content;
73     my $xmlsimple = XML::Simple->new();
74     my $response = $xmlsimple->XMLin(
75         $content,
76         forcearray => [ qw(Fld520) ],
77     ) unless !$content;
78         # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
79         my $summary = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
80     return $summary if $summary;
81 }
82
83 sub get_syndetics_toc {
84     my ( $isbn ) = @_;
85
86     #normalize the ISBN
87     $isbn = _normalize_match_point ($isbn);
88
89     # grab the AWSAccessKeyId: mine is '0V5RRRRJZ3HR2RQFNHR2'
90     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
91
92     my $url = "http://syndetics.com/index.aspx?isbn=$isbn/TOC.XML&client=$syndetics_client_code&type=xw10";
93     warn $url;
94     my $content = get($url);
95     warn "could not retrieve $url" unless $content;
96     my $xmlsimple = XML::Simple->new();
97     my $response = $xmlsimple->XMLin(
98         $content,
99         forcearray => [ qw(Fld970) ],
100     ) unless !$content;
101     # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
102     my $toc = \@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld970}} if $response;
103     return $toc if $toc;
104 }
105
106 sub _normalize_match_point {
107         my $match_point = shift;
108         (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;
109         $normalized_match_point =~ s/-//g;
110
111         return $normalized_match_point;
112 }
113
114 1;
115 __END__
116
117 =head1 NOTES
118
119 =head1 AUTHOR
120
121 Joshua Ferraro <jmf@liblime.com>
122
123 =cut