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
89
bin/add-gt-ids
Executable file
89
bin/add-gt-ids
Executable file
|
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import {findItemGTID} from '../lib/api/xivapi.js';
|
||||
import {getMediawikiClient} from '../lib/config.js';
|
||||
import {diff} from '../lib/util/diff.js';
|
||||
import {
|
||||
addParameterBesideExistingParameter,
|
||||
setEmptyParameter,
|
||||
} from '../lib/util/template-parameters.js';
|
||||
|
||||
/**
|
||||
* Inserts the `id-gt` item infobox parameter into the given page contents.
|
||||
* @param {string} pageContent
|
||||
* @param {string} gtID
|
||||
* @returns {string}
|
||||
*/
|
||||
function insertInfoboxGTBID (pageContent, gtID) {
|
||||
if (pageContent.includes(`id-gt = ${gtID}`)) {
|
||||
console.log('what????', pageContent);
|
||||
throw new Error('Page seems to already contain its own GT ID??');
|
||||
}
|
||||
|
||||
let result = setEmptyParameter(pageContent, 'id-gt', gtID)
|
||||
?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'id-edb')
|
||||
?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'release')
|
||||
?? addParameterBesideExistingParameter(pageContent, 'id-gt', gtID, 'after', 'patch');
|
||||
|
||||
if (result == null) {
|
||||
throw new Error('Dunno how to insert the parameter into this page');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const mw = await getMediawikiClient();
|
||||
|
||||
// Get pages in the "Missing internal ID" category from the main article namespace
|
||||
const itemPagesWithoutGTIDs = await mw.listCategoryPages('Category:Missing internal ID', [0], +process.env.LIMIT || 500);
|
||||
console.log('Processing', itemPagesWithoutGTIDs.length, 'item pages from [[Category:Missing internal ID]]\n');
|
||||
|
||||
for (const {title} of itemPagesWithoutGTIDs) {
|
||||
console.log('Page:', title);
|
||||
// look up on XIVAPI
|
||||
let gtID;
|
||||
try {
|
||||
gtID = await findItemGTID(title);
|
||||
} catch (error) {
|
||||
console.log('Error looking up item ID:', error);
|
||||
}
|
||||
if (!gtID) {
|
||||
console.log('No ID found for this item, skipping');
|
||||
continue;
|
||||
}
|
||||
console.log('GT ID:', gtID, `(https://garlandtools.org/db/#item/${encodeURIComponent(gtID)})`);
|
||||
|
||||
let originalText;
|
||||
try {
|
||||
originalText = await mw.readPage(title);
|
||||
} catch (error) {
|
||||
console.log('Error reading page:', error);
|
||||
continue;
|
||||
}
|
||||
|
||||
// rewrite wiki page to include id-edb infobox parameter
|
||||
let updatedText;
|
||||
try {
|
||||
updatedText = insertInfoboxGTBID(originalText, gtID);
|
||||
diff(originalText, updatedText);
|
||||
} catch (error) {
|
||||
console.log('Error inserting parameter:', error);
|
||||
console.group('Bad page content:');
|
||||
console.log(originalText);
|
||||
console.groupEnd();
|
||||
console.log();
|
||||
continue;
|
||||
}
|
||||
|
||||
// write the new stuff back to the wiki
|
||||
try {
|
||||
await mw.editPage(title, updatedText, "Add GT item ID", true);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error('writes should not fail, this seems bad, dying now');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('Written.');
|
||||
console.log();
|
||||
}
|
||||
|
||||
console.log('done!');
|
||||
Loading…
Add table
Add a link
Reference in a new issue