Tuesday, April 15, 2008

LibraryThing's JSON API

This morning I was tinkering with LibraryThing's JSON API just to demonstrate a proof of concept to some people internally. I used FireBug to take a look at what the service was actually returning. Here's the bare-bones script.

<html>
<head>
<title>LibraryThing Tests</title>
</head>
<body>
<p>This is an example of information we can get from LibraryThing.</p>
<h3>Christmas on Television</h3>
<script language="javascript" type="text/javascript">
function LTHandler(LTData)
{
for(i in LTData)
{
var book = LTData[i];
// Just display all the data we know might be there.
document.write("<p>");
if(book.id) document.write("<b>ID:</b> " + book.id);
if(book.type) document.write(" (" + book.type.toUpperCase() + ")<br />");
else document.write("<br />");
if(book.work) document.write("<b>LibraryThing (LT) work id:</b> " +
book.work + "<br />");
if(book.link) document.write("<b>LT link:</b> <a target='_blank' href='" +
book.link + "'>" + book.link + "</a><br />");
if(book.copies) document.write("<b>LT copies:</b> " + book.copies +
"<br />");
if(book.reviews) document.write("<b>LT reviews:</b> " + book.reviews +
"<br />");
if(book.rating) document.write("<b>LT rating:</b> " +
Math.round(book.rating/2));
if(book.rating_img) document.write(" or <img src='" + book.rating_img + "'/>");
document.write("</p>");
}
}
<script src="http://www.librarything.com/api/json/workinfo.js?ids=0785271295&callback=LTHandler">
</script>
</body>
</html>

No comments: