This commit is contained in:
2022-01-27 16:39:45 -06:00
commit 945b0967f9
9 changed files with 3539 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

152
dist/api.js vendored Normal file
View File

@@ -0,0 +1,152 @@
"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')

15
dist/index.js vendored Normal file
View File

@@ -0,0 +1,15 @@
"use strict";
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 api_1 = require("./api");
(() => __awaiter(void 0, void 0, void 0, function* () {
console.log(yield (0, api_1.getPlayer)('asldhasuhdbla'));
}))();

3160
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

42
package.json Normal file
View File

@@ -0,0 +1,42 @@
{
"name": "1800queue",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "node .",
"watch": "npm-watch",
"dev": "npm-watch"
},
"watch": {
"build": {
"patterns": [
"src"
],
"extensions": "ts",
"legacyWatch": true
},
"start": {
"patterns": [
"dist"
],
"extensions": "js",
"legacyWatch": true,
"runOnChangeOnly": true
}
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^17.0.13",
"npm-watch": "^0.11.0",
"typescript": "^4.5.5"
},
"dependencies": {
"@discordjs/rest": "^0.3.0",
"cheerio": "^1.0.0-rc.10",
"discord-api-types": "^0.26.1",
"discord.js": "^13.6.0"
}
}

1
readme.md Normal file
View File

@@ -0,0 +1 @@
# 1800queue

151
src/api.ts Normal file
View File

@@ -0,0 +1,151 @@
import * as cheerio from 'cheerio';
import { IncomingMessage } from 'http';
import http from '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+)%/,
}
type uniteApiData = {
name: string,
id: string,
level: string,
rank: string,
elo: string,
battles: string,
wins: string,
winrate: string
}
/*
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: string): Promise<string> {
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: IncomingMessage) => {
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 = http.request(init, callback);
req.end();
});
}
/**
* interprets the html from getHTML()
* @param name name of player
* @returns player data from site
*/
function readHTML(html: string): uniteApiData {
let metaElems = cheerio.load(html)('meta').toArray(),
foundData: uniteApiData = {
name: "",
id: "",
level: "",
rank: "",
elo: "",
battles: "",
wins: "",
winrate: ""
};
//filter down to just ones named "og:..."
metaElems = metaElems.filter(el => el.attribs.property?.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: uniteApiData): boolean {
if (data.id.length)
return true;
return false;
}
/**
* gets player data from uniteApi
* @param name name of player
* @returns player data
*/
export async function getPlayer(name: string): Promise<uniteApiData|null> {
let html = await getHTML(name);
let data = readHTML(html);
if (verifyData(data))
return data;
return null;
}
//await getPlayer('IanWhysp')

5
src/index.ts Normal file
View File

@@ -0,0 +1,5 @@
import { getPlayer } from "./api";
(async () => {
console.log(await getPlayer('asldhasuhdbla'));
})()

12
tsconfig.json Normal file
View File

@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}