-
Notifications
You must be signed in to change notification settings - Fork 0
/
headonbot.js
124 lines (115 loc) · 5.08 KB
/
headonbot.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
var fs = require("fs");
var config = require("./config.js");
var mongoose = require("mongoose");
require("./schema.js");
var Comment = mongoose.model('Comment');
mongoose.connect(config.mongoConnectionUri);
var db = mongoose.connection;
db.on("open", function () {
var Snoocore = require("snoocore");
var reddit = new Snoocore({
userAgent: 'snoocore:headonbot:v0.1.0 by /u/ki85squared',
login: { username: config.reddit.username, password: config.reddit.password },
oauth: {
type: 'script',
consumerKey: config.reddit.consumerKey,
consumerSecret: config.reddit.consumerSecret,
scope: [ 'flair', 'identity', 'read', 'submit', 'history' ]
}
});
var message = "[Apply directly to the forehead!](https://www.youtube.com/watch?v=f_SwD7RveNE)";
var regex = /\s(?:headon|head[\s-]on)[^a-zA-Z0-9]*$/i;
// Grab comments here //
reddit.auth().then(function() {
onAuthHandler();
}).catch(function (err) {
console.log(err);
});
reddit.on("access_token_expired", function (err) {
restart(err);
});
function restart (err) {
// "restart" technically is exit, but forever will re-run after exit
var message = new Buffer((new Date().toString() + " | " + err));
console.log(message.toString());
fs.writeFileSync("./auth_err.log", message);
setTimeout(function () {
process.exit(1);
}, 10000); // wait 10 sec to quit if auth fails
}
function onAuthHandler () {
setInterval(function () {
reddit('/r/all/comments.json').get().then(function(result) {
//console.log("Scanned " + result.data.children.length + " comments");
if (result.hasOwnProperty("data") && result.data.hasOwnProperty("children")) {
var comments = result.data.children;
for (var i=0; i<comments.length; i++) {
if (!comments[i].hasOwnProperty("data") || !comments[i]["data"].hasOwnProperty("body")) {
// skip if the required data isn't there
continue;
}
var cBody = comments[i]["data"]["body"];
if (cBody && cBody.match(regex)) {
var comment = result.data.children[i].data;
(function (comment) {
Comment.findOne({_id:comment.name}, function (err, doc) {
if (doc) {
Comment.update({_id : comment.name}, {$set:{body : comment.body}}, {upsert:true}, function (err) {
if (err) {console.log(err);}
});
} else {
new Comment({
_id : comment.name,
body : comment.body
}).save();
}
});
})(comment);
}
}
}
});
},3000); // Grab new comments every 3 seconds
var throttle = 0; // throttle is used to churn through queue.
var throttleInterval = 10000; // check queue every ten seconds
// Counts down the throttle //
setInterval(function () {
switch (throttle) {
case (throttle > throttleInterval):
throttle -= throttleInterval;
break;
default:
throttle = 0;
// time to post, if there are any in the queue
Comment.find({replied:false}).sort({capturedDate:-1}).limit(1).exec(function (err, result) {
if (err) { console.log(err); }
else {
if (result.length > 0) {
postComment(result[0]._id, function (err) {
if (err) { console.dir(err); }
});
throttle = 300000; // reset to 5 mins after each comment posting attempt
}
}
});
}
}, throttleInterval);
}
// Posts the comment //
function postComment (commentId, cb) {
reddit('/api/comment').post({
api_type : "json",
text : message,
thing_id : commentId
}).then(function(data) {
console.log(data); // Log the response
if (data.errors && data.errors.length > 0) {
cb(data.errors);
} else {
Comment.update({_id:commentId}, {$set:{replied:true, repliedDate: new Date()}}, function (err) {
if (err) {cb(err);} else { console.log("replied!"); cb(null);}
});
}
});
}
});