-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-export-posts.js
executable file
·122 lines (99 loc) · 2.67 KB
/
reddit-export-posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
var async = require('async');
var path = require('path');
var chalk = require('chalk');
var NeDB = require('nedb');
var lockFile = require('lockfile');
var JsonStore = require('jul11co-jsonstore');
var utils = require('jul11co-utils');
function printUsage() {
console.log('Usage: reddit-export-posts <data-dir> [OPTIONS]');
console.log('');
console.log('OPTIONS:');
console.log(' --verbose : verbose');
console.log('');
}
if (process.argv.indexOf('-h') >= 0
|| process.argv.indexOf('--help') >= 0
|| process.argv.length < 3) {
printUsage();
process.exit();
}
var options = {
verbose: false,
};
if (process.argv.indexOf('--verbose') >= 0) {
options.verbose = true;
}
var data_dir = process.argv[2] || './';
var posts_cache = null;
var posts_store = null;
var exported_posts_cache = null;
var exported_posts_count = 0;
function getPostInfo(post_id, callback) {
posts_store.findOne({id: post_id}, function(err, post) {
if (err) return callback(err);
callback(null, post);
});
}
function exportPosts(post_ids, options, callback) {
if (typeof options == 'function') {
callback = options;
options = {};
}
var count = 0;
var total = post_ids.length;
async.eachSeries(post_ids, function(post_id, cb) {
count++;
console.log('Progress:', count + '/' + total, post_id);
getPostInfo(post_id, function(err, post) {
if (err) {
console.log('Export failed:', post_id);
return cb(err);
}
if (post) {
exported_posts_count++;
exported_posts_cache.set(post_id, post);
}
cb();
})
}, function(err) {
callback(err);
});
}
function printStats() {
console.log('Exported posts count:', exported_posts_count);
}
lockFile.lock(path.join(data_dir, 'reddit.lock'), {}, function (err) {
if (err) {
console.log(err);
process.exit(1);
}
posts_cache = new JsonStore({ file: path.join(data_dir, 'posts.json') });
posts_store = new NeDB({
filename: path.join(data_dir, 'posts.db'),
autoload: true
});
exported_posts_cache = new JsonStore({ file: path.join(data_dir, 'posts-exported.json') });
var post_ids = [];
for (var post_id in posts_cache.toMap()) {
post_ids.push(post_id);
}
console.log('Posts count:', post_ids.length);
exportPosts(post_ids, options, function(err) {
var export_err = err;
lockFile.unlock(path.join(data_dir, 'reddit.lock'), function (err) {
if (err) {
console.log(err);
}
printStats();
if (export_err) {
console.log(export_err);
process.exit(1);
} else {
console.log('Done.');
process.exit();
}
});
});
});