1 # ---------------------------------------------------------------
2 # Copyright (C) 2009 David Christensen <david.a.christensen@gmail.com>
3 # Copyright (C) 2009 Dan Scott <dscott@laurentian.ca>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
16 package OpenILS::WWW::AddedContent::OpenLibrary;
17 use strict; use warnings;
18 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenSRF::Utils::SettingsParser;
20 use OpenILS::WWW::AddedContent;
21 use OpenSRF::Utils::JSON;
22 use OpenSRF::EX qw/:try/;
25 # Edit the <added_content> section of /openils/conf/opensrf.xml
27 # <module>OpenILS::WWW::AddedContent::OpenLibrary</module>
29 my $AC = 'OpenILS::WWW::AddedContent';
31 # These URLs are always the same for OpenLibrary, so there's no advantage to
32 # pulling from opensrf.xml; we hardcode them here
33 my $base_url = 'http://openlibrary.org/api/books?details=true&bibkeys=ISBN:';
34 my $cover_base_url = 'http://covers.openlibrary.org/b/isbn/';
37 my( $class, $args ) = @_;
38 $class = ref $class || $class;
39 return bless($args, $class);
42 # --------------------------------------------------------------------------
44 my( $self, $key ) = @_;
45 return $self->send_img(
46 $self->fetch_cover_response('-S.jpg', $key));
50 my( $self, $key ) = @_;
51 return $self->send_img(
52 $self->fetch_cover_response('-M.jpg', $key));
56 my( $self, $key ) = @_;
57 return $self->send_img(
58 $self->fetch_cover_response('-L.jpg', $key));
61 # --------------------------------------------------------------------------
65 OpenLibrary returns a JSON hash of zero or more book responses matching our
66 request. Each response may contain a table of contents within the details
67 section of the response.
69 For now, we check only the first response in the hash for a table of
70 contents, and if we find a table of contents, we transform it to a simple
76 my( $self, $key ) = @_;
77 my $book_details_json = $self->fetch_response($key)->content();
80 # Trim the "var _OlBookInfo = " declaration that makes this
82 $book_details_json =~ s/^.+?({.*?});$/$1/s;
84 $logger->debug("$key: " . $book_details_json);
88 my $book_details = OpenSRF::Utils::JSON->JSON2perl($book_details_json);
89 my $book_key = (keys %$book_details)[0];
91 # We didn't find a matching book; short-circuit our response
93 $logger->debug("$key: no found book");
97 my $toc_json = $book_details->{$book_key}->{details}->{table_of_contents};
99 # No table of contents is available for this book; short-circuit
100 if (!$toc_json or !scalar(@$toc_json)) {
101 $logger->debug("$key: no TOC");
105 # Build a basic HTML table containing the section number, section title,
106 # and page number. Some rows may not contain section numbers, we should
107 # protect against empty page numbers too.
108 foreach my $chapter (@$toc_json) {
109 my $label = $chapter->{label};
113 my $title = $chapter->{title} || '';
114 my $page_number = $chapter->{pagenum} || '';
116 $toc_html .= '<tr>' .
117 "<td class='toc_label'>$label</td>" .
118 "<td class='toc_title'>$title</td>" .
119 "<td class='toc_page'>$page_number</td>" .
123 $logger->debug("$key: $toc_html");
124 $self->send_html("<table>$toc_html</table>");
128 my( $self, $key ) = @_;
129 my $toc = $self->send_json(
130 $self->fetch_response($key)
135 my($self, $response) = @_;
137 content_type => $response->header('Content-type'),
138 content => $response->content,
144 my( $self, $content ) = @_;
145 return 0 unless $content;
147 return { content_type => 'text/plain', content => $content };
151 my( $self, $content ) = @_;
152 return 0 unless $content;
154 # Hide anything that might contain a link since it will be broken
155 my $HTML = <<" HTML";
157 <style type='text/css'>
158 div.ac input, div.ac a[href],div.ac img, div.ac button { display: none; visibility: hidden }
166 return { content_type => 'text/html', content => $HTML };
169 # returns the HTTP response object from the URL fetch
171 my( $self, $key ) = @_;
172 my $url = $base_url . "$key";
173 my $response = $AC->get_url($url);
177 # returns the HTTP response object from the URL fetch
178 sub fetch_cover_response {
179 my( $self, $size, $key ) = @_;
180 my $url = $cover_base_url . "$key$size";
181 return $AC->get_url($url);