mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
Add getSkillPage
This commit is contained in:
@@ -11,6 +11,8 @@ import {
|
||||
Clues,
|
||||
Gamemode,
|
||||
Category,
|
||||
SkillName,
|
||||
PlayerSkillRow,
|
||||
} from './types';
|
||||
import {
|
||||
getStatsURL,
|
||||
@@ -19,9 +21,11 @@ import {
|
||||
CLUES,
|
||||
MODES,
|
||||
getPlayerTableURL,
|
||||
getHiscoresPageURL,
|
||||
getSkillPageURL,
|
||||
GAMEMODES,
|
||||
OTHER,
|
||||
numberFromElement,
|
||||
rsnFromElement,
|
||||
} from './utils';
|
||||
|
||||
export async function getStats(
|
||||
@@ -111,59 +115,42 @@ export async function getStats(
|
||||
}
|
||||
}
|
||||
|
||||
// export async function getHiscores(
|
||||
// mode: Gamemode,
|
||||
// category: Category,
|
||||
// page: number
|
||||
// ) {
|
||||
// if (GAMEMODES.includes(mode) || mode.toLowerCase() === 'full') {
|
||||
// throw Error('Invalid game mode');
|
||||
// } else if (!Number.isInteger(page) || page < 1) {
|
||||
// throw Error('Page must be an integer greater than 0');
|
||||
// } else if ([...SKILLS, ...OTHER].includes(category)) {
|
||||
// throw Error('Invalid category');
|
||||
// }
|
||||
// const url = getHiscoresPageURL(mode, category, page);
|
||||
export const getSkillPage = async (
|
||||
mode: Gamemode,
|
||||
skill: SkillName,
|
||||
page: number
|
||||
): Promise<PlayerSkillRow[]> => {
|
||||
if (!GAMEMODES.includes(mode)) {
|
||||
throw Error('Invalid game mode');
|
||||
} else if (!Number.isInteger(page) || page < 1) {
|
||||
throw Error('Page must be an integer greater than 0');
|
||||
} else if (!SKILLS.includes(skill)) {
|
||||
throw Error('Invalid skill');
|
||||
}
|
||||
const url = getSkillPageURL(mode, skill, page);
|
||||
|
||||
// const players = [];
|
||||
// const response = await axios(url);
|
||||
// const $ = cheerio.load(response.data);
|
||||
// const playersHTML = $('.personal-hiscores__row').toArray();
|
||||
const response = await axios(url);
|
||||
const $ = cheerio.load(response.data);
|
||||
const playersHTML = $('.personal-hiscores__row').toArray();
|
||||
|
||||
// for (const player of playersHTML) {
|
||||
// const attributes = player.children.filter(node => node.name === 'td');
|
||||
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.filter(el => el.name === 'a');
|
||||
|
||||
// let playerInfo = {
|
||||
// mode,
|
||||
// category,
|
||||
// rank: (attributes[0].children[0].data || '').slice(1, -1),
|
||||
// rsn: (attributes[1].children[1].children[0].data || '').replace(
|
||||
// /\uFFFD/g,
|
||||
// ' '
|
||||
// ),
|
||||
// };
|
||||
return {
|
||||
rsn: rsnFromElement(nameEl),
|
||||
rank: numberFromElement(rankEl),
|
||||
level: numberFromElement(levelEl),
|
||||
xp: numberFromElement(xpEl),
|
||||
dead: nameCell.children.length === 4,
|
||||
};
|
||||
});
|
||||
|
||||
// SKILLS.includes(category)
|
||||
// ? (playerInfo = Object.assign(
|
||||
// {
|
||||
// level: attributes[2].children[0].data.slice(1, -1),
|
||||
// xp: attributes[3].children[0].data.slice(1, -1),
|
||||
// },
|
||||
// playerInfo
|
||||
// ))
|
||||
// : (playerInfo.score = attributes[2].children[0].data.slice(1, -1));
|
||||
return players;
|
||||
};
|
||||
|
||||
// if (mode === 'hc') {
|
||||
// playerInfo.dead = attributes[1].children.length > 1;
|
||||
// }
|
||||
|
||||
// players.push(playerInfo);
|
||||
// }
|
||||
|
||||
// return players;
|
||||
// }
|
||||
|
||||
export const getRSNFormat = async (rsn: string) => {
|
||||
export const getRSNFormat = async (rsn: string): Promise<string> => {
|
||||
const url = getPlayerTableURL('main', rsn);
|
||||
|
||||
try {
|
||||
|
||||
10
src/types.ts
10
src/types.ts
@@ -85,3 +85,13 @@ export interface Player extends Modes {
|
||||
deulted: boolean;
|
||||
deironed: boolean;
|
||||
}
|
||||
|
||||
export interface PlayerSkillRow extends Skill {
|
||||
rsn: string;
|
||||
dead: boolean;
|
||||
}
|
||||
|
||||
export interface PlayerActivityRow extends Activity {
|
||||
rsn: string;
|
||||
dead: boolean;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Gamemode, Category } from '../types';
|
||||
import { Gamemode, Category, SkillName } from '../types';
|
||||
import {
|
||||
BASE_URL,
|
||||
GAMEMODE_URL,
|
||||
@@ -16,15 +16,22 @@ export const getPlayerTableURL = (gamemode: Gamemode, rsn: string) =>
|
||||
GAMEMODE_URL[gamemode]
|
||||
}${SCORES_URL}table=0&user=${encodeURIComponent(rsn)}`;
|
||||
|
||||
export const getHiscoresPageURL = (
|
||||
export const getSkillPageURL = (
|
||||
gamemode: Gamemode,
|
||||
category: Category,
|
||||
skill: SkillName,
|
||||
page: number
|
||||
) => {
|
||||
const table = [...SKILLS, ...OTHER];
|
||||
return `${BASE_URL}${GAMEMODE_URL[gamemode]}${SCORES_URL}${
|
||||
table.includes(category)
|
||||
? `table=${table.indexOf(category)}`
|
||||
: `category_type=1&table=${OTHER.indexOf(category)}`
|
||||
}&page=${page}`;
|
||||
) =>
|
||||
`${BASE_URL}${GAMEMODE_URL[gamemode]}${SCORES_URL}table=${SKILLS.indexOf(
|
||||
skill
|
||||
)}&page=${page}`;
|
||||
|
||||
export const numberFromElement = (el: CheerioElement) => {
|
||||
const innerText = el.firstChild.data;
|
||||
const number = innerText ? innerText.replace(/[\n|,]/g, '') : '-1';
|
||||
return parseInt(number, 10);
|
||||
};
|
||||
|
||||
export const rsnFromElement = (el: CheerioElement) => {
|
||||
const innerText = el.firstChild.data;
|
||||
return innerText ? innerText.replace(/\uFFFD/g, ' ') : '';
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user