Archive for the ‘javascript’ Category

Sunday, July 6th, 2008

Introducing the LibraryThing books API

See bottom of post for updates…

I’ve just finished a first draft of a JSON-based API for book data, created a test page and typed up some basic documentation.

What is this for? The API gives you Javascript access to your book data. The most obvious use of this would be to create new, much better widgets. At first, we expect this to interest programmers, but as new widgets are developed, non-programmers will get cool things. I started by redoing our traditional widgets in a new way here. That’s the base, not the ceiling!

How does it work? Every user can retrieve their data, in JSON format—basically as a ready-made JavaScript data structure. You control what is returned—books, tags, ratings, etc.—how it’s sorted and so forth. By default we give you a standard library of functions to parse and display the data. You can use it, build on it or start from scratch. Find out more here.

What’s great? All our code for processing the JSON API has been and will be released as open source—available for use, reuse and modification. Better—since we’re not the best programmers, particularly in JavaScript!—we are requiring any software that builds upon the API to be released under similar terms, so everyone can take advantage of improvements and advances. 

Does this make code look sexy?

What’s the catch? The API is not intended for making backups or exporting your data to other programs. For that, use our CSV and TSV export functions, from the Tools tab. We are licensing the JSON API for browser-use only. This is about our data licenses. In-browser widgets have never drawn ire from our data providers.

Where can this go? This is just getting started. Everything can be expanded and improved. As members want new or different data, I will be only too happy to add it to the API. But the most interesting development will probably come from members, not LibraryThing employees.

I have created a LibraryThing API Development group to discuss the API, work through code and come up with new ideas.

At a minimum, I can see:

  • New widget types, like widgets showing your most recent reviews.
  • Widgets that take you to libraries, and other places other than LibraryThing. (Libraries have been clamoring for this for ages. Many use LibraryThing to feature new books on the website, and want the links to go to their catalog, not LibraryThing.)
  • New result sets, for your tags or authors (separate from our books), your book’s works, series info, etc.
  • Integration with other JS-based APIs, like Google Book Search.

What if I’m not a programmer? No problem. Come and LibraryThing API Developmenttell us what you want. We’ll help you, or maybe someone else will.


UPDATE: I’ve made some changes to the programming, changing how the code is structured and adding result sets for reading dates. We also have the first outside use of the API, a very promising—if not perfect—cover flip test by MMcM (here). Follow what’s going on in the LibraryThing API Development group.

Labels: apis, books api, javascript, json, member projects

Tuesday, March 25th, 2008

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:

<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.

<script src="http://www.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(
{
"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":""}}
);

More later. It’s 2:48am and need to get to bed. There’s much more to say, of course.

Labels: apis, javascript, json

Saturday, March 15th, 2008

Free covers for your library, from Google

On Wednesday we added integration with Google Book Search, and talked about it on the main blog. We did it together with a number of cool libraries.

My thoughts are still percolating, but I wanted to throw out a piece of my ham-handed JavaScript code. The code gives your library covers, something libraries usually pay for.

This basic grabs cover images from Google. You feed it an ISBN and it gets the cover. It doesn’t link to them. Would they mind? Maybe.

<div id="gbsthumbnail"></div>

<script type="text/javascript">

/* GBS Cover Script by Tim Spalding/LibraryThing */

function addTheCover(booksInfo)
{
for (i in booksInfo)
{
var book = booksInfo[i];
if (book.thumbnail_url != undefined)
{
document.getElementById('gbsthumbnail').innerHTML =
'<img src="' + book.thumbnail_url + '"/>';
}
}
}

</script>

<script src="http://books.google.com/books?jscmd=viewapi&bibkeys=ISBN:0670880728&callback=addTheCover"></script>

Here’s a version that links to them, but only if they have a full version. Surely they wouldn’t mind this.

<div id="gbsthumbnail"></div>
<div id="gbslink"></div>

<script type="text/javascript">

/* GBS Cover Script by Tim Spalding/LibraryThing */

function addTheCover(booksInfo)
{
var gbsnameA = new Array("No information", "Book info", "Partial view", "Full view");

for (i in booksInfo)
{
var book = booksInfo[i];

var quality = 0;
if(book.preview == "noview") { quality = 1; }
if(book.preview == "partial") { quality = 2; }
if(book.preview == "full") { quality = 3; }

if (book.thumbnail_url != undefined)
{
document.getElementById('gbsthumbnail').innerHTML =
'<img src="' + book.thumbnail_url + '">';
}
if (quality > 3)
{
document.getElementById('gbslink').innerHTML =
"<a href='" + book.preview_url + "'>" + "Google Books: " + gbsnameA[quality] + "</a>";
}
}
}

</script>

<script src="http://books.google.com/books?jscmd=viewapi&bibkeys=ISBN:0670880728&callback=addTheCover"></script>

So, book covers for the price of an occasional link to Google. Sounds like a good deal to me!

If this saves your library money, consider getting LibraryThing for Libraries. We’re clever all over.

Labels: code, gbs, google book search, javascript