-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (46 loc) · 1.85 KB
/
index.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
"use strict";
var request = require("request");
var async = require("async");
var search_objects = {};
var _ = require("lodash");
var lib = require("./lib");
var format = require("util").format;
var keywords = process.argv.slice(2,process.argv.length);
var create_function = function(query) {
var options = {
"url" : "http://www.reddit.com/search.json",
"method": "GET",
"qs": {
"q" : query,
"sort": "new"
}
};
return function(cb) {
console.log(format("Wait a moment when we are getting all informations from reddit for %s", query));
request(options, function(err, result) {
cb(err, JSON.parse(result.body));
});
};
};
keywords.forEach(function(k) {
search_objects[k] = create_function(k);
});
async.parallel(search_objects, function(err, results){
if (err) { return console.log("erro", err); }
var num_comments = {};
_.forIn(results, function(data, k) {
num_comments[k] = lib.get_ncomments(data);
var number_posts = lib.get_numberposts(data);
var lastpost_author = lib.get_lastpostauthor(data);
console.log(format("\n%s have a total of %s of posts", k, number_posts));
console.log(format("%s author of the last post is '%s'", k, lastpost_author));
console.log(format("%s sum of comments of all posts is %s", k, num_comments[k]));
});
var comments_percent = lib.compare_comments(num_comments);
var calc_str = "\n";
_.forIn(comments_percent, function(data, k) {
if (data === "not a number") { console.log(format("Was not possible to calculate the percent of comments for %s, reason %s", k, data)); }
else { calc_str += format("%s has %s% of comments; ", k, data); }
});
console.log(calc_str);
});