Compare commits

..

11 Commits

Author SHA1 Message Date
maxswa
0f68c1995c v2.3.0 2021-01-10 16:20:54 -05:00
Max Swartwout
7a3f3a0800 Merge pull request #17 from maxswa/dependabot/npm_and_yarn/ini-1.3.7
Bump ini from 1.3.5 to 1.3.7
2021-01-10 16:00:05 -05:00
Max Swartwout
b16e8ace6e Merge pull request #18 from maxswa/dependabot/npm_and_yarn/axios-0.21.1
Bump axios from 0.19.2 to 0.21.1
2021-01-10 15:59:55 -05:00
Max Swartwout
09f7805fd4 Merge pull request #20 from molo-pl/master
Fixes #19 adding support for Soul Wars Zeal
2021-01-10 15:59:43 -05:00
molo-pl
939f4d2721 Fixes #19 adding support for Soul Wars Zeal 2021-01-06 17:21:46 +01:00
dependabot[bot]
a4577ffb2c Bump axios from 0.19.2 to 0.21.1
Bumps [axios](https://github.com/axios/axios) from 0.19.2 to 0.21.1.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/v0.21.1/CHANGELOG.md)
- [Commits](https://github.com/axios/axios/compare/v0.19.2...v0.21.1)

Signed-off-by: dependabot[bot] <support@github.com>
2021-01-05 09:06:49 +00:00
dependabot[bot]
19ba7e2916 Bump ini from 1.3.5 to 1.3.7
Bumps [ini](https://github.com/isaacs/ini) from 1.3.5 to 1.3.7.
- [Release notes](https://github.com/isaacs/ini/releases)
- [Commits](https://github.com/isaacs/ini/compare/v1.3.5...v1.3.7)

Signed-off-by: dependabot[bot] <support@github.com>
2020-12-11 09:18:50 +00:00
maxswa
7de2d9a95a v2.2.0 2020-07-23 18:22:38 -04:00
Max Swartwout
a143306519 Merge pull request #15 from maxswa/release/2.2.0
Release/2.2.0
2020-07-23 18:20:49 -04:00
maxswa
86b81abfd8 Upgrade dependencies. 2020-07-23 18:18:58 -04:00
maxswa
8d065742d0 Replace cheerio with jsdom. 2020-07-23 18:13:51 -04:00
8 changed files with 586 additions and 766 deletions

View File

@@ -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
@@ -186,6 +187,7 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
leaguePoints: {},
bountyHunter: {},
lastManStanding: {},
soulWarsZeal: {},
bosses: {}
}
}

View File

@@ -43,6 +43,7 @@ test('Parse CSV to json', () => {
392,250
1,6143
4814,898
37,225
382,2780
944,3000
1981,1452
@@ -121,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 },

View File

@@ -1,6 +1,6 @@
{
"name": "osrs-json-hiscores",
"version": "2.1.0",
"version": "2.3.0",
"description": "The Oldschool Runescape API wrapper that does more!",
"main": "lib/index.js",
"types": "lib/index.d.ts",
@@ -36,12 +36,12 @@
},
"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"
},
"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",

View File

@@ -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') {
@@ -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 {
@@ -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,
};

View File

@@ -114,6 +114,7 @@ export type ActivityName =
| 'hunterBH'
| 'rogueBH'
| 'lastManStanding'
| 'soulWarsZeal'
| 'allClues'
| 'beginnerClues'
| 'easyClues'
@@ -129,6 +130,7 @@ export interface Stats {
leaguePoints: Activity;
bountyHunter: BH;
lastManStanding: Activity;
soulWarsZeal: Activity;
bosses: Bosses;
}
export type Modes = { [M in Gamemode]?: Stats };

View File

@@ -127,6 +127,7 @@ export const ACTIVITIES: ActivityName[] = [
'eliteClues',
'masterClues',
'lastManStanding',
'soulWarsZeal',
...BOSSES,
];
@@ -236,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';

View File

@@ -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, ' ') || '';
};

1264
yarn.lock

File diff suppressed because it is too large Load Diff