Last active
April 19, 2024 02:26
-
-
Save alyssais/7420289 to your computer and use it in GitHub Desktop.
A script to calculate a leaderboard for Node Knockout 2013.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require("async"), | |
request = require("request"), | |
_ = require("lodash"), | |
cheerio = require("cheerio"); | |
var get = function(url, callback) { | |
request(url, function(error, response, body) { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
callback(cheerio.load(body)) | |
}) | |
}; | |
get("http://nodeknockout.com/entries", function($) { | |
var times = Math.ceil(parseInt($(".count").html().replace(/[^\d]/g, ""), 10) / 30); | |
async.times(times, function(i, next) { | |
get("http://nodeknockout.com/entries?page=" + (i + 1), function($) { | |
async.map($(".teams h2 a").toArray(), function(elem, next) { | |
var slug = $(elem).attr("href").split(/\//)[2], | |
team = { name: $(elem).html() } | |
get("http://nodeknockout.com/iframe/" + slug, function($) { | |
team.votes = parseInt($("#count").html(), 10); | |
next(null, team); | |
}); | |
}, next); | |
}); | |
}, function(error, teams) { | |
_(teams).flatten().sortBy(function(team) { | |
return team.votes; | |
}).reverse().value().forEach(function(team) { | |
console.log(team.votes, team.name); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment