Add script for populating id-gt from XIVAPI
This commit is contained in:
parent
a687833764
commit
3aa9a44cae
3 changed files with 120 additions and 3 deletions
25
lib/api/xivapi.js
Normal file
25
lib/api/xivapi.js
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue