Add ISSN support to Syndetic AC handler
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / AddedContent / Syndetic.pm
1 package OpenILS::WWW::AddedContent::Syndetic;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw/$logger/;
4 use OpenSRF::Utils::SettingsParser;
5 use OpenSRF::Utils::JSON;
6 use OpenSRF::EX qw/:try/;
7 use OpenILS::WWW::AddedContent;
8 use XML::LibXML;
9 use MIME::Base64;
10
11 my $AC = 'OpenILS::WWW::AddedContent';
12
13
14 sub new {
15     my( $class, $args ) = @_;
16     $class = ref $class || $class;
17     return bless($args, $class);
18 }
19
20 sub base_url {
21     my $self = shift;
22     return $self->{base_url};
23 }
24
25 sub userid {
26     my $self = shift;
27     return $self->{userid};
28 }
29
30 sub expects_keyhash {
31     # we expect a keyhash as opposed to a simple scalar containing an ISBN
32     return 1;
33 }
34
35 # --------------------------------------------------------------------------
36 sub jacket_small {
37     my( $self, $keys ) = @_;
38     return $self->send_img(
39         $self->fetch_response('sc.gif', $keys, 1));
40 }
41
42 sub jacket_medium {
43     my( $self, $keys ) = @_;
44     return $self->send_img(
45         $self->fetch_response('mc.gif', $keys, 1));
46
47 }
48 sub jacket_large {
49     my( $self, $keys ) = @_;
50     return $self->send_img(
51         $self->fetch_response('lc.gif', $keys, 1));
52 }
53
54 # --------------------------------------------------------------------------
55
56 sub toc_html {
57     my( $self, $key ) = @_;
58     return $self->send_html(
59         $self->fetch_content('toc.html', $key));
60 }
61
62 sub toc_xml {
63     my( $self, $key ) = @_;
64     return $self->send_xml(
65         $self->fetch_content('toc.xml', $key));
66 }
67
68 sub toc_json {
69     my( $self, $key ) = @_;
70     return $self->send_json(
71         $self->fetch_content('toc.xml', $key));
72 }
73
74 # --------------------------------------------------------------------------
75
76 sub anotes_html {
77     my( $self, $key ) = @_;
78     return $self->send_html(
79         $self->fetch_content('anotes.html', $key));
80 }
81
82 sub anotes_xml {
83     my( $self, $key ) = @_;
84     return $self->send_xml(
85         $self->fetch_content('anotes.xml', $key));
86 }
87
88 sub anotes_json {
89     my( $self, $key ) = @_;
90     return $self->send_json(
91         $self->fetch_content('anotes.xml', $key));
92 }
93
94
95 # --------------------------------------------------------------------------
96
97 sub excerpt_html {
98     my( $self, $key ) = @_;
99     return $self->send_html(
100         $self->fetch_content('dbchapter.html', $key));
101 }
102
103 sub excerpt_xml {
104     my( $self, $key ) = @_;
105     return $self->send_xml(
106         $self->fetch_content('dbchapter.xml', $key));
107 }
108
109 sub excerpt_json {
110     my( $self, $key ) = @_;
111     return $self->send_json(
112         $self->fetch_content('dbchapter.xml', $key));
113 }
114
115 # --------------------------------------------------------------------------
116
117 sub reviews_html {
118     my( $self, $key ) = @_;
119
120     my %reviews;
121
122     $reviews{ljreview} = $self->fetch_content('ljreview.html', $key); # Library Journal
123     $reviews{pwreview} = $self->fetch_content('pwreview.html', $key); # Publishers Weekly
124     $reviews{sljreview} = $self->fetch_content('sljreview.html', $key); # School Library Journal
125     $reviews{chreview} = $self->fetch_content('chreview.html', $key); # CHOICE Review
126     $reviews{blreview} = $self->fetch_content('blreview.html', $key); # Booklist Review
127     $reviews{hbreview} = $self->fetch_content('hbreview.html', $key); # Horn Book Review
128     $reviews{kireview} = $self->fetch_content('kireview.html', $key); # Kirkus Reviews
129     #$reviews{abreview} = $self->fetch_content('abreview.html', $key); # Bookseller+Publisher
130     #$reviews{criticasreview} = $self->fetch_content('criticasreview.html', $key); # Criticas
131     $reviews{nyreview} = $self->fetch_content('nyreview.html', $key); # New York Times
132     #$reviews{gdnreview} = $self->fetch_content('gdnreview.html', $key); # Guardian Review
133     #$reviews{doodysreview} = $self->fetch_content('doodysreview.html', $key); # Doody's Reviews
134
135     for(keys %reviews) {
136         if( ! $self->data_exists($reviews{$_}) ) {
137             delete $reviews{$_};
138             next;
139         }
140         $reviews{$_} =~ s/<!.*?>//og; # Strip any doctype declarations
141     }
142
143     return 0 if scalar(keys %reviews) == 0;
144     
145     #my $html = "<div>";
146     my $html;
147     $html .= $reviews{$_} for keys %reviews;
148     #$html .= "</div>";
149
150     return $self->send_html($html);
151 }
152
153 # we have to aggregate the reviews
154 sub reviews_xml {
155     my( $self, $key ) = @_;
156     my %reviews;
157
158     $reviews{ljreview} = $self->fetch_content('ljreview.xml', $key);
159     $reviews{pwreview} = $self->fetch_content('pwreview.xml', $key);
160     $reviews{sljreview} = $self->fetch_content('sljreview.xml', $key);
161     $reviews{chreview} = $self->fetch_content('chreview.xml', $key);
162     $reviews{blreview} = $self->fetch_content('blreview.xml', $key);
163     $reviews{hbreview} = $self->fetch_content('hbreview.xml', $key);
164     $reviews{kireview} = $self->fetch_content('kireview.xml', $key);
165     #$reviews{abreview} = $self->fetch_content('abreview.xml', $key);
166     #$reviews{criticasreview} = $self->fetch_content('criticasreview.xml', $key);
167     $reviews{nyreview} = $self->fetch_content('nyreview.xml', $key);
168     #$reviews{gdnreview} = $self->fetch_content('gdnreview.xml', $key);
169     #$reviews{doodysreview} = $self->fetch_content('doodysreview.xml', $key);
170
171     for(keys %reviews) {
172         if( ! $self->data_exists($reviews{$_}) ) {
173             delete $reviews{$_};
174             next;
175         }
176         # Strip the xml and doctype declarations
177         $reviews{$_} =~ s/<\?xml.*?>//og;
178         $reviews{$_} =~ s/<!.*?>//og;
179     }
180
181     return 0 if scalar(keys %reviews) == 0;
182     
183     my $xml = "<reviews>";
184     $xml .= $reviews{$_} for keys %reviews;
185     $xml .= "</reviews>";
186
187     return $self->send_xml($xml);
188 }
189
190
191 sub reviews_json {
192     my( $self, $key ) = @_;
193     return $self->send_json(
194         $self->fetch_content('dbchapter.xml', $key));
195 }
196
197 # --------------------------------------------------------------------------
198
199 sub summary_html {
200     my( $self, $key ) = @_;
201     return $self->send_html(
202         $self->fetch_content('summary.html', $key));
203 }
204
205 sub summary_xml {
206     my( $self, $key ) = @_;
207     return $self->send_xml(
208         $self->fetch_content('summary.xml', $key));
209 }
210
211 sub summary_json {
212     my( $self, $key ) = @_;
213     return $self->send_json(
214         $self->fetch_content('summary.xml', $key));
215 }
216
217 # --------------------------------------------------------------------------
218
219 sub data_exists {
220     my( $self, $data ) = @_;
221     return 0 if $data =~ m/<title>No Data Available<\/title>/iog;
222     return 0 if $data =~ m/<title>error<\/title>/iog;
223     return 1;
224 }
225
226
227 sub send_json {
228     my( $self, $xml ) = @_;
229     return 0 unless $self->data_exists($xml);
230     my $doc;
231
232     try {
233         $doc = XML::LibXML->new->parse_string($xml);
234     } catch Error with {
235         my $err = shift;
236         $logger->error("added content XML parser error: $err\n\n$xml");
237         $doc = undef;
238     };
239
240     return 0 unless $doc;
241     my $perl = OpenSRF::Utils::SettingsParser::XML2perl($doc->documentElement);
242     my $json = OpenSRF::Utils::JSON->perl2JSON($perl);
243     return { content_type => 'text/plain', content => $json };
244 }
245
246 sub send_xml {
247     my( $self, $xml ) = @_;
248     return 0 unless $self->data_exists($xml);
249     return { content_type => 'application/xml', content => $xml };
250 }
251
252 sub send_html {
253     my( $self, $content ) = @_;
254     return 0 unless $self->data_exists($content);
255
256     # Hide anything that might contain a link since it will be broken
257     my $HTML = <<"    HTML";
258         <div>
259             <style type='text/css'>
260                 div.ac input, div.ac a[href],div.ac img, div.ac button { display: none; visibility: hidden }
261             </style>
262             <div class='ac'>
263                 $content
264             </div>
265         </div>
266     HTML
267
268     return { content_type => 'text/html', content => $HTML };
269 }
270
271 sub send_img {
272     my($self, $response) = @_;
273     return { 
274         content_type => $response->header('Content-type'),
275         content => $response->content, 
276         binary => 1 
277     };
278 }
279
280 # returns the raw content returned from the URL fetch
281 sub fetch_content {
282     my( $self, $page, $key ) = @_;
283     return $self->fetch_response($page, $key)->content;
284 }
285
286 # returns the HTTP response object from the URL fetch
287 sub fetch_response {
288     my( $self, $page, $keys, $notype ) = @_;
289     my $uname = $self->userid;
290
291     # Fetch single isbn, upc, and issn
292     my $isbn = $keys->{isbn}[0];
293     my $upc  = $keys->{upc}[0];
294     my $issn = $keys->{issn}[0];
295
296     $isbn = '' if !defined($isbn);
297     $upc  = '' if !defined($upc);
298     $issn = '' if !defined($issn);
299
300     my $url = $self->base_url . "?isbn=$isbn/$page&upc=$upc&issn=$issn&client=$uname" . (($notype) ? '' : "&type=rw12");
301     return $AC->get_url($url);
302 }
303
304
305
306 1;