mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2f788fabf6 | ||
![]() |
0f5aea795c | ||
![]() |
7acb84e310 | ||
![]() |
9619bd2a2b | ||
![]() |
55abd9f800 | ||
![]() |
cf8a4cc26e | ||
![]() |
0f68c1995c | ||
![]() |
ae47df31f1 | ||
![]() |
7a3f3a0800 | ||
![]() |
b16e8ace6e | ||
![]() |
09f7805fd4 | ||
![]() |
939f4d2721 | ||
![]() |
a4577ffb2c | ||
![]() |
19ba7e2916 | ||
![]() |
7de2d9a95a | ||
![]() |
a143306519 | ||
![]() |
86b81abfd8 | ||
![]() |
8d065742d0 | ||
![]() |
eaa3d4a299 | ||
![]() |
a92fa7fffe | ||
![]() |
90f2939761 | ||
![]() |
12046246ef | ||
![]() |
adf73e59c4 | ||
![]() |
f2fcbad1c4 | ||
![]() |
0f47310814 | ||
![]() |
30530fde01 |
@@ -108,6 +108,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
|
||||
| Bounty Hunter (Rogue) | `rogueBH` |
|
||||
| Bounty Hunter (Hunter) | `hunterBH` |
|
||||
| Last Man Standing | `lastManStanding` |
|
||||
| Soul Wars Zeal | `soulWarsZeal` |
|
||||
|
||||
### Leagues
|
||||
|
||||
@@ -146,6 +147,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` |
|
||||
@@ -185,6 +187,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
|
||||
leaguePoints: {},
|
||||
bountyHunter: {},
|
||||
lastManStanding: {},
|
||||
soulWarsZeal: {},
|
||||
bosses: {}
|
||||
}
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ test('Parse CSV to json', () => {
|
||||
392,250
|
||||
1,6143
|
||||
4814,898
|
||||
37,225
|
||||
382,2780
|
||||
944,3000
|
||||
1981,1452
|
||||
@@ -70,6 +71,7 @@ test('Parse CSV to json', () => {
|
||||
625,2391
|
||||
120,2981
|
||||
1,109
|
||||
3,22666
|
||||
26,323
|
||||
201,1101
|
||||
82,3404
|
||||
@@ -120,6 +122,7 @@ test('Parse CSV to json', () => {
|
||||
hunter: { rank: -1, score: -1 },
|
||||
},
|
||||
lastManStanding: { rank: 4814, score: 898 },
|
||||
soulWarsZeal: { rank: 37, score: 225 },
|
||||
clues: {
|
||||
all: { rank: 32, score: 12148 },
|
||||
beginner: { rank: 3105, score: 76 },
|
||||
@@ -157,6 +160,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"
|
||||
}
|
||||
|
15
package.json
15
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "osrs-json-hiscores",
|
||||
"version": "2.0.2",
|
||||
"version": "2.3.1",
|
||||
"description": "The Oldschool Runescape API wrapper that does more!",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
@@ -36,16 +36,17 @@
|
||||
},
|
||||
"homepage": "https://github.com/maxswa/osrs-json-hiscores#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"cheerio": "^1.0.0-rc.3"
|
||||
"axios": "^0.21.1",
|
||||
"jsdom": "^16.3.0",
|
||||
"useragent-generator": "^1.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cheerio": "^0.22.11",
|
||||
"@types/jest": "^24.0.14",
|
||||
"jest": "^24.8.0",
|
||||
"@types/jest": "^26.0.20",
|
||||
"@types/jsdom": "^16.2.3",
|
||||
"jest": "^26.6.3",
|
||||
"np": "^5.0.3",
|
||||
"prettier": "^1.19.1",
|
||||
"ts-jest": "^24.0.2",
|
||||
"ts-jest": "^26.4.4",
|
||||
"tslint": "^5.17.0",
|
||||
"tslint-config-airbnb": "^5.11.1",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
|
51
src/@types/useragent-generator.d.ts
vendored
Normal file
51
src/@types/useragent-generator.d.ts
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
declare module 'useragent-generator' {
|
||||
/********************
|
||||
* Google Chrome *
|
||||
/*******************/
|
||||
export function chrome(opt: number | string | { version: string, os?: string }): string;
|
||||
export namespace chrome {
|
||||
function androidPhone(opt: number | string | { version: string, androidVersion?: string, device?: string }): string;
|
||||
function androidTablet(opt: number | string | { version: string, androidVersion?: string, device?: string })
|
||||
: string;
|
||||
function androidWebview(opt: number | string | { androidVersion: string, chromeVersion?: string, device?: string })
|
||||
: string;
|
||||
function chromecast(opt: number | string | { version: string }): string;
|
||||
function iOS(opt: number | string | { iOSVersion: string, chromeVersion?: string, device?: string }): string;
|
||||
}
|
||||
export function chromium(opt: number | string | { version: string, os?: string }): string;
|
||||
/***************
|
||||
* Firefox *
|
||||
/*************/
|
||||
export function firefox(opt: number | string | { version: string, os?: string }): string;
|
||||
export namespace firefox {
|
||||
function androidPhone(opt: number | string | { version: string, androidVersion?: string, device?: string }): string;
|
||||
function androidTablet(opt: number | string | { version: string, androidVersion?: string, device?: string })
|
||||
: string;
|
||||
function iOS(opt: number | string | { iOSVersion: string, device?: string }): string;
|
||||
}
|
||||
/**************
|
||||
* Safari *
|
||||
/************/
|
||||
export function safari(opt: number | string | { version: string, os?: string }): string;
|
||||
export namespace safari {
|
||||
function iOS(opt: number | string | { iOSVersion: string, safariVersion?: string, device?: string }): string;
|
||||
function iOSWebview(opt: number | string | { iOSVersion: string, safariVersion?: string, device?: string }): string;
|
||||
}
|
||||
/***********************
|
||||
* Internet Explorer *
|
||||
/*********************/
|
||||
export function ie(opt: number | string | { version: string, os?: string }): string;
|
||||
export namespace ie {
|
||||
function windowsPhone(opt: number | string | { version: string, device?: string }): string;
|
||||
}
|
||||
/**********************
|
||||
* Microsoft Edge *
|
||||
/********************/
|
||||
export function edge(opt: number | string | { version: string, chromeVersion?: string, os?: string }): string;
|
||||
/************************
|
||||
* Search Engine Bots *
|
||||
/**********************/
|
||||
export function googleBot(opt?: number | string | { version?: string }): string;
|
||||
export function bingBot(opt?: number | string | { version?: string }): string;
|
||||
export function yahooBot(): string;
|
||||
}
|
@@ -1,5 +1,3 @@
|
||||
import axios from 'axios';
|
||||
import * as cheerio from 'cheerio';
|
||||
import {
|
||||
Player,
|
||||
Activity,
|
||||
@@ -14,7 +12,6 @@ import {
|
||||
ActivityName,
|
||||
PlayerActivityRow,
|
||||
Bosses,
|
||||
Boss,
|
||||
} from './types';
|
||||
import {
|
||||
getStatsURL,
|
||||
@@ -28,8 +25,10 @@ import {
|
||||
numberFromElement,
|
||||
rsnFromElement,
|
||||
getActivityPageURL,
|
||||
httpGet,
|
||||
BOSSES,
|
||||
} from './utils';
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
export async function getStats(rsn: string): Promise<Player> {
|
||||
if (typeof rsn !== 'string') {
|
||||
@@ -40,19 +39,19 @@ export async function getStats(rsn: string): Promise<Player> {
|
||||
throw Error('RSN must be between 1 and 12 characters');
|
||||
}
|
||||
|
||||
const mainRes = await axios(getStatsURL('main', rsn));
|
||||
const mainRes = await httpGet(getStatsURL('main', rsn));
|
||||
if (mainRes.status === 200) {
|
||||
const otherResponses = await Promise.all([
|
||||
axios(getStatsURL('ironman', rsn)).catch(err => err),
|
||||
axios(getStatsURL('hardcore', rsn)).catch(err => err),
|
||||
axios(getStatsURL('ultimate', rsn)).catch(err => err),
|
||||
getRSNFormat(rsn),
|
||||
httpGet(getStatsURL('ironman', rsn)).catch(err => err),
|
||||
httpGet(getStatsURL('hardcore', rsn)).catch(err => err),
|
||||
httpGet(getStatsURL('ultimate', rsn)).catch(err => err),
|
||||
getRSNFormat(rsn).catch(() => undefined),
|
||||
]);
|
||||
|
||||
const [ironRes, hcRes, ultRes, formattedName] = otherResponses;
|
||||
|
||||
const player: Player = {
|
||||
name: formattedName,
|
||||
name: formattedName || rsn,
|
||||
mode: 'main',
|
||||
dead: false,
|
||||
deulted: false,
|
||||
@@ -121,7 +120,7 @@ export async function getStatsByGamemode(
|
||||
} else if (!GAMEMODES.includes(mode)) {
|
||||
throw Error('Invalid game mode');
|
||||
}
|
||||
const response = await axios(getStatsURL(mode, rsn));
|
||||
const response = await httpGet(getStatsURL(mode, rsn));
|
||||
if (response.status !== 200) {
|
||||
throw Error('Player not found');
|
||||
}
|
||||
@@ -144,23 +143,27 @@ 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 response = await httpGet(url);
|
||||
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;
|
||||
@@ -180,22 +183,25 @@ 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 response = await httpGet(url);
|
||||
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;
|
||||
@@ -212,11 +218,14 @@ 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 response = await httpGet(url);
|
||||
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 {
|
||||
@@ -256,7 +265,7 @@ export function parseStats(csv: string): Stats {
|
||||
const [leaguePoints] = activityObjects.splice(0, 1);
|
||||
const bhObjects = activityObjects.splice(0, BH_MODES.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 skills: Skills = skillObjects.reduce<Skills>((prev, curr, index) => {
|
||||
@@ -288,6 +297,7 @@ export function parseStats(csv: string): Stats {
|
||||
leaguePoints,
|
||||
bountyHunter,
|
||||
lastManStanding,
|
||||
soulWarsZeal,
|
||||
clues,
|
||||
bosses,
|
||||
};
|
||||
|
@@ -89,6 +89,7 @@ export type Boss =
|
||||
| 'kreeArra'
|
||||
| 'krilTsutsaroth'
|
||||
| 'mimic'
|
||||
| 'nightmare'
|
||||
| 'obor'
|
||||
| 'sarachnis'
|
||||
| 'scorpia'
|
||||
@@ -113,6 +114,7 @@ export type ActivityName =
|
||||
| 'hunterBH'
|
||||
| 'rogueBH'
|
||||
| 'lastManStanding'
|
||||
| 'soulWarsZeal'
|
||||
| 'allClues'
|
||||
| 'beginnerClues'
|
||||
| 'easyClues'
|
||||
@@ -128,6 +130,7 @@ export interface Stats {
|
||||
leaguePoints: Activity;
|
||||
bountyHunter: BH;
|
||||
lastManStanding: Activity;
|
||||
soulWarsZeal: Activity;
|
||||
bosses: Bosses;
|
||||
}
|
||||
export type Modes = { [M in Gamemode]?: Stats };
|
||||
|
@@ -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',
|
||||
@@ -126,6 +127,7 @@ export const ACTIVITIES: ActivityName[] = [
|
||||
'eliteClues',
|
||||
'masterClues',
|
||||
'lastManStanding',
|
||||
'soulWarsZeal',
|
||||
...BOSSES,
|
||||
];
|
||||
|
||||
@@ -161,6 +163,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',
|
||||
@@ -234,4 +237,5 @@ export const FORMATTED_BH_NAMES: FormattedBHNames = {
|
||||
};
|
||||
|
||||
export const FORMATTED_LMS = 'Last Man Standing';
|
||||
export const FORMATTED_SOUL_WARS = 'Soul Wars Zeal';
|
||||
export const FORMATTED_LEAGUE_POINTS = 'League Points';
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import * as ua from 'useragent-generator';
|
||||
import { Gamemode, SkillName, ActivityName } from '../types';
|
||||
import {
|
||||
GAMEMODE_URL,
|
||||
@@ -35,13 +37,22 @@ 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, ' ') || '';
|
||||
};
|
||||
|
||||
export const httpGet = (url: string) => {
|
||||
return axios.get(url, {
|
||||
headers: {
|
||||
// without User-Agent header requests may be rejected by DDoS protection mechanism
|
||||
'User-Agent': ua.firefox(80)
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user