-
Notifications
You must be signed in to change notification settings - Fork 11
/
vegassist.js
88 lines (79 loc) · 3.53 KB
/
vegassist.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
var Twit = require('twit');
var settings = require('./settings.js');
var TweetFilter = require('./lib/filter');
var util = require('./lib/util');
var path = require('path');
var fs = require('fs');
var pg = require('pg');
// Declare your own Twitter app credentials here, if duplicating
var T = new Twit(settings.CREDS);
// Set a delay time constant of 2 minutes - RTs will be delayed by this amount
var DELAYTIME = 1000 * 60 * 2;
// Load filters from all files in the filters directory
var settingsFilteredTerms = settings.FILTERED_TERMS || [];
var settingsFilters = settings.FILTERS || [];
var handle = settings.HANDLE || "";
if (handle.length > 0){
if (handle.indexOf('@') != 0)
handle = '@' + handle;
util.track(handle);
}
const client = new pg.Client({
user: settings.DATABASE['user'],
password: settings.DATABASE['password'],
database: settings.DATABASE['db_name']
});
client.connect();
var filter = new TweetFilter('filters', settingsFilteredTerms, settingsFilters, handle);
// Whenever the Twitter stream notifies us of a new Tweet with the term 'vegan' (or its international equivalents), we handle it!
var stream = T.stream('statuses/filter', { track: util.trackedTerms });
// Run with option '--dry-run' to disable retweeting and instead log matches to console
var isDryRun = process.argv[2] === '--dry-run';
// Use a different log file for dry-run
var logFile = path.join(__dirname, (isDryRun ? 'matches.dry-run' : 'matches') + '.json');
// Check that we can write to disk, so that we can log it upfront and safely ignore errors later
// This allows the bot to function even if it doesn't have write access to the disk
fs.appendFile(logFile, "", function(err) {
if (err) { console.warn("Unable to write to log file: " + path.relative(__dirname, logFile)); }
})
console.log("Loaded filters: " + Object.keys(filter.filters).join(", "));
// log tweets and their matches as json, each tweet/match will be a separate line of json
var logMatches = function(tweet, matches) {
// flatten match objects a bit
// matches = matches.map(function(match){
// return {match: match[0], index: match.index, filter: match.filter.toString(), filterList: match.filterList };
//})
// provide an empty callback to swallow errors
// fs.appendFile(logFile, JSON.stringify({ tweet: tweet, matches: matches }) + "\n", function(){});
client.query('INSERT INTO retweet(tweet_id, text, time, username, user_follower_count) VALUES($1, $2, $3, $4, $5)', [tweet.id, tweet.text, tweet.created_at, tweet.user.followers_count], function(){
client.query('COMMIT');
});
}
console.log("Tracking terms: " + util.trackedTerms.join(", "));
stream.on('connect', function (response) {
console.log("Connecting to Twitter..." + (isDryRun ? " (dry run, will not retweet matches)" : ""))
})
stream.on('connected', function (response) {
console.log("Connected")
})
stream.on('reconnect', function (response) {
console.log("Reconnecting...")
})
stream.on('tweet', function(tweet) {
var matches = filter.matches(tweet);
if (matches) {
logMatches(tweet, matches);
if (isDryRun) {
console.log(tweet.id_str + ' : ' + tweet.user.screen_name + ' : ' + tweet.text);
return;
}
// positive match; let's retweet!
setTimeout(function(){T.post('statuses/retweet/:id', {id: tweet.id_str}, function(err, data, response) {
if (err) {
console.log(err);
return false;
}
console.log('Retweeted: ' + tweet.id_str);
});}, DELAYTIME);
}
});