Thursday, December 22, 2011

webProgramming - The Google Book Search API v1 - Part 2

This is my first working code for the small application I'm working on. It would have taken me so much more time if weren't for HJRT.

Note the use of jquery-1.7.1.min.js and jquery.jsonp-2.2.0.min.js

<html>
<head>
<style type="text/css">
    .inputtext { width: 754px; height: 154px; }
</style>
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="jquery.jsonp-2.2.0.min.js"></script>
<script type="text/javascript" >
        function getBook(isbn) {
            $.jsonp({
                "url": "https://www.googleapis.com/books/v1/volumes?callback=?&q=isbn:"+ isbn,
                "success": function(response){
                    console.log("sucess ,isbn: ");
                    console.log(isbn);
                    for (var i = 0; i < response.items.length; i++) {
                    var item = response.items[i];
                    // in production code, item.text should have the HTML entities escaped.
                    document.getElementById("content").innerHTML += " " +isbn+ " | "
                    + item.volumeInfo.industryIdentifiers[0].type + " | "
                    + item.volumeInfo.industryIdentifiers[0].identifier + " | "
                    + item.volumeInfo.industryIdentifiers[1].type + " | "
                    + item.volumeInfo.industryIdentifiers[1].identifier + " | "
                    + item.volumeInfo.title +" | "
                    + item.volumeInfo.authors +" | "
                    + item.volumeInfo.publisher +" | "
                    + item.volumeInfo.publishedDate +" | "
                    + item.volumeInfo.description + " | "
                    + item.volumeInfo.pageCount +" | "
                    + item.volumeInfo.printType +" | "
                    + item.volumeInfo.categories +" | "
                    + item.volumeInfo.averageRating +" | "
                    + item.volumeInfo.ratingsCount + " | "
                    + item.volumeInfo.imageLinks.smallThumbnail +" | "
                    + item.volumeInfo.imageLinks.thumbnail + " | "
                    + item.volumeInfo.language + " | "
                    + item.volumeInfo.previewLink  + " | "
                    + item.volumeInfo.infoLink + " | "
                    + item.saleInfo.country + " | "
                    + item.saleInfo.saleability + " | "
                    + item.saleInfo.isEbook + " ~ ";
                    }
                   
                },
                "error": function(d,msg) {
                    console.log("Error");
                    alert("Could not find book ");
                }
            });
        }
    </script>   
    <script type="text/javascript">
    function parseInput(){
        var input = document.getElementById("isbnList").value.trim();
        console.log(input);
        getBook(input);
    }
    </script>
</head>
<title>Books API Example</title>
Introduce el ISBN
<br>
<textarea id='isbnList' class='inputtext'> </textarea>
<br>
<button type="button" onclick='parseInput()'>Click</button>
<div id="content"></div>

<html>


Firebug was of invaluable help as well
https://addons.mozilla.org/es-es/firefox/addon/firebug/

One point to have in mind is that you'll need an access key from google if you are to use the API on a regular basis.

TODO:
+ to genuinely receive several ISBN as currently is only accepting one
+ trim any new line character that might be inside of any of the fields
http://www.webtoolkit.info/javascript-trim.html

No comments:

Post a Comment