mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7de2d9a95a | ||
![]() |
a143306519 | ||
![]() |
86b81abfd8 | ||
![]() |
8d065742d0 | ||
![]() |
eaa3d4a299 | ||
![]() |
a92fa7fffe | ||
![]() |
90f2939761 | ||
![]() |
12046246ef | ||
![]() |
adf73e59c4 | ||
![]() |
f2fcbad1c4 | ||
![]() |
0f47310814 | ||
![]() |
30530fde01 |
@@ -146,6 +146,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
|
||||
| Kreearra | `kreeArra` |
|
||||
| K'ril Tsutsaroth | `krilTsutsaroth` |
|
||||
| Mimic | `mimic` |
|
||||
| The Nightmare of Ashihama | `nightmare` |
|
||||
| Obor | `obor` |
|
||||
| Sarachnis | `sarachnis` |
|
||||
| Scorpia | `scorpia` |
|
||||
|
@@ -70,6 +70,7 @@ test('Parse CSV to json', () => {
|
||||
625,2391
|
||||
120,2981
|
||||
1,109
|
||||
3,22666
|
||||
26,323
|
||||
201,1101
|
||||
82,3404
|
||||
@@ -157,6 +158,7 @@ test('Parse CSV to json', () => {
|
||||
kreeArra: { rank: 625, score: 2391 },
|
||||
krilTsutsaroth: { rank: 120, score: 2981 },
|
||||
mimic: { rank: 1, score: 109 },
|
||||
nightmare: { rank: 3, score: 22666 },
|
||||
obor: { rank: 26, score: 323 },
|
||||
sarachnis: { rank: 201, score: 1101 },
|
||||
scorpia: { rank: 82, score: 3404 },
|
||||
|
@@ -3,5 +3,6 @@
|
||||
"^.+\\.(t|j)sx?$": "ts-jest"
|
||||
},
|
||||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
||||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
|
||||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "osrs-json-hiscores",
|
||||
"version": "2.0.2",
|
||||
"version": "2.2.0",
|
||||
"description": "The Oldschool Runescape API wrapper that does more!",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
@@ -37,11 +37,11 @@
|
||||
"homepage": "https://github.com/maxswa/osrs-json-hiscores#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"cheerio": "^1.0.0-rc.3"
|
||||
"jsdom": "^16.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cheerio": "^0.22.11",
|
||||
"@types/jest": "^24.0.14",
|
||||
"@types/jsdom": "^16.2.3",
|
||||
"jest": "^24.8.0",
|
||||
"np": "^5.0.3",
|
||||
"prettier": "^1.19.1",
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import * as cheerio from 'cheerio';
|
||||
import {
|
||||
Player,
|
||||
Activity,
|
||||
@@ -14,7 +13,6 @@ import {
|
||||
ActivityName,
|
||||
PlayerActivityRow,
|
||||
Bosses,
|
||||
Boss,
|
||||
} from './types';
|
||||
import {
|
||||
getStatsURL,
|
||||
@@ -30,6 +28,7 @@ import {
|
||||
getActivityPageURL,
|
||||
BOSSES,
|
||||
} from './utils';
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
export async function getStats(rsn: string): Promise<Player> {
|
||||
if (typeof rsn !== 'string') {
|
||||
@@ -46,13 +45,13 @@ export async function getStats(rsn: string): Promise<Player> {
|
||||
axios(getStatsURL('ironman', rsn)).catch(err => err),
|
||||
axios(getStatsURL('hardcore', rsn)).catch(err => err),
|
||||
axios(getStatsURL('ultimate', rsn)).catch(err => err),
|
||||
getRSNFormat(rsn),
|
||||
getRSNFormat(rsn).catch(() => undefined),
|
||||
]);
|
||||
|
||||
const [ironRes, hcRes, ultRes, formattedName] = otherResponses;
|
||||
|
||||
const player: Player = {
|
||||
name: formattedName,
|
||||
name: formattedName || rsn,
|
||||
mode: 'main',
|
||||
dead: false,
|
||||
deulted: false,
|
||||
@@ -145,22 +144,26 @@ export async function getSkillPage(
|
||||
const url = getSkillPageURL(mode, skill, page);
|
||||
|
||||
const response = await axios(url);
|
||||
const $ = cheerio.load(response.data);
|
||||
const playersHTML = $('.personal-hiscores__row').toArray();
|
||||
const dom = new JSDOM(response.data);
|
||||
const playersHTML = dom.window.document.querySelectorAll(
|
||||
'.personal-hiscores__row'
|
||||
);
|
||||
|
||||
const players: PlayerSkillRow[] = playersHTML.map(row => {
|
||||
const cells = row.children.filter(el => el.name === 'td');
|
||||
const [rankEl, nameCell, levelEl, xpEl] = cells;
|
||||
const nameEl = nameCell.children.find(el => el.name === 'a');
|
||||
const isDead = !!nameCell.children.find(el => el.name === 'img');
|
||||
const players: PlayerSkillRow[] = [];
|
||||
playersHTML.forEach(row => {
|
||||
const rankEl = row.querySelector('td');
|
||||
const nameEl = row.querySelector('td a');
|
||||
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),
|
||||
rank: numberFromElement(rankEl),
|
||||
level: numberFromElement(levelEl),
|
||||
xp: numberFromElement(xpEl),
|
||||
dead: isDead,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
return players;
|
||||
@@ -181,21 +184,24 @@ export async function getActivityPage(
|
||||
const url = getActivityPageURL(mode, activity, page);
|
||||
|
||||
const response = await axios(url);
|
||||
const $ = cheerio.load(response.data);
|
||||
const playersHTML = $('.personal-hiscores__row').toArray();
|
||||
const dom = new JSDOM(response.data);
|
||||
const playersHTML = dom.window.document.querySelectorAll(
|
||||
'.personal-hiscores__row'
|
||||
);
|
||||
|
||||
const players: PlayerActivityRow[] = playersHTML.map(row => {
|
||||
const cells = row.children.filter(el => el.name === 'td');
|
||||
const [rankEl, nameCell, scoreEl] = cells;
|
||||
const nameEl = nameCell.children.find(el => el.name === 'a');
|
||||
const isDead = !!nameCell.children.find(el => el.name === 'img');
|
||||
const players: PlayerActivityRow[] = [];
|
||||
playersHTML.forEach(row => {
|
||||
const rankEl = row.querySelector('td');
|
||||
const nameEl = row.querySelector('td a');
|
||||
const scoreEl = row.querySelector('td.left + td');
|
||||
const isDead = !!row.querySelector('td img');
|
||||
|
||||
return {
|
||||
players.push({
|
||||
name: rsnFromElement(nameEl),
|
||||
rank: numberFromElement(rankEl),
|
||||
score: numberFromElement(scoreEl),
|
||||
dead: isDead,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
return players;
|
||||
@@ -213,10 +219,13 @@ export async function getRSNFormat(rsn: string): Promise<string> {
|
||||
const url = getPlayerTableURL('main', rsn);
|
||||
try {
|
||||
const response = await axios(url);
|
||||
const $ = cheerio.load(response.data);
|
||||
const rawName = $('[style="color:#AA0022;"]')[1].children[0].data;
|
||||
if (rawName) {
|
||||
return rawName.replace(/\uFFFD/g, ' ');
|
||||
const dom = new JSDOM(response.data);
|
||||
const spans = dom.window.document.querySelectorAll(
|
||||
'span[style="color:#AA0022;"]'
|
||||
);
|
||||
if (spans.length >= 2) {
|
||||
const nameSpan = spans[1];
|
||||
return rsnFromElement(nameSpan);
|
||||
}
|
||||
throw Error('Player not found');
|
||||
} catch {
|
||||
|
@@ -89,6 +89,7 @@ export type Boss =
|
||||
| 'kreeArra'
|
||||
| 'krilTsutsaroth'
|
||||
| 'mimic'
|
||||
| 'nightmare'
|
||||
| 'obor'
|
||||
| 'sarachnis'
|
||||
| 'scorpia'
|
||||
|
@@ -7,7 +7,7 @@ import {
|
||||
ActivityName,
|
||||
} from '../types';
|
||||
|
||||
export const BASE_URL = 'http://services.runescape.com/m=hiscore_oldschool';
|
||||
export const BASE_URL = 'https://secure.runescape.com/m=hiscore_oldschool';
|
||||
export const STATS_URL = 'index_lite.ws?player=';
|
||||
export const SCORES_URL = 'overall.ws?';
|
||||
|
||||
@@ -97,6 +97,7 @@ export const BOSSES: Boss[] = [
|
||||
'kreeArra',
|
||||
'krilTsutsaroth',
|
||||
'mimic',
|
||||
'nightmare',
|
||||
'obor',
|
||||
'sarachnis',
|
||||
'scorpia',
|
||||
@@ -161,6 +162,7 @@ export const FORMATTED_BOSS_NAMES: FormattedBossNames = {
|
||||
kreeArra: "Kree'Arra",
|
||||
krilTsutsaroth: "K'ril Tsutsaroth",
|
||||
mimic: 'Mimic',
|
||||
nightmare: 'The Nightmare of Ashihama',
|
||||
obor: 'Obor',
|
||||
sarachnis: 'Sarachnis',
|
||||
scorpia: 'Scorpia',
|
||||
|
@@ -35,13 +35,13 @@ export const getActivityPageURL = (
|
||||
activity
|
||||
)}&page=${page}`;
|
||||
|
||||
export const numberFromElement = (el: CheerioElement) => {
|
||||
const innerText = el.firstChild.data;
|
||||
const number = innerText ? innerText.replace(/[\n|,]/g, '') : '-1';
|
||||
export const numberFromElement = (el: Element | null) => {
|
||||
const { innerHTML } = el || {};
|
||||
const number = innerHTML?.replace(/[\n|,]/g, '') ?? '-1';
|
||||
return parseInt(number, 10);
|
||||
};
|
||||
|
||||
export const rsnFromElement = (el: CheerioElement | undefined) => {
|
||||
const innerText = el?.firstChild.data;
|
||||
return innerText ? innerText.replace(/\uFFFD/g, ' ') : '';
|
||||
export const rsnFromElement = (el: Element | null) => {
|
||||
const { innerHTML } = el || {};
|
||||
return innerHTML?.replace(/\uFFFD/g, ' ') || '';
|
||||
};
|
||||
|
Reference in New Issue
Block a user