-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtweet-sweeper.js
More file actions
178 lines (155 loc) · 6.09 KB
/
tweet-sweeper.js
File metadata and controls
178 lines (155 loc) · 6.09 KB
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
var Engage = require('engage-api');
var Twit = require('twit');
var engage = Engage(require('./config/engage.js'));
var twitter = new Twit(require('./config/twitter.js'));
var twitterHandles = [];
var twitterListId = null;
/* hash of twitter handle -> (array of recipient id) */
/* {"joelafiosca": [12345, 34567]} */
var recipientIdsByHandle = {};
/* hash of recipient id -> email address */
/* {12345: "jlafiosc@us.ibm.com", 34567: "jlafiosca@silverpop.com"} */
var emailByRecipientId = {};
/* hash of recipient id -> (array of contact list id) */
/* {12345: [77777, 77778], 34567: [77774]} */
var additions = {};
var exportOptions = {
listId: 4782179, // Engage database id to sweep for
exportType: Engage.EXPORT_TYPE.OPT_IN,
exportColumns: ["Email", "RECIPIENT_ID", "TwitterHandle"],
forEachCallback: function(rec) {
if (rec.TwitterHandle) {
twitterHandles.push(rec.TwitterHandle);
if (!recipientIdsByHandle[rec.TwitterHandle]) {
recipientIdsByHandle[rec.TwitterHandle] = [];
}
recipientIdsByHandle[rec.TwitterHandle].push(rec.RECIPIENT_ID);
emailByRecipientId[rec.RECIPIENT_ID] = rec.Email;
}
}
};
// run the app:
_exportEngageDatabase();
function _exportEngageDatabase() {
console.log('Exporting Engage database for Twitter handles');
engage.exportListForEach(exportOptions, function(err) {
if (err) {
console.log('! Failed to export Engage database: ' + err);
} else if (twitterHandles.length === 0) {
console.log('- No Twitter handles found to sweep');
} else {
console.log('- Found ' + twitterHandles.length + ' Twitter handles to sweep');
_createTwitterList();
}
});
}
function _createTwitterList() {
console.log('Creating Twitter list');
twitter.post('lists/create', {name: "amplify-demo", mode: "private"}, function(err, data, response) {
if (err) {
console.log('! Failed to create Twitter list: ' + err);
} else {
twitterListId = data.id;
console.log('- Created Twitter list id ' + twitterListId);
twitter.post('lists/members/create_all', {list_id: twitterListId, screen_name: twitterHandles.join(',')}, function(err, data, response) {
if (err) {
console.log('! Failed to populate Twitter list: ' + err);
} else {
console.log('- Populated Twitter list with ' + twitterHandles.length + ' handles');
_sweepTweets();
}
});
}
});
}
function _sweepTweets() {
console.log('Sweeping Twitter list for statuses');
twitter.get('lists/statuses', {list_id: twitterListId, include_rts: false}, function(err, data, response) {
if (err) {
console.log('! Failed to read Twitter list statuses: ' + err);
} else {
data.forEach(function(tweet) {
//console.log('- @' + tweet.user.screen_name + ': ' + tweet.text);
tweet.text.match(/\S+/g).forEach(function(word) {
if (word.charAt(0) === '#') {
_processHashtag(tweet.user.screen_name, word.substring(1));
}
});
});
_handleAdditions();
}
});
}
function _processHashtag(twitterHandle, hashtag) {
if (!recipientIdsByHandle[twitterHandle]) {
console.log("! Received tweet from unrecognized handle '" + twitterHandle + "'");
} else {
recipientIdsByHandle[twitterHandle].forEach(function(recipientId) {
var email;
var contactListId = _getContactListIdForHashtag(hashtag);
if (contactListId !== null) {
email = emailByRecipientId[recipientId];
console.log("- @" + twitterHandle + " tweeted #" + hashtag + " -> add " + email + " (" + recipientId + ") to contact list " + contactListId);
if (!additions[recipientId]) {
additions[recipientId] = [contactListId];
} else if (additions[recipientId].indexOf(contactListId) === -1) {
additions[recipientId].push(contactListId);
}
}
});
}
}
function _getContactListIdForHashtag(hashtag) {
switch (hashtag) {
case 'hike':
case 'hiking':
case 'mountain':
return 4800385; // contact list id for hiking shoes
case 'walk':
case 'walking':
case 'steps':
return 4822445; // contact list id for walking shoes
case 'beach':
case 'ocean':
case 'coast':
return 4822450; // contact list id for beach shoes
case 'running':
case 'jogging':
case 'marathon':
return 4822447; // contact list id for running shoes
default:
return null;
}
}
function _handleAdditions() {
var recipientId, email, contactListId;
console.log('Processing contact list additions');
for (recipientId in additions) {
email = emailByRecipientId[recipientId];
console.log('- For ' + email + ' (' + recipientId + ')');
additions[recipientId].forEach(_handleAddition.bind(null, email, recipientId));
}
_destroyTwitterList();
}
function _handleAddition(email, recipientId, contactListId) {
console.log('-- Add to contact list ' + contactListId);
engage.addContactToContactList({contactId: recipientId, contactListId: contactListId}, function(err) {
if (err) {
console.log('! Failed to add ' + email + ' (' + recipientId + ') to contact list ' + contactListId);
}
});
}
function _destroyTwitterList() {
console.log('Destroying Twitter list id ' + twitterListId);
twitter.post('lists/destroy', {list_id: twitterListId}, function(err, data, response) {
if (err) {
console.log('! Failed to destroy Twitter list: ' + err);
} else {
console.log('- Destroyed Twitter list');
_finalReport();
}
});
}
function _finalReport() {
console.log('Done');
}