Bugzilla – Attachment 48160 Details for
Bug 6624
Allow Koha to use the new read API from OpenLibrary
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
bug_6624: Added Open Library Search and Read API to openlibrary.js
bug6624-Added-Open-Library-Search-and-Read-API-to-.patch (text/plain), 7.10 KB, created by
Jonathan Druart
on 2016-02-17 11:15:10 UTC
(
hide
)
Description:
bug_6624: Added Open Library Search and Read API to openlibrary.js
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2016-02-17 11:15:10 UTC
Size:
7.10 KB
patch
obsolete
>From b7f285db6660490e0f403edcfc9274e32ff5037c Mon Sep 17 00:00:00 2001 >From: Srdjan <srdjan@catalyst.net.nz> >Date: Mon, 14 Dec 2015 20:07:08 +1300 >Subject: [PATCH] bug_6624: Added Open Library Search and Read API to > openlibrary.js > >Signed-off-by: Nicole Engard <nengard@bywatersolutions.com> > >Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >--- > koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js | 180 +++++++++++++++++++++++- > 1 file changed, 175 insertions(+), 5 deletions(-) > >diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js b/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js >index 995cb6b..318576a 100644 >--- a/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js >+++ b/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js >@@ -5,8 +5,7 @@ if (typeof KOHA == "undefined" || !KOHA) { > /** > * A namespace for OpenLibrary related functions. > */ >-KOHA.OpenLibrary = { >- >+KOHA.OpenLibrary = new function() { > > /** > * Search all: >@@ -17,7 +16,7 @@ KOHA.OpenLibrary = { > * The result is asynchronously returned by OpenLibrary and catched by > * olCallBack(). > */ >- GetCoverFromIsbn: function() { >+ this.GetCoverFromIsbn = function() { > var bibkeys = []; > $("[id^=openlibrary-thumbnail]").each(function(i) { > bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn >@@ -30,13 +29,13 @@ KOHA.OpenLibrary = { > "&callback=KOHA.OpenLibrary.olCallBack&jscmd=data"); > scriptElement.setAttribute("type", "text/javascript"); > document.documentElement.firstChild.appendChild(scriptElement); >- }, >+ } > > /** > * Add cover pages <div > * and link to preview if div id is gbs-thumbnail-preview > */ >- olCallBack: function(booksInfo) { >+ this.olCallBack = function(booksInfo) { > for (id in booksInfo) { > var book = booksInfo[id]; > var isbn = id.substring(5); >@@ -64,4 +63,175 @@ KOHA.OpenLibrary = { > }); > } > } >+ >+ var search_url = 'https://openlibrary.org/search?'; >+ this.searchUrl = function( q ) { >+ var params = {q: q}; >+ return search_url + $.param(params); >+ }; >+ >+ var search_url_json = 'https://openlibrary.org/search.json'; >+ this.search = function( q, page_no, callback ) { >+ var params = {q: q}; >+ if (page_no) { >+ params.page = page_no; >+ } >+ $.ajax( { >+ type: 'GET', >+ url: search_url_json, >+ dataType: 'json', >+ data: params, >+ error: function( xhr, error ) { >+ try { >+ callback( JSON.parse( xhr.responseText )); >+ } catch ( e ) { >+ callback( {error: xhr.responseText || true} ); >+ } >+ }, >+ success: callback >+ } ); >+ }; >+}; >+/* readapi_automator.js */ >+ >+/* >+This script helps to put readable links to Open Library books into >+online book catalogs. >+When loaded, it searches the DOM for <div> elements with class >+"ol_readapi_book", extracts book identifiers from them (e.g. isbn, >+lccn, etc.) and puts those into an asynchronous call to the Read API. >+When the call returns, the results are used to add clickable links >+to the "ol_readapi_book" elements found earlier. >+A demonstration use of this script is available here: >+http://internetarchive.github.com/read_api_extras/readapi_demo.html >+*/ >+ >+var ol_readapi_automator = >+(function () { // open anonymous scope for tidiness >+ >+// 'constants' >+var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys']; >+var magic_classname = 'ol_readapi_book'; >+ >+// added to book divs to correlate with API results >+var magic_bookid = 'ol_bookid'; >+var ol_button_classname = 'ol_readapi_button'; >+ >+// Find all book divs and concatenate ids from them to create a read >+// API query url >+function create_query() { >+ var q = 'http://openlibrary.org/api/volumes/brief/json/'; >+ >+ function add_el(i, el) { >+ // tag with number found so it's easy to discover later >+ // (necessary? just go by index?) >+ // (choose better name?) >+ $(el).attr(magic_bookid, i); >+ >+ if (i > 0) { >+ q += '|'; >+ } >+ q += 'id:' + i; >+ >+ for (bi in readapi_bibids) { >+ bibid = readapi_bibids[bi]; >+ if ($(el).attr(bibid)) { >+ q += ';' + bibid + ':' + $(el).attr(bibid); >+ } >+ } >+ } >+ >+ $('.' + magic_classname).each(add_el); >+ return q; >+} >+ >+function make_read_button(bookdata) { >+ buttons = { >+ 'full access': >+ "http://openlibrary.org/images/button-read-open-library.png", >+ 'lendable': >+ "http://openlibrary.org/images/button-borrow-open-library.png", >+ 'checked out': >+ "http://openlibrary.org/images/button-checked-out-open-library.png" >+ }; >+ if (bookdata.items.length == 0) { >+ return false; >+ } >+ first = bookdata.items[0]; >+ if (!(first.status in buttons)) { >+ return false; >+ } >+ result = '<a href="' + first.itemURL + '">' + >+ '<img class="' + ol_button_classname + >+ '" src="' + buttons[first.status] + '"/></a>'; >+ return result; >+} >+ >+// Default function for decorating document elements with read API data >+function default_decorate_el_fn(el, bookdata) { >+ // Note that 'bookdata' may be undefined, if the Read API call >+ // didn't return results for this book >+ if (!bookdata) { >+ decoration = 'Not found'; >+ } else { >+ decoration = make_read_button(bookdata); >+ } >+ if (decoration) { >+ el.innerHTML += decoration; >+ } >+} >+ >+function do_query(q, decorate_el_fn) { >+ if (!decorate_el_fn) { >+ decorate_el_fn = default_decorate_el_fn; >+ } >+ var starttime = (new Date()).getTime(); >+ >+ // Call a function on each <div class="ol_readapi_book"> element >+ // with the target element and the data found for that element. >+ // Use decorate_el_fn if supplied, falling back to >+ // default_decorate_el_fn, above. >+ function query_callback(data, textStatus, jqXHR) { >+ var endtime = (new Date()).getTime(); >+ var duration = (endtime - starttime) / 1000; >+ // console.log('took ' + duration + ' seconds'); >+ >+ $('.' + magic_classname).each(function(i, el) { >+ var bookid = $(el).attr(magic_bookid); >+ if (bookid && bookid in data) { >+ decorate_el_fn(el, data[bookid]); >+ } else { >+ decorate_el_fn(el); >+ } >+ }); >+ } >+ >+ // console.log('calling ' + q); >+ $.ajax({ url: q, >+ data: { 'show_all_items': 'true' }, >+ dataType: 'jsonp', >+ success: query_callback >+ }); >+} >+ >+// Do stuff >+var q = create_query(); >+do_query(q); >+ >+result = { >+ do_query: do_query, >+ create_query: create_query, >+ make_read_button: make_read_button > }; >+ >+return result; >+})(); // close anonymous scope >+ >+/* >+Possible futures: >+* Support alternate query targets, e.g. Hathi >+* show_all_items >+* show_inlibrary >+* ezproxy prefix (implies show_inlibrary?) >+* console debug output? (check all console.log) >+*/ >-- >2.7.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 6624
:
46291
|
46292
|
46293
|
46655
|
46656
|
46657
|
48152
|
48159
| 48160 |
48161
|
48162