From ee4a2cb8f73cc15311f9fc777e287dd98744b3e1 Mon Sep 17 00:00:00 2001 From: Dan Scott Date: Sun, 12 Jun 2011 21:24:01 -0400 Subject: [PATCH] Refactor cover image fetching in OpenLibrary added content We were only trying to retrieve images from the items array in the OpenLibrary response, but there are only items if the OpenLibrary has online editions available - whereas there may be covers attached to the metadata for the work. So now we make the metadata work harder. Also, cut down on broken images in search results by short-circuiting if OpenLibrary doesn't have a matching record for a given ISBN. Thanks to Bill Erickson for pointing out that there were a lot of blank images getting returned due to the items reliance. Signed-off-by: Dan Scott Signed-off-by: Bill Erickson --- .../lib/OpenILS/WWW/AddedContent/OpenLibrary.pm | 26 ++++++++++++++++++---- Open-ILS/web/opac/skin/default/js/result_common.js | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/OpenLibrary.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/OpenLibrary.pm index a157389943..c2c2cc688b 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/OpenLibrary.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/OpenLibrary.pm @@ -331,22 +331,40 @@ sub fetch_items_response { return $book_results->{items}; } - # returns a cover image from the list of associated items # TODO: Look for the best match in the array of items sub fetch_cover_response { my( $self, $size, $key ) = @_; - my $items = $self->fetch_items_response($key); + my $cover; + + my $response = $self->fetch_response($key); - if (!$items) { + # Short-circuit if we get an empty response, or a response + #with no matching records + if (!$response or scalar(keys %$response) == 0) { return $AC->get_url($blank_img); } + # Try to return a cover image from the record->data metadata + foreach my $rec_key (keys %{$response->{records}}) { + my $record = $response->{records}->{$rec_key}; + if (exists $record->{data}->{cover}->{$size}) { + $cover = $record->{data}->{cover}->{$size}; + } + if ($cover) { + return $AC->get_url($cover); + } + } + + # If we didn't find a cover in the record metadata, look in the items + # Seems unlikely, but might as well try. + my $items = $response->{items}; + $logger->debug("$key: items request got " . scalar(@$items) . " items back"); foreach my $item (@$items) { - if ($item->{cover}) { + if (exists $item->{cover}->{$size}) { return $AC->get_url($item->{cover}->{$size}) || 0; } } diff --git a/Open-ILS/web/opac/skin/default/js/result_common.js b/Open-ILS/web/opac/skin/default/js/result_common.js index 3988e1e45f..6a5d8c3024 100644 --- a/Open-ILS/web/opac/skin/default/js/result_common.js +++ b/Open-ILS/web/opac/skin/default/js/result_common.js @@ -763,7 +763,7 @@ function renderOpenLibraryLinks(response) { ); } else if (ol_ebooks[isbn]['similar_lendable']) { createOpenLibraryLink( - isbn, ol_ebooks[isbn]['similar_full'], 'Borrow similar online' + isbn, ol_ebooks[isbn]['similar_lendable'], 'Borrow similar online' ); } } -- 2.11.0