-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifyFollowers.js
171 lines (156 loc) · 6.12 KB
/
notifyFollowers.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
"use strict";
var Twitter = require('twitter');
var sendEmail = require('./email.js');
var config = require('./config.js');
var async = require('./async.js').async;
Array.prototype.subtract = function(a) {
return this.filter(function(i) { return a.indexOf(i) === -1; });
};
function getFollowers(T, screenName) {
return new Promise((resolve, reject) => {
T.get('followers/ids', { screen_name: screenName }, function (err, data, response) {
if (err) {
console.log('err: ' + JSON.stringify(err));
return reject(new Error(err));
}
if (data.ids) {
// console.log('ids: ' + data.ids);
// console.log('ids: ' + JSON.stringify(data));
// console.log('id0: ' + data.ids[0]);
// console.log('response: ' + JSON.stringify(response));
var b = response.body;
var idStrings = b.substring(b.indexOf('[') + 1, b.indexOf(']')).split(',');
//console.log('resonse.ids: ' + idStrings);
resolve(idStrings);
} else {
return reject(new Error('returned null list of followers!'));
}
});
});
}
function printArrayTypes(array, arrayName) {
var arrayString = array.map(e => { return typeof(e); });
console.log("types for " + arrayName + ": " + arrayString);
}
function notify(emailOptions, message) {
let localEmailOptions = JSON.parse(JSON.stringify(emailOptions));
localEmailOptions.htmlMessage = message;
sendEmail(localEmailOptions)
.then(res => console.log('sent notification email! ' + res.response))
.catch(err => console.log('failed to send notification email! ' + err.stack));
}
function getFollowerFromIds(T, ids) {
return new Promise((resolve, reject) => {
console.log('looking up user ids: ' + ids + ', typeof id: ' + typeof(ids.toString()));
T.get('users/lookup', { user_id: ids.toString() }, function (err, data, response) {
if (err) {
console.log('err: ' + JSON.stringify(err));
return reject(new Error(err));
}
if (data) {
console.log('follower ' + JSON.stringify(data, null, 2) + ' from ids: ' + ids);
resolve(data);
} else {
console.log('got null data from user lookup api, response: ' + response);
resolve([]);
}
});
});
}
var checkNotify = async(function* (T, followersList, emailOptions, screenName) {
try {
var latestIds = yield getFollowers(T, screenName);
// printArrayTypes(latestIds, 'fresh idStrings');
// printArrayTypes(followersList, 'followersList');
// console.log('\r\ngot latestIds: ' + latestIds + '\r\nfollowersList: ' + followersList);
let follows = latestIds.subtract(followersList);
let unfollows = followersList.subtract(latestIds);
// console.log('\r\ngot follows: ' + follows + '\r\nunfollows: ' + unfollows);
let notificationMessage = {};
if (follows && follows.length > 0) {
console.log('found new follows: ' + follows);
notificationMessage.followProfiles = yield getFollowerFromIds(T, follows);
follows.map(e => { followersList.push(e); });
}
if (unfollows && unfollows.length > 0) {
console.log('found new unfollows: ' + unfollows);
notificationMessage.unfollowProfiles = yield getFollowerFromIds(T, unfollows);
for (let i = 0, ulen = unfollows.length; i < ulen; i++) {
for (let j = 0, flen = followersList.length; j < flen; j++) {
if (followersList[j] == unfollows[i]) {
console.log('removing unfollows: ' + unfollows[i] +
' == followersList: ' + followersList[j]);
followersList.splice(j, 1);
}
}
}
}
if (Object.keys(notificationMessage).length > 0) {
console.log('found changes in followers list: ' + JSON.stringify(notificationMessage));
notify(emailOptions, JSON.stringify(notificationMessage, null, 2));
} else {
console.log('no changes found in followers list: ' +
JSON.stringify(notificationMessage));
}
} catch (e) {
console.log('failed in checkNotify: ' + JSON.stringify(e) + e.stack);
}
});
function notificationLoop(T, followersList, emailOptions, checkIntervalSeconds) {
console.log('setup to check api every ' + checkIntervalSeconds + ' seconds');
setInterval(() => {
console.log((new Date()).toString() + ' checking followers');
checkNotify(T, followersList, emailOptions);
}, checkIntervalSeconds * 1000);
}
var testNotify = async(function* (T, ids, emailOptions) {
try {
console.log('emailing: ');
var followerFromID = yield getFollowerFromIds(T, [1026, 160763]);
console.log('follower from id: ' + JSON.stringify(followerFromID));
var result = yield notify(emailOptions, JSON.stringify(followerFromID));
console.log('notify result: ' + JSON.stringify(result));
} catch(e) {
console.log(e.stack);
}
});
function main() {
console.log('starting, using config: ' + JSON.stringify(config));
var T = new Twitter({
consumer_key: config.twitOptions.consumer_key,
consumer_secret: config.twitOptions.consumer_secret,
access_token_key: config.twitOptions.access_token,
access_token_secret: config.twitOptions.access_token_secret
});
//testNotify(T, [1026, 160763], config.emailOptions);
// var initialFollowers = []
// getFollowers(T, config.screenName)
// .then(res => {
// initialFollowers = res;
// console.log('initial followers ids: ' + res);
// return initialFollowers;
// })
// .then(res => {
// getFollowerFromIds(T, res[0])
// .then(res => console.log('res: ' + JSON.stringify(res)))
// })
// .catch(err => console.log('err: ' + err.stack));
getFollowers(T, config.screenName)
.then(initialFollowers => {
console.log('initial followers ids: ' + initialFollowers);
if (initialFollowers && initialFollowers.length > 0) {
return initialFollowers;
} else {
throw new Error('failed to get initial followers list');
}
})
.then(res => {
notificationLoop(T, res, config.emailOptions, config.checkIntervalSeconds);
})
.catch(err => {
console.log('failed in main: ' + err.stack);
});
}
if (require.main === module) {
main();
}