Retrieving IMDB movie IDs

So , I recently was working on an interesting problem where I needed a way to retrieve IMDB movie IDs given movie names and use the IDs to further retrieve its Ratings . I used this opportunity to test a long standing code that I wrote to read data in the JSON format.  Well, it worked and made the job a lot quicker and easier. Here's the code

just copy and paste in your notepad/ text editor >> save it as an HTML and RUN!



<!DOCTYPE html>
<html>

<body>

<h1 id="header">IMDB ID RETREIVER...Loading, Please be patient</h1>
<div id="id01"></div>

<script>
var urlArray=
[
"http://www.omdbapi.com/?s=$9.99",
"http://www.omdbapi.com/?s=10 Years",
"http://www.omdbapi.com/?s=10000 BC",
"http://www.omdbapi.com/?s=12",
"http://www.omdbapi.com/?s=12 Rounds",
"http://www.omdbapi.com/?s=127 Hours",
"http://www.omdbapi.com/?s=13 Assassins",
"http://www.omdbapi.com/?s=17 Again",
"http://www.omdbapi.com/?s=17 Girls",
"http://www.omdbapi.com/?s=180 Degrees South Conquerors Of The Useless",
"http://www.omdbapi.com/?s=1911 Revolution",
"http://www.omdbapi.com/?s=2 Days In NY",
"http://www.omdbapi.com/?s=Ambassador",
"http://www.omdbapi.com/?s=Amelia",
"http://www.omdbapi.com/?s=America The Beautiful",
"http://www.omdbapi.com/?s=American",
"http://www.omdbapi.com/?s=American Gangster",
"http://www.omdbapi.com/?s=American Reunion (Universal)",
"http://www.omdbapi.com/?s=American Swing",
"http://www.omdbapi.com/?s=American Teen",
"http://www.omdbapi.com/?s=American Violet",
"http://www.omdbapi.com/?s=Amigo",
"http://www.omdbapi.com/?s=Amour",
"http://www.omdbapi.com/?s=Amreeka",
"http://www.omdbapi.com/?s=An American Affair",
"http://www.omdbapi.com/?s=An American Carol",
"http://www.omdbapi.com/?s=An Education",
"http://www.omdbapi.com/?s=Sugar"
];
var innerHTML="<table><tr><td>######</td><td>Movie</td><td>IMDB ID</td></tr>";
var counter=0;
window.onload = function(){

    var f = (function(){
        var xhr = [];
        for (i = 0; i < urlArray.length; i++)

        {
            (function (i)
                {
                xhr[i] = createCORSRequest("get", urlArray[i]);
                if(xhr[i])
               {
  xhr[i].onreadystatechange = function () 
                    {
                        if (xhr[i].readyState == 4 && xhr[i].status == 200)
                             {
                                 
                                    myFunction(xhr[i].responseText);
                             }
                             else
                             {

                             }
                    };
               }
                xhr[i].send();

            }) (i);




   
        }

    })();




};


function myFunction(response) {

    var arr = JSON.parse(response);

counter++;

var out = "";
        out += "<tr><td>"+counter+"</td>" ; 

if(arr.Response=='False')
{
//alert(urlArray[counter-1])

out += "<td>"+urlArray[counter-1] +
        "</td><td>" +
        'XXXXXXXXXX' +
        "</td></tr>";
}
else
{
out += "<td>"+arr.Search[0].Title +
        "</td><td>" +
        arr.Search[0].imdbID +
        "</td></tr>";
}
          
      

       
    //alert(out);

    innerHTML+=out;
        
   
          if (counter==urlArray.length-1) 
    {

        innerHTML+="</table>"
document.getElementById("id01").innerHTML = innerHTML;
document.getElementById("header").innerHTML = "IMDB ID RETREIVER...Data Retrieved";
    };
}



function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr){
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined"){
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}


</script>

</body>
</html>

No comments: