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