fixed special character bug
This commit is contained in:
107
src/api.ts
107
src/api.ts
@@ -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];
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user