Created
March 16, 2015 17:14
-
-
Save barisusakli/7aa94f47103f24e7ad34 to your computer and use it in GitHub Desktop.
Get posts and topics of user
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
'use strict'; | |
/*globals require, console, process */ | |
var nconf = require('nconf'); | |
var async = require('async'); | |
var fs = require('fs'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
var db = require('./src/database'); | |
var uid = 7; | |
var counter = 0; | |
db.init(function(err) { | |
if (err) { | |
console.log("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message); | |
process.exit(); | |
} | |
async.series([ | |
function(next) { | |
exportPosts(next); | |
}, | |
function(next) { | |
exportTopics(next); | |
} | |
], function(err) { | |
if (err) { | |
console.log(err); | |
process.exit(); | |
return; | |
} | |
console.log('done ', counter); | |
}); | |
}); | |
function exportPosts(callback) { | |
db.getSortedSetRevRange('uid:' + uid + ':posts', 0, -1, function(err, pids) { | |
if (err) { | |
return callback(err); | |
} | |
async.mapLimit(pids, 50, function(pid, next) { | |
++counter; | |
console.log(counter); | |
db.getObjectFields('post:' + pid, ['content', 'timestamp'], next); | |
}, function(err, posts) { | |
if (err) { | |
return callback(err); | |
} | |
fs.writeFile('posts.json', JSON.stringify(posts, null, 4), callback); | |
}); | |
}); | |
} | |
function exportTopics(callback) { | |
db.getSortedSetRevRange('uid:' + uid + ':topics', 0, -1, function(err, tids) { | |
if (err) { | |
return callback(err); | |
} | |
async.mapLimit(tids, 50, function(tid, next) { | |
++counter; | |
console.log(counter); | |
db.getObjectFields('topic:'+ tid, ['title', 'mainPid', 'timestamp'], function(err, topic) { | |
if (err) { | |
return next(err); | |
} | |
db.getObjectField('post:' + topic.mainPid, 'content', function(err, content) { | |
if (err) { | |
return next(err); | |
} | |
topic.content = content; | |
delete topic.mainPid; | |
next(null, topic); | |
}); | |
}); | |
}, function(err, topics) { | |
if (err) { | |
return callback(err); | |
} | |
fs.writeFile('topics.json', JSON.stringify(topics, null, 4), callback); | |
}); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment