-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagTweets.js
69 lines (61 loc) · 1.8 KB
/
tagTweets.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
// **************************
// ****** DEPENDENCIES ******
// **************************
var proc = require('./util/process');
var _ = require('lodash');
var database = require('./api/database');
// **************************
// ******** PROGRAM ********
// **************************
var getAllTweets = function(callback) {
database.findTweets({}, function(err, tweets) {
if (err) { proc.bail('Failed to find tweets', err); }
callback(tweets);
});
};
database.runWithConn(function() {
getAllTweets(function(tweets) {
var queries = ['#foodpoisoning', '#stomachache', '"food poison"', '"food poisoning"', 'stomach', 'vomit', 'puke', 'diarrhea', '"the runs"'];
_.each(tweets, function(tweet) {
var newTags = {};
_.each(queries, function(query) {
if (tweet.text.toLowerCase().indexOf(query.replace('"', '')) != -1) {
newTags[query] = true;
}
});
var diff = true;
if (!tweet.tags || tweet.tags.length == 0) {
diff = !_.isEmpty(newTags);
}
else {
var allTagsInNew = _.every(tweet.tags, function(tag) {
return newTags[tag];
});
var allNewInTags = _.difference(_.keys(newTags), tweet.tags).length == 0;
diff = !allTagsInNew || !allNewInTags;
}
if (diff) {
tweet.tags = _.keys(newTags);
database.updateTweet(tweet['_id'], {tags:tweet.tags}, function(err, updated) {
if (err) {
bail('Failed to update tweet', err);
}
});
}
});
var countByTag = {'NO TAGS': 0};
_.each(tweets, function(tweet) {
if (tweet.tags && tweet.tags.length > 0) {
_.each(tweet.tags, function(tag) {
countByTag[tag] = countByTag[tag] || 0;
countByTag[tag] = countByTag[tag] + 1;
});
}
else {
countByTag['NO TAGS'] = countByTag['NO TAGS'] + 1;
}
});
console.log(countByTag);
proc.done('DONE');
});
});