Created
April 23, 2019 03:33
-
-
Save idooo/d94cec11300e1a202e7e827841978a62 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 this you need to | |
// 1) Download and enable Basic Auth plugin: https://github.com/WP-API/Basic-Auth | |
// 2) Install dependencies: "npm i csv-parse wpapi" | |
const fs = require('fs'); | |
const WPAPI = require('wpapi'); | |
const parse = require("csv-parse/lib/es5"); | |
const PASSWORD = process.env.PASSWORD || ""; | |
const wp = new WPAPI({ | |
endpoint: 'https://........./wp-json', | |
username: '<username>', | |
password: PASSWORD, | |
}); | |
function readInputFile(fileName) { | |
const file = fs.readFileSync(fileName).toString('utf8'); | |
const lines = []; | |
return new Promise((resolve) => { | |
parse(file, { | |
trim: true, | |
skip_empty_lines: true | |
}) | |
.on('readable', function () { | |
let record; | |
while (record = this.read()) { | |
lines.push(record) | |
} | |
}) | |
.on('end', function () { | |
resolve(lines) | |
}); | |
}) | |
} | |
async function deleteUser(id) { | |
return new Promise((resolve, reject) => { | |
wp.users().id(id).delete({reassign: false, force: true}).then(function( response ) { | |
resolve( response ); | |
}).catch((err) => { | |
resolve(err) | |
}); | |
}) | |
} | |
async function getUser(id) { | |
return new Promise((resolve, reject) => { | |
wp.users().id(id).then(function( response ) { | |
resolve( response ); | |
}).catch((err) => { | |
resolve(err) | |
}); | |
}) | |
} | |
async function main() { | |
const lines = await readInputFile('./wp_users.csv'); | |
const poolSize = 4; | |
const pools = splitUp(lines, poolSize); | |
for (let i = 0; i < poolSize; i++) { | |
setTimeout(() => startWorker(i + 1, pools[i])); | |
} | |
async function startWorker(id, pool) { | |
console.log(`[${id}] WorkerStarted #${id}`); | |
const total = pool.length + 1; | |
for (let i = 0; i < pool.length; i++) { | |
const line = pool[i]; | |
console.log(`[${id}] > removing user (${i + 1}/${total}) ${line[0]} -> "${line[1]}"`); | |
const user = await getUser(line[0]); | |
if (user.id) { | |
const del = await deleteUser(line[0]); | |
if (del.deleted) { | |
console.log(`[${id}] >> deleted`) | |
} else { | |
console.log(`[${id}] >>> ERROR deleting user: ${del.message}`) | |
} | |
} else { | |
console.log(`>${id} >>> ERROR getting user data: ${user.message}`) | |
} | |
} | |
} | |
} | |
function splitUp(arr, n) { | |
var rest = arr.length % n, // how much to divide | |
restUsed = rest, // to keep track of the division over the elements | |
partLength = Math.floor(arr.length / n), | |
result = []; | |
for(var i = 0; i < arr.length; i += partLength) { | |
var end = partLength + i, | |
add = false; | |
if(rest !== 0 && restUsed) { // should add one element for the division | |
end++; | |
restUsed--; // we've used one division element now | |
add = true; | |
} | |
result.push(arr.slice(i, end)); // part of the array | |
if(add) { | |
i++; // also increment i in the case we added an extra element for division | |
} | |
} | |
return result; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment