]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Syndetic.pm
Finish Added Content by record ID
[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, $keys ) = @_;
34     return $self->send_img(
35         $self->fetch_response('sc.gif', $keys, 1));
36 }
37
38 sub jacket_medium {
39     my( $self, $keys ) = @_;
40     return $self->send_img(
41         $self->fetch_response('mc.gif', $keys, 1));
42
43 }
44 sub jacket_large {
45     my( $self, $keys ) = @_;
46     return $self->send_img(
47         $self->fetch_response('lc.gif', $keys, 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>No Data Available<\/title>/iog;
218     return 0 if $data =~ m/<title>error<\/title>/iog;
219     return 1;
220 }
221
222
223 sub send_json {
224     my( $self, $xml ) = @_;
225     return 0 unless $self->data_exists($xml);
226     my $doc;
227
228     try {
229         $doc = XML::LibXML->new->parse_string($xml);
230     } catch Error with {
231         my $err = shift;
232         $logger->error("added content XML parser error: $err\n\n$xml");
233         $doc = undef;
234     };
235
236     return 0 unless $doc;
237     my $perl = OpenSRF::Utils::SettingsParser::XML2perl($doc->documentElement);
238     my $json = OpenSRF::Utils::JSON->perl2JSON($perl);
239     return { content_type => 'text/plain', content => $json };
240 }
241
242 sub send_xml {
243     my( $self, $xml ) = @_;
244     return 0 unless $self->data_exists($xml);
245     return { content_type => 'application/xml', content => $xml };
246 }
247
248 sub send_html {
249     my( $self, $content ) = @_;
250     return 0 unless $self->data_exists($content);
251
252     # Hide anything that might contain a link since it will be broken
253     my $HTML = <<"    HTML";
254         <div>
255             <style type='text/css'>
256                 div.ac input, div.ac a[href],div.ac img, div.ac button { display: none; visibility: hidden }
257             </style>
258             <div class='ac'>
259                 $content
260             </div>
261         </div>
262     HTML
263
264     return { content_type => 'text/html', content => $HTML };
265 }
266
267 sub send_img {
268     my($self, $response) = @_;
269     return { 
270         content_type => $response->header('Content-type'),
271         content => $response->content, 
272         binary => 1 
273     };
274 }
275
276 # returns the raw content returned from the URL fetch
277 sub fetch_content {
278     my( $self, $page, $key ) = @_;
279     return $self->fetch_response($page, $key)->content;
280 }
281
282 # returns the HTTP response object from the URL fetch
283 sub fetch_response {
284     my( $self, $page, $keys, $notype ) = @_;
285     my $uname = $self->userid;
286
287     # Fetch single isbn and single upc
288     my $isbn = $keys->{isbn}[0];
289     my $upc = $keys->{upc}[0];
290
291     my $url = $self->base_url . "?isbn=$isbn/$page&client=$uname" . (($notype) ? '' : "&type=rw12");
292     return $AC->get_url($url);
293 }
294
295
296
297 1;