JSON Books API : Standard library
From WikiThing
<<back to LibraryThing JSON Books API
All code on this page is covered by the open-source license shown detailed on LibraryThing JSON Books API.
Note: Needless to say, this isn't a live copy of the code. But we'll watch out for changes and push them live as warranted. When editing, try to add to the code or improve how the code works, not change what it does. Although the whole API is very much beta, we should move to stable functionality whenever possible.
// Make coverimage
function LT_API_coverimage(book)
{
if(book.cover)
{
// make cover link
var coverlink = document.createElement("a");
coverlink.setAttribute('class','LT_cover');
coverlink.setAttribute('href', 'http://www.librarything.com/work/book/'+book.book_id);
// make cover image
var coverimage = document.createElement("img");
coverimage.setAttribute('src', book.cover);
coverimage.setAttribute('title', book.title);
// put the image in the link
coverlink.appendChild(coverimage);
return coverlink;
}
else
{
return null;
}
}
// Make a title and author link
function LT_API_titleandauthorlink(book)
{
var titleandauthorlink = document.createElement("span");
titleandauthorlink.setAttribute('class','LT_titleandauthor');
if(book.title)
{
var titlelink = document.createElement("a");
titlelink.setAttribute('class','LT_title');
titlelink.setAttribute('href', 'http://www.librarything.com/work/book/'+book.book_id);
titlelink.textContent = book.title;
titleandauthorlink.appendChild(titlelink);
}
if(book.author_code)
{
var byspan = document.createElement("span");
byspan.textContent = ' by ';
titleandauthorlink.appendChild(byspan);
var authorlink = document.createElement("a");
authorlink.setAttribute('href', 'http://www.librarything.com/author/'+book.author_code);
authorlink.setAttribute('class','LT_author');
authorlink.textContent = book.author_fl;
titleandauthorlink.appendChild(authorlink);
}
return titleandauthorlink;
}
// Make a title and author link
function LT_API_getbookreview(book)
{
if (book.bookreview)
{
var bookreview = document.createElement('div');
bookreview.setAttribute('class','LT_bookreview');
bookreview.innerHTML = book.bookreview;
return bookreview;
}
else
{
return null;
}
}
// Make a title and author link
function LT_API_bookreviewlink(book)
{
if (book.hasreview && book.hasreview == 1)
{
var bookreviewlink = document.createElement("span");
bookreviewlink.setAttribute('class','LT_reviewlink');
// cheating
var readreviewlink = ' (<a href="http://www.librarything.com/review/'+book.book_id+'">' +
'read review' +
'</a>)';
bookreviewlink.innerHTML = readreviewlink;
return bookreviewlink;
}
else
{
return null;
}
}
function LT_API_getrating(book)
{
if (book.rating)
{
var ratingimage = document.createElement("img");
ratingimage.setAttribute('class','LT_rating');
var url = 'http://static.librarything.com/pics/ss' + (book.rating*2) + '.gif';
ratingimage.setAttribute('src', url);
ratingimage.setAttribute('title', book.rating + ' <? echo translate('stars', null, true); ?>');
return ratingimage;
}
else
{
return null;
}
}
// The main callback
function LT_API_dowidget(ltdata)
{
var LT_widget = document.getElementById('LT_widget');
for (i in ltdata.books)
{
var book = ltdata.books[i];
if (book.book_id)
{
// create item div
var itemdiv = document.createElement("div");
itemdiv.setAttribute('class', 'LT_item');
// put cover image in
if (book.cover)
{
itemdiv.appendChild(LT_API_coverimage(book));
}
// create title and author
var titleandauthor = LT_API_titleandauthorlink(book);
titleandauthor.id = book.book_id
itemdiv.appendChild(titleandauthor);
if(book.hasreview && book.hasreview == 1)
{
itemdiv.appendChild(LT_API_bookreviewlink(book));
}
if(book.rating)
{
itemdiv.appendChild(LT_API_getrating(book));
}
if(book.bookreview)
{
itemdiv.appendChild(LT_API_getbookreview(book));
}
LT_widget.appendChild(itemdiv);
}
}
}
//Convert the default LibraryThing data into a standard indexable array (complete with a .length property and everything).
function LT_API_toArray(ltdata)
{
var books = [];
for(var i in data.books)
{
books.push(ltdata.books[i]);
}
return books;
}