Skip to content

Instantly share code, notes, and snippets.

@willerce
Created March 13, 2014 10:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willerce/9526282 to your computer and use it in GitHub Desktop.
Save willerce/9526282 to your computer and use it in GitHub Desktop.
Noderce 数据导出脚本
//需要安装两个依赖包
// npm install mongoskin
// npm install moment
// 根目录建立一个 markdown 目录保存文章
var mongo = require('mongoskin');
var moment = require('moment');
var fs = require('fs');
//这里需要数据库连接串
var db = mongo.db("mongodb://", {native_parser: true});
db.bind('post');
db.bind('comment');
db.post.find({}).sort({created: -1}).toArray(function (err, posts) {
if (!err) {
db.comment.find({}).sort({created: -1}).toArray(function (err, comments) {
if (!err) {
var threadArr = [];
var postArr = [];
//保存文章markdown
//保存多说的
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
threadArr.push({
"thread_key": post._id.toString(),
"title": post.title,
//这里的地址记得修改
"url": "http://willerce.com/post/" + post.slug +"/"
});
var content = [
"title: " + post.title,
"permalink: " + post.slug,
"date: " + moment(post.created).format("YYYY-MM-DD HH:mm"),
"tags: " + (JSON.stringify(post.tags) || ""),
"",
"---",
"",
post.content
].join('\r\n');
fs.writeFile('./markdown/' + moment(post.created).format("YYYY-MM-DD") + " " + post.title + '.md', content, function (err) {
if (err) throw err;
console.log('makdown posts saved');
});
}
var getpostbyid = function (id) {
for (var k = 0; k < threadArr.length; k++) {
if (threadArr[k].thread_key === id) {
return threadArr[k];
}
}
};
//保存评论为多说评论数据
for (var j = 0; j < comments.length; j++) {
var comment = comments[j];
var _post = getpostbyid(comment.post_id);
if (comment.status != '0') {
postArr.push({
'title': _post.title,
'url': _post.url,
"post_key": comment._id.toString(),
"thread_key": comment.post_id,
"author_name": comment.author,
"author_email": comment.email,
"author_url": comment.url,
"created_at": moment(comment.created).format("YYYY-MM-DD HH:mm:ss"),
"message": comment.content,
"ip" : comment.ip,
"status": "approved"
});
}
}
fs.writeFile('./duoshuo.json', JSON.stringify({"threads": threadArr, "posts": postArr}), function (err) {
if (err) throw err;
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment