-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartobot.js
187 lines (170 loc) · 5.47 KB
/
cartobot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*jslint node: true, white: true, nomen: true, vars: true, unparam: true */
"use strict";
var config = require('./etc/config');
var TwitterBot = require('./twitter-bot');
var PocketBot = require('./pocket-bot');
var Twit = require('twit');
var StdIo = require('stdio');
var Logger = require('simple-node-logger');
function CartoBot(config, loggers) {
this.config = config;
this.loggers = loggers;
this.log = this.loggers.cartobot;
this.twit = new Twit(this.config.twitter.auth);
this.twitterBot = new TwitterBot({
config: this.config,
twit: this.twit,
logger: this.loggers.twitterbot
});
this.pocketBot = new PocketBot({
config: this.config,
twit: this.twit,
logger: this.loggers.pocketbot
});
}
CartoBot.prototype = {
refreshConfig: function() {
var self = this;
this.log.info('Getting latest t.co limits from Twitter ...');
this.twit.get('help/configuration', {}, function(err, reply) {
if (err) {
self._errorHandler(err);
}
else {
self.config.twitter.short_url_length = reply.short_url_length;
self.config.twitter.short_url_length_https = reply.short_url_length_https;
self.twitterBot.refreshConfig(self.config);
self.log.info('Current Twitter t.co limits are ' + reply.short_url_length + ' chars (' + reply.short_url_length_https + ' for HTTPS)');
}
});
},
run: function() {
this.log.info('Running ...');
// this.log.debug('Getting current Twitter config ...');
// var self = this;
// this.twit.get('help/configuration', {}, function(err, reply) {
// if (err) {
// self._errorHandler(err);
// }
// else {
// console.log(reply);
// self.config.twitter.short_url_length = reply.short_url_length;
// self.config.twitter.short_url_length_https = reply.short_url_length_https;
//
// this.twitterBot = new TwitterBot(this.config, this.twit);
// this.pocketBot = new PocketBot(this.config, this.twit);
//
// this.twitterBot.run();
// this.pocketBot.run();
// }
// });
if (this.config.twitter.enabled === true) {
this.twitterBot.run();
}
else {
this.loggers.twitterbot.info('Twitter search is disabled, skipping ...');
}
if (this.config.getpocket.enabled === true) {
this.pocketBot.run();
}
else {
this.loggers.pocketbot.info('GetPocket search is disabled, skipping ...');
}
},
_errorHandler: function(err) {
this.log.error(err);
}
};
var options = StdIo.getopt({
'debug': {
key: 'd',
description: 'Produce loads of debug logging',
mandatory: false
},
'interval': {
key: 'i',
description: 'How often CartoBot wakes up and runs, in minutes',
mandatory: false,
args: 1,
default: 120
},
'tweetcount': {
key: 't',
description: 'Maximum number of Tweets to search for',
mandatory: false,
args: 1,
default: 5
},
'bookmarkcount': {
key: 'b',
description: 'Maximum number of unarchived GetPocket bookmarks to search for',
mandatory: false,
args: 1,
default: 5
},
'notwitter': {
key: 'T',
description: 'Disable searching for related Tweets',
mandatory: false
},
'nopocket': {
key: 'P',
description: 'Disable searching for unarchived GetPocket bookmarks',
mandatory: false
},
'norepeat': {
key: 'R',
description: 'Run once and then terminate',
mandatory: false
},
'dryrun': {
key: 'D',
description: 'Run searches but don\'t Tweet or Archive GetPocket bookmarks',
mandatory: false
}
});
var title = 'cartobot';
if (options.title) {
title = options.title;
}
process.title = title;
var logManager = Logger.createLogManager();
var logLevel = 'info';
if (options.debug) {
logLevel = 'debug';
}
var loggers = {
cartobot: logManager.createLogger('CartoBot', logLevel),
twitterbot: logManager.createLogger('TwitterBot', logLevel),
pocketbot: logManager.createLogger('PocketBot', logLevel)
};
var interval = (options.interval * 60) * 1000;
loggers.cartobot.debug('Running every ' + options.interval + ' minutes (' + interval + ' ms)');
if (options.notwitter) {
config.twitter.enabled = false;
}
if (options.nopocket) {
config.getpocket.enabled = false;
}
if (options.dryrun) {
config.dryrun = true;
}
config.twitter.tweets = options.tweetcount;
config.getpocket.bookmarks = options.bookmarkcount;
var cartoBot = new CartoBot(config, loggers);
if (!options.norepeat) {
setInterval(function() {
loggers.cartobot.info('I\'m awake and making coffee ...');
cartoBot.run();
loggers.cartobot.info('All done; time for a snooze');
}, interval);
var refreshInterval = 86400000;
setInterval(function() {
loggers.cartobot.info('I\'m awake and checking the daily config for t.co');
cartoBot.refreshConfig();
loggers.cartobog.info('Config check done; snoozing for another 24 hours');
}, refreshInterval);
}
cartoBot.refreshConfig();
cartoBot.run();
loggers.cartobot.info('All done; time for a snooze');