liquipedia-nodejs
Version:
**Work in progress**
136 lines (135 loc) • 5.7 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const node_html_parser_1 = require("node-html-parser");
exports.default = (appName) => {
const headers = {
"User-Agent": appName,
"Accept-Encoding": "gzip",
};
const baseUrl = "https://liquipedia.net/dota2/api.php";
const parse = (page) => {
return axios_1.default.get(baseUrl, {
headers,
params: {
action: "parse",
format: "json",
page,
},
});
};
const parseTeamName = (table, side) => {
const teamTitleLink = table.querySelector(`.team-${side} [class^='team-template-image-'] > a`);
const team = teamTitleLink ? teamTitleLink.attrs.title : "TBD";
return team;
};
const parseMatchTable = (table) => {
var _a;
const team1 = parseTeamName(table, "left");
const team2 = parseTeamName(table, "right");
const format = (_a = table.querySelector(".versus abbr")) === null || _a === void 0 ? void 0 : _a.innerText;
const time = +table.querySelector(".timer-object.timer-object-countdown-only").attrs["data-timestamp"];
const tournamentName = table.querySelector(".match-filler").innerText;
return {
team1,
team2,
format,
time,
tournament: {
name: tournamentName,
link: "",
},
};
};
const getUpcomingMatches = (html) => {
var _a;
const matches = (_a = html
.querySelectorAll(".infobox-header")
.filter((header) => header.innerText.trim() === "Upcoming Matches")
.pop()) === null || _a === void 0 ? void 0 : _a.parentNode.parentNode.querySelectorAll("table").map(parseMatchTable);
return matches;
};
const getBracketMatches = (html) => {
const matches = html
.querySelectorAll(".bracket-game")
.map((game) => {
var _a, _b, _c, _d, _e;
const team1 = ((_b = (_a = game
.querySelector(".bracket-team-top")) === null || _a === void 0 ? void 0 : _a.querySelector(".team-template-team-bracket")) === null || _b === void 0 ? void 0 : _b.innerText.trim().replace(" ", "")) || "TBD";
const team2 = ((_d = (_c = game
.querySelector(".bracket-team-bottom")) === null || _c === void 0 ? void 0 : _c.querySelector(".team-template-team-bracket")) === null || _d === void 0 ? void 0 : _d.innerText.trim().replace(" ", "")) || "TBD";
const time = +((_e = game.querySelector(".timer-object")) === null || _e === void 0 ? void 0 : _e.attrs["data-timestamp"]) || 0;
const matches = game.querySelectorAll(".bracket-popup-body-match")
.length;
return {
team1,
team2,
format: `Bo${matches}`,
time,
tournament: {
name: "",
link: "",
},
};
});
return matches.filter((m) => m.time !== 0);
};
const getTournamentInfo = (tournamentName) => __awaiter(void 0, void 0, void 0, function* () {
try {
const response = yield parse(tournamentName);
// console.log(response);
const html = response.data.parse.text["*"];
const parsedHtml = yield node_html_parser_1.default(html);
return {
upcomingMatches: getUpcomingMatches(parsedHtml),
bracketMatches: getBracketMatches(parsedHtml),
};
}
catch (e) {
console.log(e);
return e;
}
});
const getTeamInfo = (teamName) => __awaiter(void 0, void 0, void 0, function* () {
try {
const response = yield parse(teamName);
const html = response.data.parse.text["*"];
const parsedHtml = yield node_html_parser_1.default(html);
return {
upcomingMatches: getUpcomingMatches(parsedHtml),
};
}
catch (e) {
console.log(e);
return e;
}
});
const getUpcomingAndOngoingMatches = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const response = yield parse("Liquipedia:Upcoming_and_ongoing_matches");
const html = response.data.parse.text["*"];
const parsedHtml = yield node_html_parser_1.default(html);
const matchesRows = parsedHtml.querySelectorAll("div[data-toggle-area-content='1'] > .wikitable");
const matches = matchesRows.map(parseMatchTable);
return matches;
}
catch (e) {
console.log(e);
return e;
}
});
return {
getTournamentInfo,
getTeamInfo,
getUpcomingAndOngoingMatches,
};
};
;