-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterbot.js
428 lines (401 loc) · 15.5 KB
/
twitterbot.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
(function() {
var JSONStream, OAuth, async, bearerToken, createRelationship, datejs, getData, getFriends, getTweets, getUserInfo, init, insertNewUser, keys, mergeNode, mysql, mysqlConn, neo4j, neodb, req_count, request, updateSinceIdQuery, updateUserInfoQuery, userArray, userFields;
request = require("request");
JSONStream = require("JSONStream");
OAuth = require("oauth");
neo4j = require("neo4j");
async = require("async");
mysql = require("mysql");
datejs = require("datejs");
keys = require("./keys.js");
neodb = new neo4j.GraphDatabase("http://localhost:7474");
userArray = ["petecashmore", "denverfoodguy", "BrianBrownNet", "GuyKawasaki", "om", "BarackObama", "NBA", "jack", "guardiantech", "stephenfry", "WSJ", "umairh", "rustyrockets", "tinchystryder", "HilaryAlexander", "Zee", "jemimakiss", "RichardDawkins"];
req_count = 0;
bearerToken = void 0;
mysqlConn = void 0;
updateUserInfoQuery = "UPDATE users SET name = ?, description = ?, created_at = ?, location=?, profile_image_url=?, profile_image_url_https=?, url=?, listed_count=?, favourites_count=?, followers_count=?, statuses_count= ?,friends_count = ? where id_str= ?";
userFields = ["name", "description", "created_at", "location", "profile_image_url", "profile_image_url_https", "url", "listed_count", "favourites_count", "followers_count", "statuses_count", "friends_count"];
updateSinceIdQuery = "Update users set since_id=? where id_str = ?";
init = function() {
var OAuth2, oauth2;
neodb = new neo4j.GraphDatabase("http://localhost:7474");
mysqlConn = mysql.createConnection({
host: keys.mysql_host,
database: keys.mysql_db,
user: keys.mysql_username,
password: keys.mysql_pwd
});
mysqlConn.connect();
OAuth2 = OAuth.OAuth2;
oauth2 = new OAuth2(keys.consumer_key, keys.consumer_secret, "https://api.twitter.com/", null, "oauth2/token", null);
return oauth2.getOAuthAccessToken("", {
grant_type: "client_credentials"
}, function(e, access_token, refresh_token, results) {
bearerToken = access_token;
userArray = [];
mysqlConn.query("SELECT screen_name FROM users order by created_at;", function(err, result) {
if (err) {
console.log(err);
}
result.forEach(function(user) {
return userArray.push(user.screen_name);
});
return getData(userArray);
});
return getFriends();
});
};
getFriends = function() {
return async.forever((function(next) {
return setTimeout(function() {
return mysqlConn.query("SELECT screen_name FROM users WHERE created_at IS NOT NULL AND friends IS NULL limit 1;", function(err, result) {
var options, stream;
if (err) {
console.log(err);
}
stream = JSONStream.parse();
options = {
url: "https://api.twitter.com/1.1/friends/list.json?",
qs: {
screen_name: result[0].screen_name,
skip_status: true,
include_user_entities: false
},
headers: {
"User-Agent": "AIC Data Mining",
Authorization: "Bearer " + bearerToken
}
};
request(options).pipe(stream);
return stream.on("root", function(obj) {
if (obj.users != null) {
return async.eachSeries(obj.users, (function(entry, entrycallback) {
var properties;
console.log("--------------------------------" + result[0].screen_name + " IS FOLLOWING " + entry.screen_name);
properties = {
id_str: entry.id_str,
name: entry.name,
followers_count: entry.followers_count,
friends_count: entry.friends_count,
listed_count: entry.listed_count,
favourites_count: entry.favourites_count,
statuses_count: entry.statuses_count
};
return mergeNode("User", "screen_name", entry.screen_name, properties, function(err, user) {
var from, to;
if (err) {
console.log(err);
}
from = {
type: 'User',
idKey: 'screen_name',
idVal: result[0].screen_name
};
to = {
type: 'User',
idKey: 'screen_name',
idVal: entry.screen_name
};
return createRelationship(from, to, "follows", function(err) {
if (err) {
console.log(err);
}
mysqlConn.query("Update users set friends=1 WHERE screen_name=?", result[0].screen_name, function(err, result) {});
if (userArray.indexOf(entry.screen_name) === -1) {
return insertNewUser(entry.id_str, entry.screen_name, function(err, result) {
if (err) {
console.log(err);
}
userArray.push(entry.screen_name);
console.log("------------NEW USER " + entry.screen_name);
return entrycallback(err);
});
} else {
return entrycallback(err);
}
});
});
}), function(err) {
if (err) {
console.log("ERROR: " + err);
}
return setImmediate(next);
});
}
});
});
}, 30000);
}), function(err) {
return console.log(err);
});
};
getData = function(userArray) {
return async.forever((function(next) {
var user;
user = userArray.shift();
console.log(user);
mysqlConn.query("SELECT created_at FROM users WHERE screen_name=?", user, function(err, result) {
if (err) {
console.log(err);
}
if (result[0].created_at == null) {
return getUserInfo({
screen_name: user,
include_entities: false
}, function(err) {
if (err) {
return console.log(err);
}
});
}
});
return getTweets({
screen_name: user,
count: 200
}, function() {
userArray.push(user);
return setImmediate(next);
});
}), function(err) {
return console.log(err);
});
};
getUserInfo = function(params, callback) {
var options, stream;
stream = JSONStream.parse();
options = {
url: "https://api.twitter.com/1.1/users/show.json?",
qs: params,
headers: {
"User-Agent": "AIC Data Mining",
Authorization: "Bearer " + bearerToken
}
};
request(options).pipe(stream);
return stream.on("root", function(obj) {
var values;
values = [];
userFields.forEach(function(field) {
if (field === "created_at") {
return values.push(new Date(Date.parse(obj[field])));
} else {
return values.push(obj[field]);
}
});
values.push(obj.id_str);
return mysqlConn.query(updateUserInfoQuery, values, function(err, result) {
if (err) {
return console.log(err);
}
});
});
};
getTweets = function(params, reqcallback) {
var options, stream;
stream = JSONStream.parse();
options = {
url: "https://api.twitter.com/1.1/statuses/user_timeline.json?",
qs: params,
headers: {
"User-Agent": "AIC Data Mining",
Authorization: "Bearer " + bearerToken
}
};
request(options).pipe(stream);
return stream.on("root", function(obj, count) {
return async.eachSeries(obj, (function(entry, entrycallback) {
return async.waterfall([
function(callback) {
var properties;
properties = {
id_str: entry.user.id_str,
name: entry.user.name,
followers_count: entry.user.followers_count,
friends_count: entry.user.friends_count,
listed_count: entry.user.listed_count,
favourites_count: entry.user.favourites_count,
statuses_count: entry.user.statuses_count
};
return mergeNode("User", "screen_name", entry.user.screen_name, properties, function(err, user) {
return callback(err, entry.user.screen_name);
});
}, function(userScreenName, callback) {
var properties;
properties = {
text: entry.text,
retweet_count: entry.retweet_count,
favorite_count: entry.favorite_count
};
return mergeNode("Tweet", "id_str", entry.id_str, properties, function(err, tweet) {
var from, to;
console.log(entry.id_str + " " + entry.text + "...");
from = {
type: 'User',
idKey: 'screen_name',
idVal: userScreenName
};
to = {
type: 'Tweet',
idKey: 'id_str',
idVal: entry.id_str
};
return createRelationship(from, to, "tweets", function(err) {
return callback(err, userScreenName, entry.id_str);
});
});
}, function(userScreenName, tweetIdStr, callback) {
if (entry.entities.hashtags.length > 0) {
async.forEach(entry.entities.hashtags, (function(ht, cb) {
return mergeNode("Hashtag", "text", ht.text, null, function(err, hashtag) {
var from, to;
if (err) {
console.log(err.message + "164543");
}
from = {
type: 'Tweet',
idKey: 'id_str',
idVal: tweetIdStr
};
to = {
type: 'Hashtag',
idKey: 'text',
idVal: ht.text
};
return createRelationship(from, to, "has_hashtag", function(err) {
return cb(err);
});
});
}), function(err) {
if (err) {
return console.log(err.message + "434321");
}
});
}
return callback(null, userScreenName, tweetIdStr);
}, function(userScreenName, tweetIdStr, callback) {
if (entry.in_reply_to_status_id) {
mergeNode("User", "screen_name", entry.in_reply_to_screen_name, {
id_str: entry.in_reply_to_user_id_str
}, function(err, replyuser) {
if (err) {
console.log(err.message + "342345");
}
return mergeNode("Tweet", "id_str", entry.in_reply_to_status_id_str, null, function(err, replytweet) {
var query;
if (err) {
console.log(err.message(+"754656"));
}
query = "MATCH (tweet:Tweet { id_str:'" + tweetIdStr + "' }), (newuser:User { screen_name:'" + entry.in_reply_to_screen_name + "' }), (newtweet:Tweet { id_str:'" + entry.in_reply_to_user_id_str + "' }) ";
query += "MERGE (tweet)-[r:in_reply_to]->(newtweet:Tweet { id_str:'" + entry.in_reply_to_user_id_str + "' })<-[s:tweets]-(newuser:User { screen_name:'" + entry.in_reply_to_screen_name + "' }) RETURN r, s ";
return neodb.query(query, function(err, saved) {
if (err) {
return console.log(err.message + "756334");
}
});
});
});
}
return callback(null, userScreenName, tweetIdStr);
}, function(userScreenName, tweetIdStr, callback) {
if (entry.entities.user_mentions.length > 0) {
async.eachSeries(entry.entities.user_mentions, (function(um, cb) {
var properties;
properties = {
id_str: um.id_str,
name: um.name,
screen_name: um.screen_name
};
return mergeNode('User', 'screen_name', um.screen_name, properties, function(err, mentioned) {
/*if userArray.indexOf(um.screen_name) is -1 && um.screen_name isnt userScreenName
insertNewUser(um.id_str, um.screen_name, (e, result) ->
userArray.push um.screen_name
console.log "NEW USER ADDED "+ um.screen_name
)
*/
var from, to;
from = {
type: 'Tweet',
idKey: 'id_str',
idVal: tweetIdStr
};
to = {
type: 'User',
idKey: 'screen_name',
idVal: um.screen_name
};
return createRelationship(from, to, "mentions", function(err) {
if (err) {
console.log(err.message + "756534");
}
return cb;
});
});
}), function(err) {
if (err) {
return console.log(err.message + "48632");
}
});
}
return callback(null, userScreenName, tweetIdStr);
}
], function(result, err) {
return entrycallback();
});
}), function(err) {
if (err) {
console.log("ERROR: " + err);
}
return reqcallback();
});
});
};
insertNewUser = function(id_str, screen_name, callback) {
var sql;
console.log("params " + id_str + " " + screen_name);
return sql = mysqlConn.query('INSERT INTO users (id_str, screen_name) values (?, ?)', [id_str, screen_name], function(err, result) {
if (err) {
console.log(err.message + "95634");
}
return callback(err, result);
});
};
mergeNode = function(label, idKey, idVal, properties, callback) {
var k, query, v;
query = "MERGE (node:" + label + " {" + idKey + ":'" + idVal + "'}) ";
if (properties != null) {
query += " ON CREATE SET ";
for (k in properties) {
v = properties[k];
if (typeof v === 'number') {
query += " node." + k + "=" + v + ",";
} else {
query += " node." + k + "='" + v + "',";
}
}
query = query.slice(0, -1);
query += " ON MATCH SET ";
for (k in properties) {
v = properties[k];
if (typeof v === 'number') {
query += " node." + k + "=" + v + ",";
} else {
query += " node." + k + "='" + v + "',";
}
}
query = query.slice(0, -1);
}
query += " RETURN node";
return neodb.query(query, function(err, saved) {
return callback(err, saved);
});
};
createRelationship = function(from, to, type, callback) {
var query;
query = "MATCH (from:" + from.type + " { " + from.idKey + ":'" + from.idVal + "' }),(to:" + to.type + " { " + to.idKey + ":'" + to.idVal + "' }) ";
query += "MERGE (from)-[r:" + type + "]->(to) RETURN r";
return neodb.query(query, function(err, saved) {
return callback(null, saved);
});
};
init();
}).call(this);