-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
55 lines (47 loc) · 1.37 KB
/
main.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
'use strict';
const config = require('./config.json');
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./mentalbot.sqlite');
const Rawjs = require('raw.js');
const reddit = new Rawjs(config.useragent);
const program = require('commander');
const Winston = require('winston');
const logger = new (Winston.Logger)({
transports: [
new (Winston.transports.File)({
filename: 'mentalbot.log',
}),
],
});
const mentalbot = require('./mentalbot.js')(db, reddit, config, logger);
reddit.setupOAuth2(
config.clientID,
config.secret
);
program
.version('1.0.0')
.option('-r, --ignore-reddit', 'Do not post to Reddit')
.parse(process.argv);
reddit.auth({ username: config.username, password: config.password }, (error) => {
if (error) {
logger.log('error', 'Unable to authenticate user: %s', error);
db.close();
} else {
mentalbot.createTable().then(
() => mentalbot.getRecentPosts()
).then(
(body) => mentalbot.parseRecentPosts(body)
).then((data) => {
if (data === undefined) return [];
const posts = data.map(mentalbot.savePost);
return Promise.all(posts);
}).then((data) => {
if (program.ignoreReddit) return true;
const newPosts = data.map(mentalbot.postToReddit);
return Promise.all(newPosts);
}).then(() => {
db.close();
reddit.logout();
});
}
});