mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f68c1995c | ||
|
|
7a3f3a0800 | ||
|
|
b16e8ace6e | ||
|
|
09f7805fd4 | ||
|
|
939f4d2721 | ||
|
|
a4577ffb2c | ||
|
|
19ba7e2916 | ||
|
|
7de2d9a95a | ||
|
|
a143306519 | ||
|
|
86b81abfd8 | ||
|
|
8d065742d0 |
@@ -108,6 +108,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
|
|||||||
| Bounty Hunter (Rogue) | `rogueBH` |
|
| Bounty Hunter (Rogue) | `rogueBH` |
|
||||||
| Bounty Hunter (Hunter) | `hunterBH` |
|
| Bounty Hunter (Hunter) | `hunterBH` |
|
||||||
| Last Man Standing | `lastManStanding` |
|
| Last Man Standing | `lastManStanding` |
|
||||||
|
| Soul Wars Zeal | `soulWarsZeal` |
|
||||||
|
|
||||||
### Leagues
|
### Leagues
|
||||||
|
|
||||||
@@ -186,6 +187,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
|
|||||||
leaguePoints: {},
|
leaguePoints: {},
|
||||||
bountyHunter: {},
|
bountyHunter: {},
|
||||||
lastManStanding: {},
|
lastManStanding: {},
|
||||||
|
soulWarsZeal: {},
|
||||||
bosses: {}
|
bosses: {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ test('Parse CSV to json', () => {
|
|||||||
392,250
|
392,250
|
||||||
1,6143
|
1,6143
|
||||||
4814,898
|
4814,898
|
||||||
|
37,225
|
||||||
382,2780
|
382,2780
|
||||||
944,3000
|
944,3000
|
||||||
1981,1452
|
1981,1452
|
||||||
@@ -121,6 +122,7 @@ test('Parse CSV to json', () => {
|
|||||||
hunter: { rank: -1, score: -1 },
|
hunter: { rank: -1, score: -1 },
|
||||||
},
|
},
|
||||||
lastManStanding: { rank: 4814, score: 898 },
|
lastManStanding: { rank: 4814, score: 898 },
|
||||||
|
soulWarsZeal: { rank: 37, score: 225 },
|
||||||
clues: {
|
clues: {
|
||||||
all: { rank: 32, score: 12148 },
|
all: { rank: 32, score: 12148 },
|
||||||
beginner: { rank: 3105, score: 76 },
|
beginner: { rank: 3105, score: 76 },
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "osrs-json-hiscores",
|
"name": "osrs-json-hiscores",
|
||||||
"version": "2.1.0",
|
"version": "2.3.0",
|
||||||
"description": "The Oldschool Runescape API wrapper that does more!",
|
"description": "The Oldschool Runescape API wrapper that does more!",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
@@ -36,12 +36,12 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/maxswa/osrs-json-hiscores#readme",
|
"homepage": "https://github.com/maxswa/osrs-json-hiscores#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.19.0",
|
"axios": "^0.21.1",
|
||||||
"cheerio": "^1.0.0-rc.3"
|
"jsdom": "^16.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/cheerio": "^0.22.11",
|
|
||||||
"@types/jest": "^24.0.14",
|
"@types/jest": "^24.0.14",
|
||||||
|
"@types/jsdom": "^16.2.3",
|
||||||
"jest": "^24.8.0",
|
"jest": "^24.8.0",
|
||||||
"np": "^5.0.3",
|
"np": "^5.0.3",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "^1.19.1",
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import * as cheerio from 'cheerio';
|
|
||||||
import {
|
import {
|
||||||
Player,
|
Player,
|
||||||
Activity,
|
Activity,
|
||||||
@@ -14,7 +13,6 @@ import {
|
|||||||
ActivityName,
|
ActivityName,
|
||||||
PlayerActivityRow,
|
PlayerActivityRow,
|
||||||
Bosses,
|
Bosses,
|
||||||
Boss,
|
|
||||||
} from './types';
|
} from './types';
|
||||||
import {
|
import {
|
||||||
getStatsURL,
|
getStatsURL,
|
||||||
@@ -30,6 +28,7 @@ import {
|
|||||||
getActivityPageURL,
|
getActivityPageURL,
|
||||||
BOSSES,
|
BOSSES,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
import { JSDOM } from 'jsdom';
|
||||||
|
|
||||||
export async function getStats(rsn: string): Promise<Player> {
|
export async function getStats(rsn: string): Promise<Player> {
|
||||||
if (typeof rsn !== 'string') {
|
if (typeof rsn !== 'string') {
|
||||||
@@ -145,22 +144,26 @@ export async function getSkillPage(
|
|||||||
const url = getSkillPageURL(mode, skill, page);
|
const url = getSkillPageURL(mode, skill, page);
|
||||||
|
|
||||||
const response = await axios(url);
|
const response = await axios(url);
|
||||||
const $ = cheerio.load(response.data);
|
const dom = new JSDOM(response.data);
|
||||||
const playersHTML = $('.personal-hiscores__row').toArray();
|
const playersHTML = dom.window.document.querySelectorAll(
|
||||||
|
'.personal-hiscores__row'
|
||||||
|
);
|
||||||
|
|
||||||
const players: PlayerSkillRow[] = playersHTML.map(row => {
|
const players: PlayerSkillRow[] = [];
|
||||||
const cells = row.children.filter(el => el.name === 'td');
|
playersHTML.forEach(row => {
|
||||||
const [rankEl, nameCell, levelEl, xpEl] = cells;
|
const rankEl = row.querySelector('td');
|
||||||
const nameEl = nameCell.children.find(el => el.name === 'a');
|
const nameEl = row.querySelector('td a');
|
||||||
const isDead = !!nameCell.children.find(el => el.name === 'img');
|
const levelEl = row.querySelector('td.left + td');
|
||||||
|
const xpEl = row.querySelector('td.left + td + td');
|
||||||
|
const isDead = !!row.querySelector('td img');
|
||||||
|
|
||||||
return {
|
players.push({
|
||||||
name: rsnFromElement(nameEl),
|
name: rsnFromElement(nameEl),
|
||||||
rank: numberFromElement(rankEl),
|
rank: numberFromElement(rankEl),
|
||||||
level: numberFromElement(levelEl),
|
level: numberFromElement(levelEl),
|
||||||
xp: numberFromElement(xpEl),
|
xp: numberFromElement(xpEl),
|
||||||
dead: isDead,
|
dead: isDead,
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return players;
|
return players;
|
||||||
@@ -181,21 +184,24 @@ export async function getActivityPage(
|
|||||||
const url = getActivityPageURL(mode, activity, page);
|
const url = getActivityPageURL(mode, activity, page);
|
||||||
|
|
||||||
const response = await axios(url);
|
const response = await axios(url);
|
||||||
const $ = cheerio.load(response.data);
|
const dom = new JSDOM(response.data);
|
||||||
const playersHTML = $('.personal-hiscores__row').toArray();
|
const playersHTML = dom.window.document.querySelectorAll(
|
||||||
|
'.personal-hiscores__row'
|
||||||
|
);
|
||||||
|
|
||||||
const players: PlayerActivityRow[] = playersHTML.map(row => {
|
const players: PlayerActivityRow[] = [];
|
||||||
const cells = row.children.filter(el => el.name === 'td');
|
playersHTML.forEach(row => {
|
||||||
const [rankEl, nameCell, scoreEl] = cells;
|
const rankEl = row.querySelector('td');
|
||||||
const nameEl = nameCell.children.find(el => el.name === 'a');
|
const nameEl = row.querySelector('td a');
|
||||||
const isDead = !!nameCell.children.find(el => el.name === 'img');
|
const scoreEl = row.querySelector('td.left + td');
|
||||||
|
const isDead = !!row.querySelector('td img');
|
||||||
|
|
||||||
return {
|
players.push({
|
||||||
name: rsnFromElement(nameEl),
|
name: rsnFromElement(nameEl),
|
||||||
rank: numberFromElement(rankEl),
|
rank: numberFromElement(rankEl),
|
||||||
score: numberFromElement(scoreEl),
|
score: numberFromElement(scoreEl),
|
||||||
dead: isDead,
|
dead: isDead,
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return players;
|
return players;
|
||||||
@@ -213,10 +219,13 @@ export async function getRSNFormat(rsn: string): Promise<string> {
|
|||||||
const url = getPlayerTableURL('main', rsn);
|
const url = getPlayerTableURL('main', rsn);
|
||||||
try {
|
try {
|
||||||
const response = await axios(url);
|
const response = await axios(url);
|
||||||
const $ = cheerio.load(response.data);
|
const dom = new JSDOM(response.data);
|
||||||
const rawName = $('[style="color:#AA0022;"]')[1].children[0].data;
|
const spans = dom.window.document.querySelectorAll(
|
||||||
if (rawName) {
|
'span[style="color:#AA0022;"]'
|
||||||
return rawName.replace(/\uFFFD/g, ' ');
|
);
|
||||||
|
if (spans.length >= 2) {
|
||||||
|
const nameSpan = spans[1];
|
||||||
|
return rsnFromElement(nameSpan);
|
||||||
}
|
}
|
||||||
throw Error('Player not found');
|
throw Error('Player not found');
|
||||||
} catch {
|
} catch {
|
||||||
@@ -256,7 +265,7 @@ export function parseStats(csv: string): Stats {
|
|||||||
const [leaguePoints] = activityObjects.splice(0, 1);
|
const [leaguePoints] = activityObjects.splice(0, 1);
|
||||||
const bhObjects = activityObjects.splice(0, BH_MODES.length);
|
const bhObjects = activityObjects.splice(0, BH_MODES.length);
|
||||||
const clueObjects = activityObjects.splice(0, CLUES.length);
|
const clueObjects = activityObjects.splice(0, CLUES.length);
|
||||||
const [lastManStanding] = activityObjects.splice(0, 1);
|
const [lastManStanding, soulWarsZeal] = activityObjects.splice(0, 2);
|
||||||
const bossObjects = activityObjects.splice(0, BOSSES.length);
|
const bossObjects = activityObjects.splice(0, BOSSES.length);
|
||||||
|
|
||||||
const skills: Skills = skillObjects.reduce<Skills>((prev, curr, index) => {
|
const skills: Skills = skillObjects.reduce<Skills>((prev, curr, index) => {
|
||||||
@@ -288,6 +297,7 @@ export function parseStats(csv: string): Stats {
|
|||||||
leaguePoints,
|
leaguePoints,
|
||||||
bountyHunter,
|
bountyHunter,
|
||||||
lastManStanding,
|
lastManStanding,
|
||||||
|
soulWarsZeal,
|
||||||
clues,
|
clues,
|
||||||
bosses,
|
bosses,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ export type ActivityName =
|
|||||||
| 'hunterBH'
|
| 'hunterBH'
|
||||||
| 'rogueBH'
|
| 'rogueBH'
|
||||||
| 'lastManStanding'
|
| 'lastManStanding'
|
||||||
|
| 'soulWarsZeal'
|
||||||
| 'allClues'
|
| 'allClues'
|
||||||
| 'beginnerClues'
|
| 'beginnerClues'
|
||||||
| 'easyClues'
|
| 'easyClues'
|
||||||
@@ -129,6 +130,7 @@ export interface Stats {
|
|||||||
leaguePoints: Activity;
|
leaguePoints: Activity;
|
||||||
bountyHunter: BH;
|
bountyHunter: BH;
|
||||||
lastManStanding: Activity;
|
lastManStanding: Activity;
|
||||||
|
soulWarsZeal: Activity;
|
||||||
bosses: Bosses;
|
bosses: Bosses;
|
||||||
}
|
}
|
||||||
export type Modes = { [M in Gamemode]?: Stats };
|
export type Modes = { [M in Gamemode]?: Stats };
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ export const ACTIVITIES: ActivityName[] = [
|
|||||||
'eliteClues',
|
'eliteClues',
|
||||||
'masterClues',
|
'masterClues',
|
||||||
'lastManStanding',
|
'lastManStanding',
|
||||||
|
'soulWarsZeal',
|
||||||
...BOSSES,
|
...BOSSES,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -236,4 +237,5 @@ export const FORMATTED_BH_NAMES: FormattedBHNames = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const FORMATTED_LMS = 'Last Man Standing';
|
export const FORMATTED_LMS = 'Last Man Standing';
|
||||||
|
export const FORMATTED_SOUL_WARS = 'Soul Wars Zeal';
|
||||||
export const FORMATTED_LEAGUE_POINTS = 'League Points';
|
export const FORMATTED_LEAGUE_POINTS = 'League Points';
|
||||||
|
|||||||
@@ -35,13 +35,13 @@ export const getActivityPageURL = (
|
|||||||
activity
|
activity
|
||||||
)}&page=${page}`;
|
)}&page=${page}`;
|
||||||
|
|
||||||
export const numberFromElement = (el: CheerioElement) => {
|
export const numberFromElement = (el: Element | null) => {
|
||||||
const innerText = el.firstChild.data;
|
const { innerHTML } = el || {};
|
||||||
const number = innerText ? innerText.replace(/[\n|,]/g, '') : '-1';
|
const number = innerHTML?.replace(/[\n|,]/g, '') ?? '-1';
|
||||||
return parseInt(number, 10);
|
return parseInt(number, 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const rsnFromElement = (el: CheerioElement | undefined) => {
|
export const rsnFromElement = (el: Element | null) => {
|
||||||
const innerText = el?.firstChild.data;
|
const { innerHTML } = el || {};
|
||||||
return innerText ? innerText.replace(/\uFFFD/g, ' ') : '';
|
return innerHTML?.replace(/\uFFFD/g, ' ') || '';
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user