-
-
Save jdlrobson/1a00ae06c6d88e48d7280e2639ba802e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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