First cut: Works JSON API
I've finished a simple Javascript/JSON API to LibraryThing's core work information. In structure and implementation the API resembles Google's recent Book Search API, but for LibraryThing.
Purpose. The API is designed to help libraries and others to add links to LibraryThing when LibraryThing has a book, and omit them when we don't. It's an easy conditional-linking system.
But the API returns other work information too, including the number of copies, number of reviews and average rating (with rating image). It comes with a simple function to insert the data where appropriate, but you can funnel this information to functions of your own devising.
Scope. This is an API to work information. Once I've worked through the kinks here, I plan to release a member API, allowing members to do clever things with their data. For example, members will be able to make their own widgets, not just rely on ours.
How it works. The basic mode of operation is to insert a script as follows:
Purpose. The API is designed to help libraries and others to add links to LibraryThing when LibraryThing has a book, and omit them when we don't. It's an easy conditional-linking system.
But the API returns other work information too, including the number of copies, number of reviews and average rating (with rating image). It comes with a simple function to insert the data where appropriate, but you can funnel this information to functions of your own devising.
Scope. This is an API to work information. Once I've worked through the kinks here, I plan to release a member API, allowing members to do clever things with their data. For example, members will be able to make their own widgets, not just rely on ours.
How it works. The basic mode of operation is to insert a script as follows:
<script src="http://www.librarything.com/api/json/workinfo.js?ids=*******"></script>The ******* is reserved for the ISBNs you want to look up on LibraryThing, separated by commas. NOTE: This script should be placed at the bottom of the page.
For example, the JSON API Test includes one ISBN-10, one ISBN-13, one LCCN and one OCLC number.
The function LT_addLibraryThinglinks looks for elements (DIVs, SPANs, etc.) with the ID "LT_xxx" where xxx is one of your identifiers. If LibraryThing has a work, it adds "(See on LibraryThing)", with link. If not, it does nothing.
Here's the JavaScript returned for the URL above:
<script src="http://athena.librarything.com/api/json/workinfo.js?ids=0066212898,9780520042728,99030698,ocn8474750911"></script>The script returns a hunk of JavaScript, including both the simple function and the JSON hash with all the book data. The hash is sent to a function of your choosing, or the simple LT_addLibraryThinglinks by default. To name another callback function add &callback= and the function name to the URL.
The function LT_addLibraryThinglinks looks for elements (DIVs, SPANs, etc.) with the ID "LT_xxx" where xxx is one of your identifiers. If LibraryThing has a work, it adds "(See on LibraryThing)", with link. If not, it does nothing.
Here's the JavaScript returned for the URL above:
LT_addLibraryThinglinks(More later. It's 2:48am and need to get to bed. There's much more to say, of course.
{
"0066212898":
{"id":"0066212898","type":"isbn","work":"3702986","link":"http:\/\/www.librarything.com\/work\/3702986","copies":"105","reviews":"7","rating":8.33,"rating_img":"http:\/\/www.librarything.com\/pics\/ss8.gif"},
"9780520042728":
{"id":"9780520042728","type":"isbn","work":"44723","link":"http:\/\/www.librarything.com\/work\/44723","copies":"92","reviews":"3","rating":8.47,"rating_img":"http:\/\/www.librarything.com\/pics\/ss8.gif"},
"99030698":
{"id":"99030698","type":"lccn","work":"32155","link":"http:\/\/www.librarything.com\/work\/32155","copies":"345","reviews":"10","rating":7.8,"rating_img":"http:\/\/www.librarything.com\/pics\/ss8.gif"},
"ocn8474750911":
{"id":"ocn8474750911","type":"oclc","work":"4161224","link":"http:\/\/www.librarything.com\/work\/4161224","copies":"1","reviews":"0","rating":0,"rating_img":""}}
);
Labels: apis, javascript, JSON



9 Comments:
Looks useful! I had trouble getting it to work till I figured out that the script had to be after the DIV and SPAN tags that used it.
I tried to get it to work on LiveJournal, but couldn't, probably because they filter out third-party Javascript (which is sensible).
Oh, good point. Yeah, it should be at the bottom of the page. I'll add that.
LiveJournal filters out all JS. See http://www.librarything.com/blog/2006/12/graphical-widgets-for-lj-and-etc-first.php for our lame soluton.
The hash is sent to a function of your choosing, or the simple LT_addLibraryThinglinks by default.
How do I do that choosing?
Sorry add &callback=FUNCTIONNAME to the URL.
T
Thanks, works well:
sample search on NLA Library Labs
Oh, I should explain that the "rating" is twice what is displayed. That is ratings are 0-5 stars, with halves. So I use 0-10 stars.
Are there different parameters you can pass? I want to implement your tagging system too, but I can't figure out how. Is it part of this API or another one?
thanks
No free tagging API. We sell tagging—and book recommendations—through LibraryThing for Libraries. See /forlibraries.
Perhaps I am missing something obvious - but - is there any way to programatically access my library? (in particular I'd love to get back a JSON list of all my books with a particular tag, that I could manipulate). But I don't see a way to use this or any other LT api to do so...I suppose I could automate an export & filter, but that seems clumsy...
Post a Comment
<< Home