-
Notifications
You must be signed in to change notification settings - Fork 1
/
verifier.js
75 lines (64 loc) · 2.33 KB
/
verifier.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
(function () {
var async = require('async');
var request = require('request');
var BASE_URL = 'http://www.reddit.com/r/SUBREDDIT/about.json';
module.exports = function (config, externalcallback) {
console.log('Checking config.json');
// check valid document counts for each subreddit
for (var meta in config) {
for (var subreddit in config[meta]) {
if (!Number.isInteger(config[meta][subreddit])) {
console.error('Invalid count: %s in %s', config[meta][subreddit], meta);
process.exit(1);
}
}
}
// check valid subreddit titles
var metaArray = Object.keys(config);
// for each meta tag, if any of the subreddits belongong to a meta returns a true,
// the meta will return true and detect will call it out
async.detect(metaArray, function (meta, outercallback) {
var keys = Object.keys(config[meta]);
// detect if each of the subreddits are valid
async.detect(keys, function (key, callback) {
request(BASE_URL.replace('SUBREDDIT', key), function (err, resp, body) {
var respObject = JSON.parse(body);
// valid if kind == t5
if (respObject.kind === 't5') {
callback(false);
} else {
// otherwise invalid
callback(true);
}
});
}, function (result) {
if (result !== undefined) {
// if result is not undefined, one of the callbacks returned true. invalid subreddit.
console.error('Invalid subreddit: %s', result);
outercallback(true);
} else {
outercallback(false);
}
});
}, function (outerresult) {
// if one of the subreddits returned true, the meta will return true. exit.
if (outerresult !== undefined) {
process.exit(1);
} else {
// otherwise, we have a valid json.
console.log('Valid config.json');
// print some summary statistics
var totalDocCount = 0;
var totalSubCount = 0;
for (var meta in config) {
for (var subreddit in config[meta]) {
totalDocCount += config[meta][subreddit];
totalSubCount++;
}
}
console.log('Sraping %d threads from %d subreddits', totalDocCount, totalSubCount);
externalcallback();
}
});
}
})();