break things up, do some proper mediawiki API stuff to support editing

This commit is contained in:
ewin 2025-03-27 10:48:33 -04:00
parent 64eb4da894
commit e3af35a68e
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc
4 changed files with 260 additions and 101 deletions

32
lodestone.mjs Normal file
View file

@ -0,0 +1,32 @@
// Utilities for scraping data from the Lodestone
/**
* @see https://stackoverflow.com/a/6969486
* @param {string}
* @returns {string}
*/
const regExpEscape = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
/**
* Creates a regular expression that matches a link to the named item and
* captures its EDB ID from the matched link's `href` attribute.
* @param {string} name
* @returns {RegExp}
*/
const itemLinkRegExp = name => new RegExp(`<a href="/lodestone/playguide/db/item/(?<id>[a-z0-9]+)[^"]+"[^>]*>(?<name>${regExpEscape(name)})</a>`, 'i');
/**
* Gets the ID of the named item in Eorzea Database.
* @param {string} name
* @returns {Promise<string | undefined>}
*/
export async function findItemEDBID (name) {
// execute a search for the item's name
const searchURL = `https://na.finalfantasyxiv.com/lodestone/playguide/db/item/?q=${encodeURIComponent(name)}`;
const response = await fetch(searchURL);
const body = await response.text();
// find an `<a>` in the HTML response whose text exactly matches the name
const match = body.match(itemLinkRegExp(name));
// return the ID parsed from the URL in the `href` attribute
return match?.groups.id;
}