fixed special character bug

This commit is contained in:
2022-01-31 15:50:25 -06:00
parent 5167a25626
commit 5ee97cfd08
2 changed files with 170 additions and 19 deletions

82
dist/api.js vendored
View File

@@ -38,11 +38,15 @@ const https_1 = __importDefault(require("https"));
//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 ]+) \((.*)\)/i,
//$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+)%/i,
//for non-masters (no elo)
//ogdescription: /pokémon unite : lv\.(\d+) (\w+): class (\d+) *\n battles : (\d+) *\n wins : (\d+) *\n win rate : (\d+)%/i,
ogtitle: /unite api - (.+) \((.*)\)/i,
//$1 = level, $2 = rank, $3 = elo/class (rest is found by splitting each line)
ogdescription: [
//line 1
[
/lv\.(\d+) (\w+) \((\d+)\)/i,
/lv\.(\d+) (\w+): class (\d+)/i //other
],
]
};
/*
og:title
@@ -60,7 +64,7 @@ Pokémon Unite : Lv.40 Master (1741)
* @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, '');
//name = name.replace(/[^\w\d]/g, '');
return new Promise((resolve, reject) => {
const init = {
host: 'uniteapi.dev',
@@ -96,7 +100,8 @@ function readHTML(html) {
id: "",
level: "",
rank: "",
elo: "",
elo: null,
class: null,
battles: "",
wins: "",
winrate: ""
@@ -113,7 +118,62 @@ function readHTML(html) {
}
}
else if (attr.property === 'og:description') {
let data = uniteApiRegex.ogdescription.exec(attr.content);
//all lines
let lines = attr.content.split('\n').map(l => l.trim()), extraLines = [];
//ensure first line is correct
while (lines.length && !/pok.mon unite/i.test(lines[0])) {
let line = lines.shift();
if (line)
extraLines.push(line);
}
if (!lines.length)
throw 'Unable to read data, please try again';
//bring the first lines removed back into the data
lines = [
...lines,
...extraLines.filter(d => d)
];
//line 1
//Pokémon Unite : Lv.40 Veteran: Class 3
//Pokémon Unite : Lv.40 Master (1741)
{
//will be only text after "pokemon unite:"
let line = lines[0].split(':').slice(1).join(':').trim();
let regex = uniteApiRegex.ogdescription[0];
if (regex[0].test(line)) { //is master/has elo
let regexData = line.match(regex[0]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.elo = regexData[3];
}
else { //is not master/has a class
let regexData = line.match(regex[1]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.class = regexData[3];
}
}
lines.shift();
//rest of lines
lines.forEach(line => {
let split = line.split(':').map(l => l.trim()), key = split[0].toLowerCase().replace(/[^\w]/g, ''), value = split[1];
switch (key) {
case 'battles':
foundData.battles = value;
break;
case 'wins':
foundData.wins = value;
break;
case 'winrate':
foundData.winrate = value;
break;
}
});
/*let data = uniteApiRegex.ogdescription.exec(attr.content);
if (data !== null && data.length >= 7) {
foundData.level = data[1];
foundData.rank = data[2];
@@ -121,7 +181,7 @@ function readHTML(html) {
foundData.battles = data[4];
foundData.wins = data[5];
foundData.winrate = data[6];
}
}*/
}
});
return foundData;
@@ -151,6 +211,10 @@ function getPlayer(name) {
});
}
exports.getPlayer = getPlayer;
/**
* calls getPlayer() with the name from the interaction
* @param interaction discord interaction
*/
function getPlayerInteraction(interaction) {
return __awaiter(this, void 0, void 0, function* () {
let username = interaction.options.getString('username', true);

View File

@@ -7,11 +7,16 @@ import http from 'https';
//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 ]+) \((.*)\)/i,
//$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+)%/i,
//for non-masters (no elo)
//ogdescription: /pokémon unite : lv\.(\d+) (\w+): class (\d+) *\n battles : (\d+) *\n wins : (\d+) *\n win rate : (\d+)%/i,
ogtitle: /unite api - (.+) \((.*)\)/i,
//$1 = level, $2 = rank, $3 = elo/class (rest is found by splitting each line)
ogdescription: [
//line 1
[
/lv\.(\d+) (\w+) \((\d+)\)/i, //master
/lv\.(\d+) (\w+): class (\d+)/i //other
],
]
}
type uniteApiData = {
@@ -20,7 +25,8 @@ type uniteApiData = {
level: string,
rank: string,
elo: string,
class: string|null,
elo: string|null,
battles: string,
wins: string,
winrate: string
@@ -43,7 +49,7 @@ Pokémon Unite : Lv.40 Master (1741)
*/
function getHTML(name: string): Promise<string> {
name = name.replace(/[^\w\d]/g, '');
//name = name.replace(/[^\w\d]/g, '');
return new Promise((resolve, reject) => {
@@ -91,7 +97,8 @@ function readHTML(html: string): uniteApiData {
level: "",
rank: "",
elo: "",
elo: null,
class: null,
battles: "",
wins: "",
winrate: ""
@@ -110,7 +117,86 @@ function readHTML(html: string): uniteApiData {
foundData.id = data[2];
}
} else if (attr.property === 'og:description') {
let data = uniteApiRegex.ogdescription.exec(attr.content);
//all lines
let lines = attr.content.split('\n').map(l => l.trim()),
extraLines: string[] = [];
//ensure first line is correct
while (lines.length && !/pok.mon unite/i.test(lines[0])) {
let line = lines.shift();
if (line)
extraLines.push(line);
}
if (!lines.length)
throw 'Unable to read data, please try again';
//bring the first lines removed back into the data
lines = [
...lines,
...extraLines.filter(d => d)
];
//line 1
//Pokémon Unite : Lv.40 Veteran: Class 3
//Pokémon Unite : Lv.40 Master (1741)
{
//will be only text after "pokemon unite:"
let line = lines[0].split(':').slice(1).join(':').trim();
let regex = uniteApiRegex.ogdescription[0];
if (regex[0].test(line)) { //is master/has elo
let regexData = line.match(regex[0]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.elo = regexData[3];
} else { //is not master/has a class
let regexData = line.match(regex[1]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.class = regexData[3];
}
}
lines.shift();
//rest of lines
lines.forEach(line => {
let split = line.split(':').map(l => l.trim()),
key = split[0].toLowerCase().replace(/[^\w]/g, ''),
value = split[1];
switch(key) {
case 'battles':
foundData.battles = value;
break;
case 'wins':
foundData.wins = value;
break;
case 'winrate':
foundData.winrate = value;
break;
}
});
/*let data = uniteApiRegex.ogdescription.exec(attr.content);
if (data !== null && data.length >= 7) {
foundData.level = data[1];
foundData.rank = data[2];
@@ -118,7 +204,8 @@ function readHTML(html: string): uniteApiData {
foundData.battles = data[4];
foundData.wins = data[5];
foundData.winrate = data[6];
}
}*/
}
})