Skip to content

Instantly share code, notes, and snippets.

@dound
Created July 20, 2014 22:16
Show Gist options
  • Save dound/cc57baa26daeb1805828 to your computer and use it in GitHub Desktop.
Save dound/cc57baa26daeb1805828 to your computer and use it in GitHub Desktop.
script to parse results from www.runraceresults.com
// Quick and dirty script for fetching and parsing results from www.runraceresults.com
// Intended to be run from the Chrome console
// load jquery
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// Parses a page of results from the results-container
var parseResultsPage = function(body) {
// second table has the individual results
var tables = $(body).find('table');
var rows = $(tables[1]).find('tr');
var ret = [];
for (var i=1; i<rows.length; i++) { // skip header row
var cols = $(rows[i]).find('td');
var place = parseInt($(cols[0]).text(), 10);
var sexPlace = parseInt($(cols[8]).text(), 10);
var sexAndAge = $(cols[7]).text().split("-");
var sex = sexAndAge[0];
var age = parseInt(sexAndAge[1], 10);
var netTimePieces = $(cols[4]).text().split(":");
var netSecs = parseInt(netTimePieces[0], 10) * 3600 + parseInt(netTimePieces[1], 10) * 60 + parseInt(netTimePieces[2], 10);
ret.push({
"place": place,
"gender": sex,
"age": age,
"netSecs": netSecs
});
}
return ret;
};
// parse all results from all pages of a particular event and race (e.g., SFM's 5K)
var parseAllResults = function(eventID, raceID, numPages) {
var allResults = [];
var done = 0;
for (var i=0; i<numPages; i++) {
(function () { // closure so success() func refers to the right page #
var page = i + 1;
var startNum = 1 + i * 100;
var url = 'https://www.runraceresults.com/Secure/raceResultsAPI.cfm?do=race%3Aresults%3Aoneclick&EVID=' + eventID + '&RCID=' + raceID + '&SROW=' + startNum + '&TYPE=overall';
$.get(url).success(function (data) {
var results = parseResultsPage(data);
console.log("got results for page " + page + " (" + results.length + ") for race " + eventID + "/" + raceID);
for (var i=0; i < results.length; i++) {
allResults.push(results[i]);
}
done += 1;
if (done === numPages) {
console.log("done with race " + eventID + "/" + raceID);
}
});
}());
}
return allResults;
};
// give jQuery a chance to load
setTimeout(function () {
// fetch and parse results for SFM 2013 1st half marathon (RCID==2==1st halfm)
SF1stHalfResults2013 = parseAllResults('RCLF2013', '2', 61);
// fetch and parse results for SFM 2013 full marathon
SFMResults2013 = parseAllResults('RCLF2013', '1', 59);
}, 1000);
// to use the results elsewhere, consider doing: copy(JSON.stringify(SFMResults2013))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment