"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPlayer = void 0; const cheerio = __importStar(require("cheerio")); const https_1 = __importDefault(require("https")); //making long regex rather than splitting the string at ":" because regex would be easier to debug than logic in the case the website changes //while names cant have spaces, the name slot in ogtitle could be shown as "No Player Name" which has spaces const uniteApiRegex = { //$1 = name, $2 = id ogtitle: /Unite API - ([\w\d ]+) \((.*)\)/, //$1 = level, $2 = rank, $3 = elo, $4 = battles, $5 = wins, $6 = win rate ogdescription: /Pokémon Unite : Lv.(\d+) (\w+) \((\d+)\)\n Battles : (\d+)\n Wins : (\d+)\n Win Rate : (\d+)%/, }; /* og:title Unite API - IanWhysp (X188GF7) og:description Pokémon Unite : Lv.40 Master (1741) Battles : 1089 Wins : 576 Win Rate : 52% */ /** * gets the html of the uniteApi page for the player * @param name name of player * @returns the html of the page through a promise (rejects if the page status is not 200) */ function getHTML(name) { name = name.replace(/[^\w\d]/g, ''); return new Promise((resolve, reject) => { const init = { host: 'uniteapi.dev', path: `/p/${encodeURIComponent(name)}`, method: 'GET', }; const callback = (response) => { if (response.statusCode !== 200) { reject(`HTTP ERROR ${response.statusCode}: ${response.statusMessage}`); return; } let result = Buffer.alloc(0); response.on('data', function (chunk) { result = Buffer.concat([result, chunk]); }); response.on('end', function () { // result has response body buffer resolve(result.toString()); }); }; const req = https_1.default.request(init, callback); req.end(); }); } /** * interprets the html from getHTML() * @param name name of player * @returns player data from site */ function readHTML(html) { let metaElems = cheerio.load(html)('meta').toArray(), foundData = { name: "", id: "", level: "", rank: "", elo: "", battles: "", wins: "", winrate: "" }; //filter down to just ones named "og:..." metaElems = metaElems.filter(el => { var _a; return (_a = el.attribs.property) === null || _a === void 0 ? void 0 : _a.startsWith('og:'); }); metaElems.forEach(el => { let attr = el.attribs; if (attr.property === 'og:title') { let data = uniteApiRegex.ogtitle.exec(attr.content); if (data !== null && data.length >= 3) { foundData.name = data[1]; foundData.id = data[2]; } } else if (attr.property === 'og:description') { let data = uniteApiRegex.ogdescription.exec(attr.content); if (data !== null && data.length >= 7) { foundData.level = data[1]; foundData.rank = data[2]; foundData.elo = data[3]; foundData.battles = data[4]; foundData.wins = data[5]; foundData.winrate = data[6]; } } }); return foundData; } /** * verifies the data * @param data uniteApi data * @returns boolean, valid or invalid */ function verifyData(data) { if (data.id.length) return true; return false; } /** * gets player data from uniteApi * @param name name of player * @returns player data */ function getPlayer(name) { return __awaiter(this, void 0, void 0, function* () { let html = yield getHTML(name); let data = readHTML(html); if (verifyData(data)) return data; return null; }); } exports.getPlayer = getPlayer; //await getPlayer('IanWhysp')