Last active
January 2, 2016 11:09
-
-
Save akhoury/8294882 to your computer and use it in GitHub Desktop.
get your nodebb users emails in a CSV file to import to mailchimp or something
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
// !!!!! IMPORTANT !!!!!! | |
// this is no longer needed since a helper users/csv generator was added by @psychobunny | |
// commit: https://github.com/designcreateplay/NodeBB/commit/cfa4256df5c2e7d9f0c1969a8098ef8472d2fa93 | |
// in order for this to work | |
// - this JS file needs to be located in: [NodeBB_PATH]/node_modules/_nodebb_get_users_emails | |
// - you must have a working NodeBB installation, with config.json and a running database | |
// USE IT THEN DELETE IT | |
// usage: | |
// cd [NodeBB_PATH]/node_modules/_nodebb_get_users_emails | |
// npm install fs-extra | |
// npm install async | |
// node nodebb.emails.js | |
// then find the data in emails.csv | |
var fs = require('fs-extra'), | |
async = require('async'), | |
path = require('path'), | |
emailsFile = path.join(__dirname, './emails.csv'), | |
User, db, nconf; | |
// todo: this is such a bummer !!! | |
// in order to require almost any NodeBB Object, nconf.get('database') needs to be set | |
// so let's require nconf first | |
nconf = require('../nconf'); | |
var nconfFile = path.join(__dirname, '../../config.json'); | |
// see if the NodeBB config.json exists | |
if (fs.existsSync(nconfFile)) { | |
// config.json is there, load it and use these values | |
var nconfigs = fs.readJsonSync(nconfFile); | |
nconfigs.use_port = nconfigs.use_port ? 'y' : 'n'; | |
} else { | |
// config.json does not exists, i'm throwing an error | |
throw new Error('run: node app --setup # to create a config.json'); | |
} | |
//tell nconf to read it the config | |
// i know it's redundant, but im not sure if I can pass the values in memory, but what the hell, it's not a huge file anyways | |
nconf.file({file: nconfFile}); | |
// requiring DB after configs, since it could be either mongo or redis now | |
if (nconfigs.database === 'redis') { | |
nconf.set('database', 'redis'); | |
} else if (nconfigs.database === 'mongo') { | |
nconf.set('database', 'mongo'); | |
} else { | |
throw new Error('NodeBB Database config is not set'); | |
} | |
// activated or not, still works if it lives in NodeBB/node_modules/_nodebb_get_users_emails | |
try { | |
User = module.parent.require('./user.js'); | |
db = module.parent.require('./database.js'); | |
} catch (e) { | |
User = require('../../src/user.js'); | |
db = require('../../src/database.js'); | |
} | |
var generateUsersEmails = function(){ | |
console.log('getting users emails started'); | |
var t0 = +new Date(); | |
fs.removeSync(emailsFile); | |
fs.createFileSync(emailsFile); | |
var csvContent = 'email,username,uid\n'; | |
db.getObjectValues('username:uid', function(err, uids) { | |
async.each(uids, function(uid, next) { | |
console.log('reading user:' + uid); | |
User.getUserFields(uid, ['email', 'username'], function(err, userData) { | |
if(err) { | |
return next(err); | |
} | |
csvContent += userData.email+ ',' + userData.username + ',' + uid +'\n'; | |
next(); | |
}); | |
}, function(err) { | |
if (err) throw err; | |
console.log('reading done, now writing to ' + emailsFile); | |
fs.writeFile(emailsFile, csvContent, function(err){ | |
if (err) throw err; | |
console.log('getting users emails took: ' + ((+new Date() - t0) / 1000 / 60).toFixed(2) + ' minutes'); | |
process.exit(); | |
}); | |
}); | |
}); | |
}; | |
generateUsersEmails(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment