Add script for populating id-gt from XIVAPI

This commit is contained in:
ewin 2025-07-29 22:02:55 -04:00
parent a687833764
commit 3aa9a44cae
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc
3 changed files with 120 additions and 3 deletions

25
lib/api/xivapi.js Normal file
View file

@ -0,0 +1,25 @@
// Escapes any quotation marks in the given string with backslashes.
const backslashEscapeQuotes = (str) => str
.replace(/\\/g, '\\\\') // replace all \ with \\
.replace(/"/g, '\\"'); // replace all " with \"
/**
* Gets the internal ID of the named item from XIVAPI.
* @param {string} name
* @returns {Promise<number | null>}
*/
export async function findItemGTID (name) {
const query = `Name~"${backslashEscapeQuotes(name)}"`;
const apiURL = `https://v2.xivapi.com/api/search?${new URLSearchParams({
sheets: 'Item',
query,
fields: 'Name',
})}`;
const response = await fetch(apiURL);
const data = await response.json();
if (!response.ok) {
throw new Error(`XIVAPI request failed: ${data.code} ${data.message}`);
}
const item = data.results.find(item => item.fields.Name.toLowerCase() === name.toLowerCase());
return item?.row_id ?? null;
}