Monday, June 7, 2010

Get IMDB movie rating using JavaScript (Cross domain AJAX solution)

UPDATE: I modified my script to use imdbparser.appspot.com that I built, so the information is always accurate and it works fast.
This query:
https://imdbparser.appspot.com/?callback=foo&context=bar&action=rating&id=tt0411008
Gives this result: foo('bar', '8.7')
So you can write something like that:

<script type="text/javascript">
function foo(context, rating)
{
  alert("The rating is: " + rating);
}
</script>
<script src="https://imdbparser.appspot.com/?callback=foo&context=bar&action=rating&id=tt0411008" type="text/javascript">
</script>

Original Post:



Solution to what exactly?

In JavaScript you aren't suppose to communicate with other sites, such as IMDB. As a result, you can't get the rating of a movie just like that.
For more information:
http://en.wikipedia.org/wiki/Same_origin_policy

Example: Vicky Cristina Barcelona

How to use it in your website?

Put this code in the top of the page:
<script src="http://sites.google.com/site/ycouriel/files/imdbrating.js" type="text/javascript"> </script>
Put this code each time you want to get rating:
<script type="text/javascript"> IMDBRATING.registerTitle("tt0068646");</script>
Finally, put this code in the bottom of the page: 
<script type="text/javascript"> IMDBRATING.processTitles();</script>
You also need to put this CSS if you want the nice stars: imdbrating.css


How it's done?

The trick is to use Google. Consider the following query:
You get in return:
IMDBRATING.ratingCallback( <search results here> );
This wonderful feature of Google allows you to write a query as the source of JavaScript: 
<script src=" <the query above here> " type="text/javascript"> </script>
By parsing the search results, you can easily find the rating.

Notes:
  • This is a JavaScript solution; if you have a server running PHP or something you can put some page like Google's, only instead of search results you return the ratings. This could be wonderful, but all the websites on the Internet that do exactly that didn't work for me or were very slow.
  • The rating you get is sometimes not updated, it entirely depends on how often Google database is updated.

6 comments:

  1. how would you do this for things other than score, e.g. if I wanted to find the runtime of the movie from IMDB?

    ReplyDelete
  2. i added some functionality so it also finds: runtime, title and description
    example:
    https://imdbparser.appspot.com/?callback=foo&context=bar&id=tt1067583
    gives:

    foo("bar", {"rating": "7.0", "description": "A veterinary student abandons his studies after his parents are killed and joins a traveling circus as their vet.", "time": "120", "id": "tt1067583", "title": "Water for Elephants"})

    ReplyDelete
  3. Can't you add one more functionality of numbers of voter?

    ReplyDelete
  4. Hi Yoni,

    I found an issue in the script... this has to do with the parseGoogleData function... the regular expression don't work.

    I replaced the current regexp to "/vote\s*of\s*([0-9\.]+)\s*\/\s*10/i"

    This script works fine for me now. Thank you very much and sorry my bad english :)

    --
    www.gilbertopineda.com

    ReplyDelete
  5. Why do not you use the IMDb Ratings boost service at leeseohits.com. I find it helpful to you

    ReplyDelete
  6. Nice functionality - that was helpful for my website.

    ReplyDelete