-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnightly_random_discussion.js
67 lines (58 loc) · 1.53 KB
/
nightly_random_discussion.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
var strftime = require('strftime');
var request = require('request-json');
var config = require('./lib/config');
var reddit = require('./lib/reddit');
// first argument from the command line can be used to
// override the subreddit that we are submitting to
//
if (process.argv[2]) {
config.defaults.subreddit = process.argv[2];
}
// the actual post to be submitted
//
var submission = {
'title': strftime('Nightly random discussion - %b %d, %Y'),
'text': config.slogan + 'Magandang hatinggabi r/' + config.defaults.subreddit + '!',
'r': config.defaults.subreddit,
'inboxReplies': false,
'save': false,
};
reddit.submit(submission, function(err, id) {
if (err) {
error(err);
} else {
log('submitted ' + id);
var client = request.newClient('http://www.reddit.com/'),
url = '/r/AskReddit/top/.json';
client.get(url, function(err, res, body) {
if (!err) {
try {
post = body.data.children[0].data
} catch(e) {}
if (post) {
var text = 'Tonights\'s [Ask PHreddit](' + post.url + '): '
+ post.title;
reddit.comment('t3_' + id, text, function(err, id) {
if (err) {
error(err);
} else {
exit('commented ' + id);
}
});
}
}
});
}
});
// in case of error, log to STDERR and exit
//
function error(err) {
exit('ERROR: ' + err);
}
function log(msg) {
console.log(strftime('%F %T ') + msg);
}
function exit(msg) {
log(msg);
process.exit();
}