Skip to content

Instantly share code, notes, and snippets.

@msridhar
Created February 25, 2012 23:29
Show Gist options
  • Save msridhar/1911657 to your computer and use it in GitHub Desktop.
Save msridhar/1911657 to your computer and use it in GitHub Desktop.
Compute Average IMDB Rating for TV Show Season using node.js
var jsdom = require('jsdom');
var re = /(\d+)\.(\d+)/;
jsdom.env({
// IMDB show URL + '/eprate' (Simpsons URL below)
html: 'http://www.imdb.com/title/tt0096697/eprate',
scripts: ['http://code.jquery.com/jquery-1.7.1.min.js'],
done: function (errors, window) {
var $ = window.$;
var season2Ratings = [];
$('tr').each(function () {
var entries = $(this).find('td');
if (entries) {
var firstText = $(entries[0]).text();
var m = firstText.match(re);
if (m) {
var season = m[1];
var rating = $(entries[2]).text();
if (season2Ratings[season]) {
season2Ratings[season].push(rating);
} else {
season2Ratings[season] = [rating];
}
}
}
});
season2Ratings.forEach(function (ratings, season) {
var sum = 0,
numRatings = 0;
ratings.forEach(function (rating) {
sum += parseFloat(rating);
numRatings++;
});
console.log((sum / numRatings).toFixed(2) + " average rating for season " + season);
});
process.exit(0);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment