-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
55 lines (51 loc) · 1.9 KB
/
bot.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
var env = process.env,
https = require('https'),
gzip = require('zlib'),
ent = require('ent'),
Twitter = require('ntwitter'),
twitter = new Twitter({
consumer_key: env.TWITTER_CONSUMER_KEY,
consumer_secret: env.TWITTER_CONSUMER_SECRET,
access_token_key: env.TWITTER_TOKEN_KEY,
access_token_secret: env.TWITTER_TOKEN_SECRET
});
function defErrorHandler(err) {
if (err) {
console.error(err);
}
}
function sendToTwitter(question) {
var tweet = ent.decode(question.title);
// Twitter shortens URLs up to 20 characters
// So we take 25 characters of title just in case
if (tweet.length > 115) {
tweet = tweet.substr(0, 112) + '...';
}
console.log('Question: ' + tweet);
tweet += ' http://stackoverflow.com/questions/' + question.question_id;
twitter.updateStatus(tweet, defErrorHandler);
}
exports.check = function (mins) {
var lastRunTimeInSeconds = Math.floor(new Date(new Date().toUTCString()).getTime() / 1000) - 60 * mins,
apiPath = '/2.0/questions?fromdate=' + lastRunTimeInSeconds +
'&order=desc&sort=creation&tagged=yui&site=stackoverflow&key=' +
env.SO_KEY;
console.log('Checking Stack Overflow: http://api.stackexchange.com' + apiPath);
https.get({ host: 'api.stackexchange.com', path: apiPath }, function (res) {
var result = '';
res
.pipe(gzip.createGunzip())
.on('data', function (chunk) {
result += chunk;
})
.on('end', function () {
result = JSON.parse(result);
if (result.error_id) {
defErrorHandler(result);
} else {
console.log('Publishing ' + result.items.length + ' questions...');
result.items.forEach(sendToTwitter);
}
});
}).on('error', defErrorHandler);
};