-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchInterests.js
64 lines (62 loc) · 2.02 KB
/
searchInterests.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
var async = require('async'),
db = require('./db'),
twitter = require('ntwitter'),
program = require('commander'),
config = require('./config/config');
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
});
function list(val) {
return val.split(',')
}
program
.option('-i, --interests <items>', 'Comma separated list of interests', list)
.parse(process.argv)
if (!program.interests) {
console.log('You must enter interests to search for by using command line arguments (-i or --interests then a comma separated list of interests)')
process.exit();
}
var interests = program.interests
var currentInterest = 0
var findTwitterUsers = function() {
async.waterfall([
function(callback){
callback(null, interests[currentInterest]);
},
function(interest, callback) {
twit.get('/users/search.json', {q: interest}, function(err, data) {
callback(err, data)
});
}, function(users, callback) {
async.map(users.splice(0,20), function(user, callback) {
callback(null, user.screen_name);
}, function(err, screen_names){
callback(null, screen_names)
})
}, function(screen_names, callback) {
console.log("Top twitter users for the interest '" + interests[currentInterest]+"'");
console.log(screen_names);
callback(null, screen_names)
}, function (screen_names, callback) {
db.interest.findOne({ name: interests[currentInterest]}, function (err, interest) {
if (!interest) {
var new_interest = new db.interest({name: interests[currentInterest], twitter_names: screen_names, needToRun:true});
new_interest.save(callback);
} else {
callback(err);
}
});
}
], function(err) {
if (err) console.log(err)
currentInterest++;
if (currentInterest < interests.length) {
findTwitterUsers()
} else
process.exit();
});
}
findTwitterUsers();