Thursday, December 22, 2011

webProgramming - BookInfo.0.5

I'm just making a back up of the last version, some improvements are in order but I achieved the functionality I was expecting, I'm leaving the key, should someone be interested in using it.

Don't forget to download jquery-1.7.1.min.js and jquery.jsonp-2.2.0.min.js in the same folder where the html file lives.

<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 nvl(node){
            return node == undefined ? " " : node;
        }
    </script>
    
<script type="text/javascript" >
        function getBook(isbn) {
            $.jsonp({
                "url": "https://www.googleapis.com/books/v1/volumes?key=amp;callback=?&q=isbn:"+ isbn,
                "success": function(response){
                    console.log("sucess ,isbn: "); 
                    console.log(isbn);
                    if (response.totalItems > 0){
                    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 += " <br> " +isbn+ " | "
                    + nvl(item.volumeInfo.title) +" | "
                    + nvl(item.volumeInfo.authors) +" | "
                    + nvl(item.volumeInfo.publisher) +" | "
                    + nvl(item.volumeInfo.publishedDate) +" | "
                    + nvl(item.volumeInfo.description) + " | "
                    + nvl(item.volumeInfo.pageCount) +" | "
                    + nvl(item.volumeInfo.printType) +" | "
                    + nvl(item.volumeInfo.categories) +" | "
                    + nvl(item.volumeInfo.averageRating) +" | "
                    + nvl(item.volumeInfo.ratingsCount) + " | "
                    + nvl(item.volumeInfo.language) + " | "
                    + nvl(item.volumeInfo.previewLink)  + " | "
                    + nvl(item.volumeInfo.infoLink) + " | ";
                    
                    if (item.volumeInfo.imageLinks != undefined){
                    document.getElementById("content").innerHTML += 
                    nvl(item.volumeInfo.imageLinks.smallThumbnail) + " | " 
                    + nvl(item.volumeInfo.imageLinks.thumbnail) + " | " ;
                    }
                    else document.getElementById("content").innerHTML +=  " | | ";
                    
                    if (item.volumeInfo.industryIdentifiers != undefined){
                    document.getElementById("content").innerHTML += 
                    nvl(item.volumeInfo.industryIdentifiers[0].type) + " | "
                    + nvl(item.volumeInfo.industryIdentifiers[0].identifier) + " | "
                    + nvl(item.volumeInfo.industryIdentifiers[1].type) + " | "
                    + nvl(item.volumeInfo.industryIdentifiers[1].identifier) + " | "
                    }
                    else document.getElementById("content").innerHTML +=  " | | | |";
                    
                    
                    if (item.saleInfo != undefined){
                    document.getElementById("content").innerHTML += 
                    nvl(item.saleInfo.country) + " | "
                    + nvl(item.saleInfo.saleability) + " | "
                    + nvl(item.saleInfo.isEbook) + " | ";
                    
                    }
                    else document.getElementById("content").innerHTML +=  " | | |";
                    
                    
                    document.getElementById("content").innerHTML += "<br> <br>";
                   
                    }
                }
                else
                {
                document.getElementById("content").innerHTML += isbn 
                + "||||||||||||||||||||||| <br> <br>";
                }    
                },
                "error": function(d,msg) {
                    alert("Could not find book ");
                }
            });
        }
    </script>   
    <script type="text/javascript">
    function parseInput(){
        var input = document.getElementById("isbnList").value.split('\n');
        for (i=0; i< input.length ; i++){
        console.log(input[i]);
        getBook(input[i].trim());
    }
    }
    </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> 

No comments:

Post a Comment