-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
179 lines (149 loc) · 5.33 KB
/
twitter.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
var Twit = require('twit'),
credentials = require('./credentials'),
Util = require('./util'),
Database = require('./database'),
Logs = require('./logs');
// console.log('JSON.stringify(Database)');
// console.log(JSON.stringify(Database));
// console.log(Database);
function stream (user, limit, callback) {
var userTw = new Twit({
consumer_key: credentials.APP_TOKEN,
consumer_secret: credentials.APP_SECRET,
access_token: user.token,
access_token_secret: user.tokenSecret
});
var StartStream = Date.now();
if(!user.settings.track || !user.settings.trackLangs || !user.settings.trackLangs.length || !user.settings.track.length){
//console.log('user: ' + user.twitter.screen_name + " does not have track or tracklangs");
Logs.twitterErr('user: ' + user.twitter.screen_name + " does not have track or tracklangs");
callback();
return;
}
var stream = userTw.stream('statuses/filter', { track: user.settings.track.join(), language: user.settings.trackLangs.join() });
var usersToFollow = [];
stream.on('tweet', function (tweet) {
var streamTimeOut = (Date.now() - StartStream) < 60000; //pasaron menos de 1 Minuto?
if (usersToFollow.length < limit && streamTimeOut) {
if (usersToFollow.map(function (uData){ return uData.id }).indexOf(tweet.user.id) >= 0 || !_checkDifference(user, tweet.user, 'followOnDifference') || !_filterUser(tweet)) {
return;
}
//console.log(tweet.text);
usersToFollow.push(_parseStreamUser(tweet.user));
} else {
stream.stop();
if(streamTimeOut == false){
console.log("*****************WARNING: STREAMING TIMEOUT");
//TODO: SEND NOTIFICATION TO USER
}
console.log("*****************STREAMING TIME:")
console.log(Date.now() - StartStream);
return callback(usersToFollow);
}
});
stream.on('error', function (error) {
//console.log("ERROR EN TWITTER STREAM!!!");
//console.log(error.message);
Logs.twitterErr("Stream ERROR with user: " + user.twitter.screen_name + " - msg: "+ error.message);
});
}
function _filterUser (tweet) {
var permited = true;
var BlackList = ['+18', 'porn', 'sex', 'gay', 'lesbian', 'xxx', 'calient', 'puta', 'puto', 'cunt', 'fuck', 'milf', 'Pics', 'hot', 'chicks', 'pibas'];
checkOnBlackList(tweet.text);
['name', 'screen_name', 'description'].forEach(function (property) {
checkOnBlackList(tweet.user[property]);
});
function checkOnBlackList (data) {
BlackList.forEach(function (word){
if (data && data.indexOf(word) !== -1){
return permited = false;
}
});
}
return permited;
}
function checkFollowers (user, unfollow, callback) {
usersToCheck = Util.getIds(unfollow);
if (usersToCheck.constructor !== Array) {
return callback('Need usersToCheck to be an array');
}
var userTw = new Twit({
consumer_key: credentials.APP_TOKEN,
consumer_secret: credentials.APP_SECRET,
access_token: user.token,
access_token_secret: user.tokenSecret
});
userTw.get('friendships/lookup', { user_id: usersToCheck.join() }, function (err, usersChecked){
if (err) {
return callback('Error while looking up friendship of: ' + user.twitter.screen_name + " | err: " + err);
}
var notFollowers = [];
var followers = [];
var newFollowers = 0;
usersChecked.forEach(function (userChecked, index) {
if (userChecked.connections.indexOf('followed_by') > -1) { //if user is following back
if (_checkDifference(user, unfollow[index], 'unfollowOnDifference')) {
console.log("unfollow! -- unfollowOnDifference")
notFollowers.push(userChecked.id);
} else {
console.log('continue following!');
followers.push(userChecked.id);
}
newFollowers++;
} else { //if(userChecked.connections.indexOf('following') > -1) {
console.log('unfollow!');
notFollowers.push(userChecked.id);
}
});
Database.saveMetrics(user, { newFollowers: newFollowers, peopleReached: usersChecked.length });
callback(null, notFollowers, followers);
});
}
function generateDailyStats (user, callback) {
console.log("GENERATE DAILY STATS");
var userTw = new Twit({
consumer_key: credentials.APP_TOKEN,
consumer_secret: credentials.APP_SECRET,
access_token: user.token,
access_token_secret: user.tokenSecret
});
console.log('user.id');
console.log(user.id);
userTw.get('users/show', { id: user.id }, function (err, twitterUserData){
if (err) {
//console.log('Error while looking up status of: ' + user.twitter.screen_name + ' (' + user.id + ')');
Logs.twitterErr('Error while looking up status of: ' + user.twitter.screen_name + ' (' + user.id + ')');
return;
}
//return callback(null,twitterUserData);
//console.log(JSON.stringify(Database));
Database.saveDailyStats(user, twitterUserData);
});
}
function _checkDifference (user , tweetUser, difference) {
if (!user.settings[difference]){
if (difference == "unfollowOnDifference"){
return false;
} else if (difference == "followOnDifference") {
return true;
}
}else {
return Math.floor(tweetUser.friends_count*100/tweetUser.followers_count) >= user.settings[difference];
}
}
function _parseStreamUser (user) {
return {
id: user.id,
name: user.name,
screen_name: user.screen_name,
lang: user.lang.split("-")[0], // if es-MX --> es
friends_count: user.friends_count,
followers_count: user.followers_count
}
}
module.exports = {
stream: stream,
checkFollowers: checkFollowers,
generateDailyStats: generateDailyStats
};