add-edb-ids: Also handle quests from Category:Missing quest EDB ID
This commit is contained in:
parent
3aa9a44cae
commit
8e89e05756
2 changed files with 25 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import {findItemEDBID} from '../lib/api/lodestone.js';
|
import {findEDBEntryID} from '../lib/api/lodestone.js';
|
||||||
import {getMediawikiClient} from '../lib/config.js';
|
import {getMediawikiClient} from '../lib/config.js';
|
||||||
import {diff} from '../lib/util/diff.js';
|
import {diff} from '../lib/util/diff.js';
|
||||||
import {
|
import {
|
||||||
|
|
@ -34,22 +34,29 @@ function insertInfoboxEDBID (pageContent, edbID) {
|
||||||
const mw = await getMediawikiClient();
|
const mw = await getMediawikiClient();
|
||||||
|
|
||||||
// Get pages in the "Missing EDB ID" category from the main article namespace
|
// Get pages in the "Missing EDB ID" category from the main article namespace
|
||||||
const itemPagesWithoutEDBIDs = await mw.listCategoryPages('Category:Missing EDB ID', [0], +process.env.LIMIT || 500);
|
const [itemPagesWithoutEDBIDs, questPagesWithoutEDBIDs] = await Promise.all([
|
||||||
|
mw.listCategoryPages('Category:Missing EDB ID', [0], +process.env.LIMIT || 500),
|
||||||
|
mw.listCategoryPages('Category:Missing quest EDB ID', [0], +process.env.LIMIT || 500),
|
||||||
|
]);
|
||||||
console.log('Processing', itemPagesWithoutEDBIDs.length, 'item pages from [[Category:Missing EDB ID]]\n');
|
console.log('Processing', itemPagesWithoutEDBIDs.length, 'item pages from [[Category:Missing EDB ID]]\n');
|
||||||
|
console.log('Processing', questPagesWithoutEDBIDs.length, 'item pages from [[Category:Missing quest EDB ID]]\n');
|
||||||
|
|
||||||
for (const {title} of itemPagesWithoutEDBIDs) {
|
for (const {title, type} of [
|
||||||
|
...itemPagesWithoutEDBIDs.map(({title}) => ({title, type: 'item'})),
|
||||||
|
...questPagesWithoutEDBIDs.map(({title}) => ({title, type: 'quest'})),
|
||||||
|
]) {
|
||||||
// this runs serially with an artificial delay between requests to decrease
|
// this runs serially with an artificial delay between requests to decrease
|
||||||
// the chance of sqenix sending ninjas to my house
|
// the chance of sqenix sending ninjas to my house
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
console.log('Page:', title);
|
console.log('Page:', title);
|
||||||
// look up on EDB
|
// look up on EDB
|
||||||
const edbID = await findItemEDBID(title);
|
const edbID = await findEDBEntryID(type, title);
|
||||||
if (!edbID) {
|
if (!edbID) {
|
||||||
console.log('No EDB ID found for this item, skipping');
|
console.log(`No EDB ID found for this ${type}, skipping`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
console.log('EDB ID:', edbID, `(https://na.finalfantasyxiv.com/lodestone/playguide/db/item/${encodeURIComponent(edbID)})`);
|
console.log('EDB ID:', edbID, `(https://na.finalfantasyxiv.com/lodestone/playguide/db/${encodeURIComponent(type)}/${encodeURIComponent(edbID)})`);
|
||||||
|
|
||||||
let originalText;
|
let originalText;
|
||||||
try {
|
try {
|
||||||
|
|
@ -75,7 +82,7 @@ for (const {title} of itemPagesWithoutEDBIDs) {
|
||||||
|
|
||||||
// write the new stuff back to the wiki
|
// write the new stuff back to the wiki
|
||||||
try {
|
try {
|
||||||
await mw.editPage(title, updatedText, "Add EDB item ID", true);
|
await mw.editPage(title, updatedText, `Add EDB ${type} ID`, true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
console.error('writes should not fail, this seems bad, dying now');
|
console.error('writes should not fail, this seems bad, dying now');
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,28 @@
|
||||||
import {regExpEscape} from '../util/regexp.js';
|
import {regExpEscape} from '../util/regexp.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a regular expression that matches a link to the named item and
|
* Creates a regular expression that matches a link to the database entry of the
|
||||||
* captures its EDB ID from the matched link's `href` attribute.
|
* given type and name, and captures its EDB ID from the matched link's `href`
|
||||||
|
* attribute.
|
||||||
|
* @param {'item' | 'quest'} type
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {RegExp}
|
* @returns {RegExp}
|
||||||
*/
|
*/
|
||||||
const itemLinkRegExp = name => new RegExp(`<a href="/lodestone/playguide/db/item/(?<id>[a-z0-9]+)[^"]+"[^>]*>(?<name>${regExpEscape(name)})</a>`, 'i');
|
const edbEntryLinkRegExp = (type, name) => new RegExp(`<a href="/lodestone/playguide/db/${regExpEscape(type)}/(?<id>[a-z0-9]+)[^"]+"[^>]*>(?<name>${regExpEscape(name)})</a>`, 'i');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the ID of the named item in Eorzea Database.
|
* Gets the ID of the Eorzea Database entry with the given type and name.
|
||||||
|
* @param {'item' | 'quest'} type
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {Promise<string | null>}
|
* @returns {Promise<string | null>}
|
||||||
*/
|
*/
|
||||||
export async function findItemEDBID (name) {
|
export async function findEDBEntryID (type, name) {
|
||||||
// execute a search for the item's name
|
// execute a search for the entry's name and type
|
||||||
const searchURL = `https://na.finalfantasyxiv.com/lodestone/playguide/db/item/?q=${encodeURIComponent(name.replace(/\([^)]+\)|[&-]/g, ' '))}`;
|
const searchURL = `https://na.finalfantasyxiv.com/lodestone/playguide/db/${type}/?q=${encodeURIComponent(name.replace(/\([^)]+\)|[&-]/g, ' '))}`;
|
||||||
const response = await fetch(searchURL);
|
const response = await fetch(searchURL);
|
||||||
const body = await response.text();
|
const body = await response.text();
|
||||||
// find an `<a>` in the HTML response whose text exactly matches the name
|
// find an `<a>` in the HTML response whose text exactly matches the name
|
||||||
const match = body.match(itemLinkRegExp(name));
|
const match = body.match(edbEntryLinkRegExp(type, name));
|
||||||
// return the ID parsed from the URL in the `href` attribute
|
// return the ID parsed from the URL in the `href` attribute
|
||||||
return match?.groups.id;
|
return match?.groups.id;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue