Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort output of world scraping #129

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-dolls-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"osrs-web-scraper": patch
---

Sort output of world scraping
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`world scraper utils getWorldLines 1`] = `
"{{WorldLine|101|United States|mems=deadman|111-126 Deadman}}
"{{WorldLine|401|United States|mems=deadman|111-126 Deadman}}
"
`;
6 changes: 6 additions & 0 deletions src/scrapers/worlds/worlds.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type World = {
activity: string;
number: number;
region: string;
type: "deadman" | "yes" | "no";
};
67 changes: 47 additions & 20 deletions src/scrapers/worlds/worlds.utils.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
import { MediaWikiTemplate } from "@osrs-wiki/mediawiki-builder";
import { HTMLElement } from "node-html-parser";

import { World } from "./worlds.types";

export const WORLD_LIST_URL = "https://oldschool.runescape.com/a=13/slu";

/**
* Convert html td rows to MediaWikiTemplate's
* @param worldRows An array of world td rows
* @returns An array of MediaWikiTemplate's
*/
export const getWorldLines = (worldRows: HTMLElement) => {
const worldRowNodes = worldRows.childNodes.filter(
(node) => node instanceof HTMLElement && node.tagName === "TR"
);
const worldLines = worldRowNodes.map((node) => {
const tdNodes = node.childNodes.filter(
(node) => node instanceof HTMLElement && node.tagName === "TD"
);
const worldLine = new MediaWikiTemplate("WorldLine", { collapsed: true });
const worldNumber =
tdNodes[0].childNodes?.[1].textContent?.replaceAll(/^\D+/g, "") ?? "";
const region = tdNodes[2].textContent;
const activity = tdNodes[4].textContent;
const members = activity.includes("Deadman")
? "deadman"
: tdNodes[3].textContent === "Members"
? "yes"
: "no";
worldLine.add("", worldNumber);
worldLine.add("", region);
worldLine.add("mems", members);
worldLine.add("", activity);
return worldLine;
});
const worldLines = worldRowNodes
.map<World>((node) => {
const tdNodes = node.childNodes.filter(
(node) => node instanceof HTMLElement && node.tagName === "TD"
);
const worldNumber =
parseInt(
tdNodes[0].childNodes?.[1].textContent?.replaceAll(/^\D+/g, "") ?? "1"
) + 300;
const region = tdNodes[2].textContent;
const activity = tdNodes[4].textContent;
const members = activity.includes("Deadman")
? "deadman"
: tdNodes[3].textContent === "Members"
? "yes"
: "no";

return {
activity,
number: worldNumber,
region,
type: members,
};
})
.sort((a, b) => a.activity.localeCompare(b.activity))
.map(getWorldTemplate);
return worldLines;
};

/**
* Convert a World to a MediaWikiTemplate
* @param world The World
* @returns MediaWikiTemplate
*/
export const getWorldTemplate = (world: World) => {
const worldLine = new MediaWikiTemplate("WorldLine", { collapsed: true });
worldLine.add("", world.number.toString());
worldLine.add("", world.region);
worldLine.add("mems", world.type);
worldLine.add("", world.activity);
return worldLine;
};
Loading