-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (152 loc) · 4.61 KB
/
index.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
var async = require("async");
var fs = require("fs");
var shell = require("shelljs");
var Twitter = require("twitter");
var auth = require("./auth.json");
var distill = require("./distill");
var db = require("./db");
var client = new Twitter({
consumer_key: auth.twitter.consumerKey,
consumer_secret: auth.twitter.consumerSecret,
access_token_key: auth.twitter.accessKey,
access_token_secret: auth.twitter.accessSecret
});
var publish = "grunt publish:live";
var follow = [
"SJGTimes",
"SusanKelleher",
"Jim_Brunner",
"pgcornwell",
"katherinelong",
"mikelindblom",
"ramondompor",
"mlbaruchman",
"ErikLacitis",
"ErikaJSchultz",
"deanrutz",
"bettinahansen",
"SeaTimesFotoKen",
"stevemiletich"
];
var dumpDB = function() {
console.log("Dumping database to file system");
pending = false;
db.getTweets(function(result) {
if (pending) {
console.log("got more results, re-exporting");
return dumpDB();
}
json = JSON.stringify(result, null, 2);
if (!shell.test("-e", "data")) shell.mkdir("data");
fs.writeFileSync("build/tweets.json", json);
console.log("Completed file dump, building");
shell.exec(publish, function() {
inProgress = false;
console.log("Completed build");
//check again for anything that came in during publish
if (pending) dumpDB();
});
})
};
var today = new Date();
var rejectTweet = function(t) {
if (t.tweet[0] == "@") return true; //skip direct replies
if (!t || follow.indexOf(t.handle) == -1) return true; //skip retweets
if (t.tweet.indexOf("RT") == 0) return true; //skip retweets
if (t.date.getMonth() < today.getMonth() || t.date.getFullYear() < today.getFullYear()) return true; //skip very old tweets
//CNC tweets
if (t.handle.toLowerCase() == "thomaswilburn") {
if (t.tweet.toLowerCase().indexOf("killserver") > -1) {
process.exit();
}
return true;
}
}
var inProgress = false;
var pending = false;
var scheduleDump = function() {
if (inProgress) {
return pending = true;
}
inProgress = setTimeout(dumpDB, 1000);
}
if (process.argv.indexOf("--dump-only") > -1) {
dumpDB();
} else async.waterfall([
db.init,
function(callback) {
var query = "SELECT * FROM users WHERE name IN (" + follow.map(JSON.stringify).join(",") + ");";
db.all(query, function(err, result) {
var identified = {};
result.forEach(function(row) {
identified[row.name] = row.id;
});
var unidentified = follow.filter(function(name) { return !(name in identified) });
var ids = result.map(function(d) { return d.id });
if (!unidentified.length) {
return callback(null, ids);
}
//lookup remaining
client.get("users/lookup", {
screen_name: unidentified.join(",")
}, function(err, response) {
async.map(response, function(item, c) {
db.insertUser(item.screen_name, item.id_str, c);
}, function(err, mapped) {
if (err) return console.error(err);
callback(null, mapped.concat(ids));
});
});
});
},
function(ids, callback) {
//backfill tweets
async.each(ids, function(id, c) {
client.get("statuses/user_timeline", {
user_id: id,
count: 100,
tweet_mode: "extended"
}, function(err, data) {
if (err) return c(err);
async.each(data, function(tweet, done) {
var t = distill(tweet);
if (rejectTweet(t)) return done();
// console.log(t);
db.get("SELECT * FROM tweets WHERE id = ?", [t.id], function(err, exists) {
if (exists) return done();
db.insertTweet(t, done);
});
}, c)
})
}, function(err) {
//pass IDs through
callback(null, ids);
})
},
function(ids) {
//dump only after backfilling
dumpDB();
client.stream("statuses/filter", {
// locations: "-122.43,47.48,-122.22,47.73"
// track: "Seattle"
follow: ids.join(","),
tweet_mode: "extended"
}, function(stream) {
stream.on("data", function(tweet) {
if (tweet.delete) {
console.log("====DELETE===", tweet.delete.status);
return db.deleteTweet(tweet.delete.status.id_str, scheduleDump);
}
// console.log("====ORIGINAL=====\n", tweet);
var t = distill(tweet);
if (rejectTweet(t)) return;
console.log("====UPDATE=====\n", t);
db.insertTweet(t, scheduleDump);
});
stream.on("error", console.error.bind(console));
stream.on("end", function(ended) {
console.error(ended.statusMessage);
});
});
}
], console.log.bind(console));