-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-api-scrape.js
executable file
·282 lines (239 loc) · 7.49 KB
/
reddit-api-scrape.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env node
var path = require('path');
var urlutil = require('url');
var async = require('async');
var chalk = require('chalk');
var lockFile = require('lockfile');
var JsonStore = require('jul11co-jsonstore');
var utils = require('jul11co-utils');
var RedditScraper = require('./lib/reddit-scraper');
function printUsage() {
console.log('Usage: ');
console.log(' reddit-api-scrape <reddit-url> [output-dir]');
console.log(' reddit-api-scrape --update [output-dir]');
console.log('');
console.log('OPTIONS:');
console.log(' --verbose : verbose');
console.log('');
console.log(' --stop-if-no-new-posts, -S : stop if current subreddit page has no new posts (default: not set)');
console.log('');
}
if (process.argv.length < 3) {
printUsage();
process.exit();
}
var options = {};
var argv = [];
for (var i = 2; i < process.argv.length; i++) {
if (process.argv[i] == '--stop-if-no-new-posts' || process.argv[i] == '-S') {
options.stop_if_no_new_posts = true;
}
else if (process.argv[i].indexOf('--') == 0) {
var arg = process.argv[i];
if (arg.indexOf("=") > 0) {
var arg_kv = arg.split('=');
arg = arg_kv[0];
arg = arg.replace('--','');
arg = utils.replaceAll(arg, '-', '_');
options[arg] = arg_kv[1];
} else {
arg = arg.replace('--','');
arg = utils.replaceAll(arg, '-', '_');
options[arg] = true;
}
} else {
argv.push(process.argv[i]);
}
}
if (argv.length < 1) {
printUsage();
process.exit();
}
var start_links = [];
for (var i = 0; i < argv.length; i++) {
if (argv[i].indexOf('http') == 0) {
var start_link = argv[i];
if (start_link.indexOf('http:') == 0) {
start_link = start_link.replace('http:', 'https:');
}
if (start_link.indexOf('//reddit.com/') != -1) {
start_link = start_link.replace('//reddit.com/', '//www.reddit.com/');
}
console.log('Start link:', start_link);
start_links.push(start_link);
} else if (argv[i].indexOf('r/') == 0) {
start_links.push('https://www.reddit.com/' + argv[i]);
} else if (argv[i].indexOf('/r/') == 0) {
start_links.push('https://www.reddit.com' + argv[i]);
}
}
var output_dir = argv[1] || './';
if (options.update) {
output_dir = argv[0];
if (!utils.fileExists(path.join(output_dir, 'reddit.json'))) {
console.log('Invalid output directory, reddit.json not found!');
process.exit();
}
} else {
if (start_links.length == 1) {
var subreddit = start_links[0].replace('https://www.reddit.com/r/','').split('/')[0].trim();
output_dir = path.join(output_dir, 'r', subreddit);
if (start_links[0].indexOf(subreddit + '/new') != -1) {
console.log('New posts from ' + subreddit);
options.new = true;
} else if (start_links[0].indexOf(subreddit + '/hot') != -1) {
console.log('Hot posts from ' + subreddit);
options.hot = true;
} else if (start_links[0].indexOf(subreddit + '/rising') != -1) {
console.log('Rising posts from ' + subreddit);
options.rising = true;
} else if (start_links[0].indexOf(subreddit + '/controversial') != -1) {
console.log('Controversial posts from ' + subreddit);
options.controversial = true;
} else if (start_links[0].indexOf(subreddit + '/top') != -1) {
console.log('Top posts from ' + subreddit);
options.top = true;
} else if (start_links[0].indexOf(subreddit + '/gilded') != -1) {
console.log('Gilded posts from ' + subreddit);
options.gilded = true;
} else if (start_links[0].indexOf(subreddit + '/promoted') != -1) {
console.log('Promoted posts from ' + subreddit);
options.promoted = true;
}
}
}
var lastChar = function(str) {
return str.substring(str.length-1);
}
var removeLastChar = function(str) {
return str.substring(0, str.length-1);
}
//
function processReport(data) {
if (typeof process.send == 'function') {
process.send(data);
}
}
function scrapeLinks(links, output_dir, sources_store, options, callback) {
// console.log('scrapeLinks');
// console.log(links);
var reddit_scraper = new RedditScraper({output_dir: output_dir});
reddit_scraper.on('new-post', function(data) {
processReport({
new_post: true,
post_info: data
});
});
reddit_scraper.on('progress', function(data) {
processReport({
progress: true,
scraped_posts_count: data.scraped_posts_count,
new_posts_count: data.new_posts_count
});
});
function printStats() {
var stats = reddit_scraper.getStats();
console.log('New posts count:', stats.new_posts_count);
processReport({
scraped_stats: true,
scraped_posts_count: stats.scraped_posts_count,
new_posts_count: stats.new_posts_count
});
}
function gracefulShutdown() {
console.log('');
console.log('Exiting... Please wait');
printStats();
console.log('Done.');
process.exit();
}
process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT' , gracefulShutdown);
async.eachSeries(links, function(link, cb) {
console.log(link);
if (link.indexOf('https://www.reddit.com/r/') == 0) {
var subreddit = link.replace('https://www.reddit.com/r/','').split('/')[0].trim();
// console.log('processSubreddit:', subreddit);
reddit_scraper.processSubreddit(subreddit, options, function(err) {
if (!err) {
sources_store.update(link, {last_update: new Date()}, true);
}
cb(err);
});
} else {
cb();
}
}, function(err) {
printStats();
callback(err);
});
}
//
if (options.update) {
utils.ensureDirectoryExists(output_dir);
lockFile.lock(path.join(output_dir, 'reddit.lock'), {}, function (err) {
if (err) {
console.log(err);
process.exit(1);
}
var sources_store = new JsonStore({ file: path.join(output_dir, 'reddit.json') });
for (var source_link in sources_store.toMap()) {
if (start_links.indexOf(source_link) == -1) {
start_links.push(source_link);
}
}
scrapeLinks(start_links, output_dir, sources_store, options, function(err) {
var scrape_err = err;
lockFile.unlock(path.join(output_dir, 'reddit.lock'), function (err) {
if (err) {
console.log(err);
}
if (scrape_err) {
console.log(scrape_err);
process.exit(1);
} else {
console.log('Done.');
process.exit(0);
}
});
});
});
} else if (start_links.length) {
utils.ensureDirectoryExists(output_dir);
lockFile.lock(path.join(output_dir, 'reddit.lock'), {}, function (err) {
if (err) {
console.log(err);
process.exit(1);
}
var sources_store = new JsonStore({ file: path.join(output_dir, 'reddit.json') });
start_links = start_links.map(function(start_link) {
if (lastChar(start_link) == '/') {
start_link = removeLastChar(start_link);
}
if (!sources_store.get(start_link)) {
sources_store.set(start_link, {added_at: new Date()});
}
return start_link;
});
scrapeLinks(start_links, output_dir, sources_store, options, function(err) {
if (err) {
console.log(err);
}
lockFile.unlock(path.join(output_dir, 'reddit.lock'), function (err) {
if (err) {
console.log(err);
}
if (err) {
console.log(err);
process.exit(1);
} else {
console.log('Done.');
process.exit(0);
}
});
});
});
} else {
printUsage();
process.exit(0);
}