From: Dan Wells Date: Thu, 17 Nov 2011 21:13:02 +0000 (-0500) Subject: Switch to new Google Books API; make SSL friendly X-Git-Tag: sprint4-merge-nov22~4785 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=7adc27d5e8db4c3c16c0da5a553dd409cf196620;p=working%2FEvergreen.git Switch to new Google Books API; make SSL friendly As implied in the title, this commit does two things. First, it switches to the new Google Books API (which is both imminent and also necessary to make SSL calls work). Though the information is scant, from what I have read and experienced, we do not need an API key to do searches and previews. I have also not hit any kind of unauthenticated limit in several days of heavy testing, so I would figure we are safe (at this point) for normal end-user OPAC browsing. Second, all Google Book requests are now done over https. This eliminates the majority of mixed content warnings when browsing securely, though you still get a warning when you actually do preview a book. In addition to possibly implementing protocol detection (rather than doing https all the time as a "lowest" common denominator), there are a few minor points where we might consider future changes. Those points are commented within the code. Signed-off-by: Dan Wells Signed-off-by: Dan Scott --- diff --git a/Open-ILS/web/opac/skin/default/js/rdetail.js b/Open-ILS/web/opac/skin/default/js/rdetail.js index 2d08344f5e..13c5eab95f 100644 --- a/Open-ILS/web/opac/skin/default/js/rdetail.js +++ b/Open-ILS/web/opac/skin/default/js/rdetail.js @@ -1254,7 +1254,7 @@ function rdetailCheckForGBPreview() { function searchForGBPreview( isbn ) { dojo.require("dojo.io.script"); dojo.io.script.get({"url": "https://www.google.com/jsapi"}); - dojo.io.script.get({"url": "http://books.google.com/books", "content": { "bibkeys": isbn, "jscmd": "viewapi", "callback": "GBPreviewCallback"}}); + dojo.io.script.get({"url": "https://www.googleapis.com/books/v1/volumes", "content": { "q": "isbn:" + isbn, "callback": "GBPreviewCallback"}}); } /** @@ -1266,21 +1266,17 @@ function searchForGBPreview( isbn ) { * @param {JSON} GBPBookInfo is the JSON object pulled from the Google books service. */ function GBPreviewCallback(GBPBookInfo) { - var GBPreviewDiv = document.getElementById("rdetail_preview_div"); - var GBPBook; + if (GBPBookInfo.totalItems < 1) return; - for ( i in GBPBookInfo ) { - GBPBook = GBPBookInfo[i]; - } - - if ( !GBPBook ) { + var accessInfo = GBPBookInfo.items[0].accessInfo; + if ( !accessInfo ) { return; } - if ( GBPBook.preview != "noview" ) { + if ( accessInfo.embeddable ) { // Add a button below the book cover image to load the preview. GBPBadge = document.createElement( 'img' ); - GBPBadge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif'; + GBPBadge.src = 'https://www.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif'; GBPBadge.title = $('rdetail_preview_badge').innerHTML; GBPBadge.style.border = 0; GBPBadgelink = document.createElement( 'a' ); 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 c8d510d817..d5052d34c6 100644 --- a/Open-ILS/web/opac/skin/default/js/result_common.js +++ b/Open-ILS/web/opac/skin/default/js/result_common.js @@ -428,15 +428,37 @@ function buildunAPISpan (span, type, id) { } function unhideGoogleBooksLink (data) { - for ( var i in data ) { - //if (data[i].preview == 'noview') continue; + for (var i = 0; i < data.items.length; i++) { + var item = data.items[i]; + + var gbspan; + for (var j = 0; j < item.volumeInfo.industryIdentifiers.length; j++) { + // XXX: As of 11-17-2011, some items do not return their own ISBN + // as an identifier, so this code fails. For example: + // https://www.googleapis.com/books/v1/volumes?q=isbn:0743243560&callback=unhideGoogleBooksLink + // It seems the only way around this would be doing a separate + // search for each result rather than one search for the whole + // page. Informal testing seems to indicate that these books + // are generally Google-unfriendly (no previews, not embeddable), + // so we will live without them for now. + var ident = item.volumeInfo.industryIdentifiers[j].identifier; + gbspan = $n(document.documentElement, 'googleBooksLink-' + ident); + if (gbspan) break; + } + if (!gbspan) continue; - var gbspan = $n(document.documentElement, 'googleBooksLink-' + i); var gba = $n(gbspan, "googleBooks-link"); gba.setAttribute( 'href', - data[i].info_url + item.volumeInfo.infoLink + // XXX: we might consider constructing the above link ourselves, + // as the link provided populates the search box with our original + // multi-item search. Something like: + // 'http://books.google.com/books?id=' + item.id + // Postive: cleaner display + // Negative: more fragile (link format subject to change; likely + // enough to matter?) ); removeCSSClass( gbspan, 'hide_me' ); } @@ -465,9 +487,13 @@ function resultDisplayRecord(rec, pos, is_mr) { if (googleBooksLink) { var gbspan = $n(r, "googleBooksLink"); + // Google never has dashes in the ISBN, records sometimes do; + // remove them to match results list + // XXX: consider making part of cleanISBN(), or we can work around + // this if we move to one request per record gbspan.setAttribute( 'name', - gbspan.getAttribute('name') + '-' + currentISBN + gbspan.getAttribute('name') + '-' + currentISBN.toString().replace(/-/g,"") ); } @@ -798,8 +824,8 @@ function fetchGoogleBooksLink () { var scriptElement = document.createElement("script"); scriptElement.setAttribute("id", "jsonScript"); scriptElement.setAttribute("src", - "http://books.google.com/books?bibkeys=" + - escape(isbnList.join(', ')) + "&jscmd=viewapi&callback=unhideGoogleBooksLink"); + "https://www.googleapis.com/books/v1/volumes?q=" + + escape('isbn:' + isbnList.join(' | isbn:')) + "&callback=unhideGoogleBooksLink"); scriptElement.setAttribute("type", "text/javascript"); // make the request to Google Book Search document.documentElement.firstChild.appendChild(scriptElement);