Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created December 18, 2023 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdlrobson/1a00ae06c6d88e48d7280e2639ba802e to your computer and use it in GitHub Desktop.
Save jdlrobson/1a00ae06c6d88e48d7280e2639ba802e to your computer and use it in GitHub Desktop.
// TO USE: Create a file mw-ui-button.csv in the same folder (where CSV export from global search)
const fetch = require( 'node-fetch');
const fs = require('fs');
const CsvReadableStream = require('csv-reader');
let inputStream = fs.createReadStream('mw-ui-button.csv', 'utf8');
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
const makeQuery = ( host, title ) => {
const url = `https://${host}.org/w/api.php?action=query&format=json&prop=linkshere&titles=${encodeURIComponent(title)}&formatversion=2&lhnamespace=0`
return fetch( url )
.then((r) => r.json())
.then((r) => {
let lh;
try {
lh = r.query.pages[0].linkshere;
} catch ( e ) {
lh = null;
}
if (lh) {
console.log(host, title, lh.length)
} else {
console.log(host,title,0)
}
})
}
let i = 0;
const queries = [];
const checkUsage = (a, b) => {
return makeQuery( a, b ).then(null, () => {
return checkUsage(a, b);
});
};
inputStream
.pipe(new CsvReadableStream({ parseNumbers: true, parseBooleans: true, trim: true }))
.on('data', function (row) {
if ( i > 0 ) {
queries.push( [ row[0], row[1] ] );
}
i++;
})
.on('end', function () {
let active = 0;
setInterval( () => {
if ( active < 3 ) {
const row = queries.pop();
active++;
checkUsage( row[0], row[1] ).then(() => {
active--;
} );
}
}, 100 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment