-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetcrawler.js
412 lines (325 loc) · 13 KB
/
tweetcrawler.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
// This is the file that will hopefully do all of the twitter database work
// Hopefully a successful refactoring of app.js
var async = require('async'),
db = require('./db'),
twitter = require('ntwitter'),
request = require('request'),
config = require('./config/config');
var tweetCrawler = {};
var twit = new twitter({
consumer_key: config.twitter.consumer_key,
consumer_secret: config.twitter.consumer_secret,
access_token_key: config.twitter.access_token_key,
access_token_secret: config.twitter.access_token_secret
});
var stats = new db.stat();
var LIMIT_USER_SEARCH = 100;
var times_run = 0;
var MAX_TIMES_RUN = 5;
// Global variables to hold stuff to keep from
// checking database if we already have
var cachedInterests;
var curInterest;
tweetCrawler.run = function() {
console.log('\n\nStarting another tweetCrawler.run()');
times_run++;
async.waterfall([
function getInterestsToCrawl(callback) {
// Reset stats to new
stats = new db.stat();
stats.start_time = new Date().getTime();
if (cachedInterests)
callback(null, cachedInterests);
else
db.interest.find({needToRun: true}, callback)
},
function cacheInterests(interests, callback) {
cachedInterests = interests;
callback(null, interests);
},
function getNextInterestToDo(interests, callback) {
// Get first interest, can later be done by priority (e.g. date last updated)
curInterest = interests.splice(0,1);
if (curInterest.length)
callback(null, curInterest[0])
// Start over if there are no more interests
else
callback("Couldn't find another interest to run.");
},
function getFollowerIDs(interest, callback) {
stats.interest = interest._id;
async.map(interest.twitter_names, function(twitter_name, callback) {
twit.get('/followers/ids.json', {screen_name: twitter_name}, callback);
}, callback);
},
function combineFollowerIds(followers, callback) {
if (!followers.length) callback("Didn't get any followers from twitter.");
async.concat(followers, function(follower, callback) {
callback(null, follower.ids);
}, callback);
},
function getCachedFollowersFromDB(ids, callback) {
stats.retrieved_followers = ids.length;
// Find all users that we already have stored
db.user.find().where('twitter_id').in(ids).populate('location').exec(function(err, users) {
callback(err, ids, users)
});
},
function getListOfCachedIds(ids, cachedUsers, callback) {
stats.cached_users = cachedUsers.length;
async.map(cachedUsers, function(user, callback) {
callback(null, user.twitter_id);
}, function(err, cachedIds){
callback(err, ids, cachedUsers, cachedIds);
})
},
function getUncachedIds(ids, cachedUsers, cachedIds, callback) {
// Loop through all ids, if they aren't in cached, add to uncached list
async.filter(ids, function(id, callback) {
var index = cachedIds.indexOf(id)
callback(index == -1)
}, function(uncachedIds) {
callback(null, uncachedIds, cachedUsers);
});
},
function determineRawUncachedUsers(uncachedIds, cachedUsers, callback) {
// Chunk the ids into groups of 100 for twitter api limit
var i, j, chunk = LIMIT_USER_SEARCH;
var chunkedIds = [];
for (i=0,j=uncachedIds.length; i<j; i+=chunk) {
chunkedIds.push(uncachedIds.slice(i,i+chunk));
}
// Call the twitter api in chunks of 100 then combine results to be handled
async.concat(chunkedIds, function(ids, callback) {
twit.get('/users/lookup.json', {user_id: ids.join()}, callback);
}, function(err, rawUsers) {
if (err)
console.log('Error from twitter API /users/lookup');
// Remove users without a location given
async.filter(rawUsers, function(rawUser, callback) {
callback(rawUser.location);
}, function(rawUsers) {
callback(err, rawUsers, cachedUsers);
});
});
},
function getRawUncachedUsers(rawUsers, cachedUsers, callback) {
stats.new_uncached_users = rawUsers.length;
async.map(rawUsers, function(rawUser, callback) {
saveRawUser(rawUser, callback);
}, function(err, newUsers) {
// Combine all users
var users = newUsers.concat(cachedUsers);
callback(err, users);
});
},
function remove_previously_counted_users(users, callback) {
stats.before_remove_prev_counted = users.length;
async.reject(users,function(user, callback) {
callback(user.interests.indexOf(curInterest[0]._id)!=-1);
}, function(users) {
callback(null, users);
});
},
function update_location_country_counts(users, callback) {
stats.after_remove_prev_counted = users.length;
async.forEachSeries(users, function(user, callback) {
if (user.location.country) { //wait, how did we get this far if the user doesn't have a country? #bug
db.interest_locations.findOne({ type: 'country', location: user.location.country, interest: curInterest[0]._id}, function (err, row) {
if (row) {
row.count++;
row.save(function(err) {
callback(err);
});
} else {
var new_interest_location_row = new db.interest_locations({type: 'country', location_short: user.location.country_short, location: user.location.country, interest: curInterest[0]._id});
new_interest_location_row.save(function(err) {
callback(err);
});
}
});
} else {
callback(null);
}
}, function(err){
callback(err, users);
});
},
function update_location_state_counts(users, callback) {
async.forEachSeries(users, function(user, callback) {
if (user.location.state) {
db.interest_locations.findOne({ type: 'state', location_parent: user.location.country_short, location: user.location.state, interest: curInterest[0]._id}, function (err, row) {
if (row) {
row.count++;
row.save(function(err) {
callback(err);
});
} else {
var new_interest_location_row = new db.interest_locations({type: 'state', location_parent: user.location.country_short, location: user.location.state, interest: curInterest[0]._id});
new_interest_location_row.save(function(err) {
callback(err);
});
}
});
} else {
callback(null);
}
}, function(err){
callback(err, users);
});
},
function update_location_city_counts(users, callback) {
async.forEachSeries(users, function(user, callback) {
if (user.location.city) {
if (user.location.country_short == 'US') {
var location_parent = user.location.country_short + '-' + user.location.state_short
} else {
var location_parent = user.location.country_short
}
db.interest_locations.findOne({ type: 'city', location: user.location.city, location_parent: location_parent, interest: curInterest[0]._id}, function (err, row) {
if (row) {
row.count++;
row.save(function(err) {
callback(err);
});
} else {
var new_interest_location_row = new db.interest_locations({type: 'city', location_parent: location_parent, location: user.location.city, interest: curInterest[0]._id});
new_interest_location_row.save(function(err) {
callback(err);
});
}
});
} else {
callback(null);
}
}, function(err){
callback(err, users);
});
},
function convert_users_to_uids(users, callback) {//There should be a better way to do this, such as just pass in the array of users to the db call, but I can't find out how to do this.
//console.log('LOGGING ALL USERS:');
//console.log(users);
async.map(users, function(user, callback) {
callback(null, user._id);
}, function(err, uids){
callback(err, uids);
});
}, function update_users_with_interest(uids, callback) {
db.user.update({ _id: { $in: uids }}, { $addToSet: { interests: curInterest[0]._id }}, {multi: true}, function(err){ //$addToSet will only add the interest id if it is not already in the array
callback(err);
});
}, function update_interest_need_to_run(callback) {
db.interest.update({_id: curInterest[0]._id}, {needToRun: false}, function(err) {
callback(err);
});
}, function(callback) {
stats.endTime = new Date();
console.log(stats);
//send stat object to separate app
console.log('Done with an interest!');
callback();
}
],
// Last error handling function
// Log error then restart
function(err, result) {
// save stats
stats.end_time = new Date().getTime();
stats.time_to_run = (stats.end_time - stats.start_time) / 1000;
if (err){
var errIsString = (typeof(err) === 'string')
console.log(err);
if (errIsString) {
stats.error_message = err;
stats.error_function = result;
}
// print stack trace if it's not a string, thus a real error
else {
stats.error_message = err.name + ': ' + err.message;
stats.error_function = result;
console.log(err.stack);
console.trace();
}
}
stats.save();
if (times_run < MAX_TIMES_RUN)
tweetCrawler.run();
});
};
// HELPER FUNCTIONS:
// Completely handles a raw user from twitter:
// 1. Saves its formatted location to db (checking if cached first)
// 2. Saves user to database, then calls callback with (err, newUser)
function saveRawUser(rawUser, callback) {
async.waterfall([
// Check database for location
function checkDBForLoc(callback) {
db.location.findOne({raw: rawUser.location}, function(err, loc) {
callback(err, loc, rawUser);
});
},
function getValidLoc(loc, rawUser, callback) {
// Found so continue
if (loc) callback(null, loc, rawUser);
// Didn't find, so create from raw and store in db
else {
getLocationFromRaw(rawUser.location, function(err, loc) {
db.location.create(loc, function(err, loc) {
callback(err, loc, rawUser);
});
});
}
},
function createNewUser(loc, rawUser, callback) {
db.user.create({twitter_id: rawUser.id, location: loc}, function(err, newUser) {
// newUser has lost his location ref here, so re add it
// This is a hacky solution that adds it by populating from database
// We shouldn't need to go back to database, because location is just loc from above
// However, if you do `newUser.location = loc` it only assigns the objectId.
// So this is a temporary workaround
db.user.findOne(newUser).populate('location').exec(callback);
});
}
],
function doCallback(err, user) {
// Do the main callback with result as the user
callback(err, user);
})
};
//we can use the google maps geolocation api to convert location strings to objects with city, state, and country strings
//this example functions takes an address and writes an object with the state and city to the console
function getLocationFromRaw(address, callback) {
request({url: 'http://maps.googleapis.com/maps/api/geocode/json', qs: {address:address, sensor: false}}, function (error, response, body) {
body = JSON.parse(body);
// If google has throttled us, try again in two seconds
if (body.status == 'OVER_QUERY_LIMIT') {
setTimeout(function() {
getLocationFromRaw(address, callback)
}, 2000);
}
else {
var location = {raw: address};
if (!error && response.statusCode == 200) {
if (body.results.length) {
var address_components = body.results[0].address_components
if (address_components){
for (var i=0;i<address_components.length;i++) {
if (address_components[i].types.indexOf('locality') != -1) {
location.city = address_components[i].long_name;
} else if (address_components[i].types.indexOf('administrative_area_level_1') != -1) {
location.state = address_components[i].long_name;
location.state_short = address_components[i].short_name
} else if (address_components[i].types.indexOf('country') != -1) {
location.country = address_components[i].long_name;
location.country_short = address_components[i].short_name
}
}
}
}
}
callback(null, location);
}
})
}
// Export!
module.exports = tweetCrawler;