Add one-off script for adding hrothgar/viera headwear compatibility data

This commit is contained in:
ewin 2025-08-12 18:27:59 -04:00
parent 7b5357c050
commit 4d7bd2515a
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc
2 changed files with 84 additions and 0 deletions

69
bin/hrothgar-viera-headwear Executable file
View file

@ -0,0 +1,69 @@
#!/usr/bin/env node
import {readFileSync} from 'node:fs';
import {join} from 'node:path';
import {getMediawikiClient} from '../lib/config.js';
import {diff} from '../lib/util/diff.js';
import {addParameterAtBottom, setExistingParameter} from '../lib/util/template-parameters.js';
const mw = await getMediawikiClient();
// parse CSV
// hats.csv is an export of this google sheet that crye compiled:
// https://docs.google.com/spreadsheets/d/13s_qxWThsensslulLfAxN0-hzQZVCqS9fKaImlLXbVU/edit?gid=38787585#gid=38787585
const data = readFileSync(join(import.meta.dirname, '../hats.csv'), 'utf-8').split(/\r?\n/g).map(line => line && line.split(','));
console.log(data.slice(0, 1));
const headers = data.splice(0, 1)[0];
const COL_ITEM = headers.findIndex(field => field.toLowerCase() === 'item');
const COL_VIERA = headers.findIndex(field => field.toLowerCase().includes('viera'));
const COL_HROTH = headers.findIndex(field => field.toLowerCase().includes('hrothgar'));
console.log('Processing', data.length, 'items');
for (const line of data) {
const item = line[COL_ITEM];
const params = {
'display-on-viera': line[COL_VIERA].toLowerCase() === 'yes' ? 'y' : 'n',
'display-on-hrothgar': line[COL_HROTH].toLowerCase() === 'yes' ? 'y' : 'n',
}
console.log(item, params);
// read and apply updates
const pageContent = await mw.readPage(item);
let newPageContent = pageContent;
try {
for (const [paramName, paramValue] of Object.entries(params)) {
newPageContent = setExistingParameter(newPageContent, paramName, paramValue)
?? addParameterAtBottom(newPageContent, paramName, paramValue);
if (!newPageContent) {
console.log(pageContent);
throw new Error('adding parameter to the page failed');
}
}
} catch (error) {
console.error(error);
console.log('skipping this item');
continue;
}
if (newPageContent === pageContent) {
// no changes
console.log('No changes, skipping');
continue;
}
diff(pageContent, newPageContent);
// write
try {
await mw.editPage(item, newPageContent, 'Add Hrothgar/Viera headwear data');
} catch (error) {
console.error('Failed to write:', error);
process.exit(1);
}
console.log('Written.');
}
console.log('done!');