Skip to content

Instantly share code, notes, and snippets.

@madr
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madr/11209778 to your computer and use it in GitHub Desktop.
Save madr/11209778 to your computer and use it in GitHub Desktop.
Dota stats
/*jslint indent: 2, devel: true, browser: true */
(function () {
"use strict";
function scrape(stopAt, startAt) {
var iframe = document.createElement("iframe"),
games = [],
heroes = {},
wins = 0,
page = 1,
heroesPlayed,
heroesHiScore,
gamesLen,
winRate;
stopAt = new Date(stopAt + "T00:00:00");
startAt = new Date(startAt + "T23:59:59");
iframe.height = 0;
iframe.width = 0;
iframe.onload = function () {
var i,
row,
endReached,
hero,
kda,
won,
date,
max,
nextPage,
res = "",
sortedHeroes = [],
games3 = [],
games2 = [],
game1 = [],
heroScore,
rows = iframe.contentDocument.querySelectorAll("#page-content table tbody tr");
for (i = 0, max = rows.length; i < max; i += 1) {
row = rows[i];
if (row.classList.contains("inactive")) { continue; }
endReached = false;
hero = row.querySelector(".cell-xlarge > a").innerHTML;
kda = row.querySelectorAll(".kda-record .value");
won = !!row.querySelector("a.won");
date = new Date(row.querySelector("time").getAttribute("datetime"));
if ((stopAt - date) > 0) { endReached = true; break; }
if ((date - startAt) <= 0) {
games.push({
hero: hero,
kda: [
kda[0].innerHTML,
kda[1].innerHTML,
kda[2].innerHTML
],
date: date,
won: won
});
if (!heroes[hero]) { heroes[hero] = [0, 0]; }
heroes[hero][0] += 1;
if (won) {
wins += 1;
heroes[hero][1] += 1;
}
}
}
nextPage = iframe.contentDocument.querySelector(".next a[rel=next]");
if (!endReached && nextPage) {
iframe.src = nextPage.href;
} else {
for (hero in heroes) {
if (heroes.hasOwnProperty(hero)) {
if (heroes[hero][0] === 3) { games3.push(hero); }
if (heroes[hero][0] === 2) { games2.push(hero); }
if (heroes[hero][0] === 1) { game1.push(hero); }
if (heroes[hero][0] > 3) {
sortedHeroes.push([hero, heroes[hero][0], heroes[hero][1]]);
}
}
}
heroesPlayed = sortedHeroes.length + games3.length + games2.length + game1.length;
sortedHeroes.sort(function (a, b) { return b[1] - a[1]; });
games3.sort();
games2.sort();
game1.sort();
heroScore = "";
for (i = 0, max = sortedHeroes.length; i < max; i += 1) {
heroScore += "\n<tr><th>" + sortedHeroes[i][0] + "</th><td>" + sortedHeroes[i][1] +
"</td><td>" + Math.round((sortedHeroes[i][2] / sortedHeroes[i][1]) * 100) + "%</td></tr>";
}
heroesHiScore = heroScore;
gamesLen = games.length;
winRate = Math.floor((wins / games.length) * 100) + "%";
res += "Games: " + gamesLen;
res += "\nWon games: " + wins;
res += "\nWin rate: " + winRate;
res += "\nHeroes played: " + heroesPlayed;
res += "\nHeroes HiScore: " + heroesHiScore;
res += "\nTre matcher: " + games3.join(", ");
res += "\nTvå matcher: " + games2.join(", ");
res += "\nEn match: " + game1.join(", ");
window.parent.console.log(res);
window.parent.res = res;
return res;
}
};
document.body.appendChild(iframe);
iframe.src = location.pathname + "?page=" + page;
}
scrape(prompt("From:"), prompt("To:"));
}());
/* npm install nightmare */
var Nightmare = require('nightmare'), stopAt, startAt;
if (process.argv.length == 4) {
stopAt = process.argv[2];
startAt = process.argv[3];
}
if (process.argv[2] === "q1") {
stopAt = "2014-01-01";
startAt = "2014-03-31";
}
if (process.argv[2] === "q2") {
stopAt = "2014-04-01";
startAt = "2014-06-30";
}
if (process.argv[2] === "q3") {
stopAt = "2014-07-01";
startAt = "2014-09-30";
}
if (process.argv[2] === "q4") {
stopAt = "2014-10-01";
startAt = "2014-12-31";
}
var doit = function () {
function scrape(stopAt, startAt) {
var iframe = document.createElement("iframe"),
games = [],
heroes = {},
wins = 0,
page = 1,
heroesPlayed,
heroesHiScore,
gamesLen,
winRate;
stopAt = new Date(stopAt + "T00:00:00");
startAt = new Date(startAt + "T23:59:59");
iframe.height = 0;
iframe.width = 0;
iframe.onload = function () {
var i,
row,
endReached,
hero,
kda,
won,
date,
max,
nextPage,
res = "",
sortedHeroes = [],
games3 = [],
games2 = [],
game1 = [],
heroScore,
rows = iframe.contentDocument.querySelectorAll("#page-content table tbody tr");
for (i = 0, max = rows.length; i < max; i += 1) {
row = rows[i];
if (row.classList.contains("inactive")) { continue; }
endReached = false;
hero = row.querySelector(".cell-xlarge > a").innerHTML;
kda = row.querySelectorAll(".kda-record .value");
won = !!row.querySelector("a.won");
date = new Date(row.querySelector("time").getAttribute("datetime"));
if ((stopAt - date) > 0) { endReached = true; break; }
if ((date - startAt) <= 0) {
games.push({
hero: hero,
kda: [
kda[0].innerHTML,
kda[1].innerHTML,
kda[2].innerHTML
],
date: date,
won: won
});
if (!heroes[hero]) { heroes[hero] = [0, 0]; }
heroes[hero][0] += 1;
if (won) {
wins += 1;
heroes[hero][1] += 1;
}
}
}
nextPage = iframe.contentDocument.querySelector(".next a[rel=next]");
if (!endReached && nextPage) {
iframe.src = nextPage.href;
} else {
for (hero in heroes) {
if (heroes.hasOwnProperty(hero)) {
if (heroes[hero][0] === 3) { games3.push(hero); }
if (heroes[hero][0] === 2) { games2.push(hero); }
if (heroes[hero][0] === 1) { game1.push(hero); }
if (heroes[hero][0] > 3) {
sortedHeroes.push([hero, heroes[hero][0], heroes[hero][1]]);
}
}
}
heroesPlayed = sortedHeroes.length + games3.length + games2.length + game1.length;
sortedHeroes.sort(function (a, b) { return b[1] - a[1]; });
games3.sort();
games2.sort();
game1.sort();
heroScore = "";
for (i = 0, max = sortedHeroes.length; i < max; i += 1) {
heroScore += "\n<tr><th>" + sortedHeroes[i][0] + "</th><td>" + sortedHeroes[i][1] +
"</td><td>" + Math.round((sortedHeroes[i][2] / sortedHeroes[i][1]) * 100) + "%</td></tr>";
}
heroesHiScore = heroScore;
gamesLen = games.length;
winRate = Math.floor((wins / games.length) * 100) + "%";
res += "====================================";
res += "\nGames: " + gamesLen;
res += "\nWon games: " + wins;
res += "\nWin rate: " + winRate;
res += "\nHeroes played: " + heroesPlayed;
res += "\n\nHeroes HiScore: " + heroesHiScore;
res += "\n\n3 matcher: " + games3.join(", ");
res += "\n\n2 matcher: " + games2.join(", ");
res += "\n\n1 match: " + game1.join(", ");
window.parent.console.log(res);
window.parent.res = res;
return res;
}
};
document.body.appendChild(iframe);
iframe.src = location.pathname + "?page=" + page;
}
scrape(window.stopAt, window.startAt);
};
new Nightmare()
.goto('http://www.dotabuff.com/players/42850321/matches')
.evaluate(function () {
var input = document.createElement("input");
input.id = "sircrapalot";
document.body.appendChild(input);
}, function (res) {})
.type("#sircrapalot", stopAt) // stopAt = process.argv[2]
.evaluate(function () {
window.stopAt = document.querySelector("#sircrapalot").value;
return window.stopAt;
}, function (res) { console.log("stops at:", res); })
.type("#sircrapalot", startAt) // startAt = process.argv[3]
.evaluate(function () {
window.startAt = document.querySelector("#sircrapalot").value;
return window.startAt;
}, function (res) { console.log("starts at:", res); })
// below script begins
.evaluate(doit, function (res) {
console.info("scriptet startat! väntar nu 60 sekunder ...");
})
.wait(10 * 1000)
.evaluate(function () {}, function (res) { console.log("50 sekunder ..."); } )
.wait(10 * 1000)
.evaluate(function () {}, function (res) { console.log("40 sekunder ..."); } )
.wait(10 * 1000)
.evaluate(function () {}, function (res) { console.log("30 sekunder ..."); } )
.wait(10 * 1000)
.evaluate(function () {}, function (res) { console.log("20 sekunder ..."); } )
.wait(10 * 1000)
.evaluate(function () {}, function (res) { console.log("10 sekunder ..."); } )
.wait(5 * 1000)
.evaluate(function () {}, function (res) { console.log("5 sekunder ..."); } )
.wait(5 * 1000)
.evaluate(function () { return window.res; }, function (res) { console.log(res); })
.run(function (err, nightmare) {
if (err) return console.log(err);
console.log('\nhämtat från dotabuff.com');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment