-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsp.js
318 lines (273 loc) · 8.5 KB
/
nsp.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
var $ = require('jquery');
var ntwitter = require('ntwitter');
var flickrnode = require('flickrnode').FlickrAPI;
var redis = require("redis");
var underscore = require("./static/lib/underscore-1.3.3-min");
var config = require('./config.js').config;
var keys = require('./keys.js'); // private
var utils = require('./utils.js');
this.db = null;
this.nTwitterApi = null;
this.twitterStream = null;
// application state
this.data = {
// twitter api
isRunning: false, // twitter stream status
acceptMsg: false, // rate limit
// twitter messages
result_idx: 0,
results: [],
// photos
photo_words_score: 0,
photo_current_word: "",
photo_idx: 0,
photos: [],
};
this.init = function() {
var self = this;
// db client
var db = redis.createClient();
db.on("error", function (err) {
console.error("REDIS ERROR (server): " + err);
});
this.db = db;
// new twitter api
this.nTwitterApi = new ntwitter({
consumer_key: keys.twitter.consumer_key,
consumer_secret: keys.twitter.consumer_secret,
access_token_key: keys.twitter.access_token_key,
access_token_secret: keys.twitter.access_token_secret,
});
// flickr api
// http://www.flickr.com/services/api/
// http://www.flickr.com/services/api/misc.urls.html
this.flickrApi = new flickrnode(keys.flickr.key, keys.flickr.secret);
// kick-off the twitter tracker process
this.initTwitterStream();
// check health of twitter tracker
setInterval(function() {
if (self.data.isRunning !== true) {
console.log("TWITTER SEARCH DIED!");
self.initTwitterStream();
} else {
console.log("twitter tracker still running");
}
}, config.app.CHECK_TWITTER_HEALTH_MS);
setInterval(function() {
self.data.acceptMsg = true;
}, config.app.RATE_LIMIT_MS);
};
this.test = function() {
console.log("test received");
var self = this;
var deferred = new $.Deferred();
this.nTwitterApi.verifyCredentials(function(err, data) {
self.db.publish(config.dbkey.EVENT_STREAM, JSON.stringify(data));
deferred.resolve(data);
});
return deferred.promise();
};
/**
* Handler for a tweet that we want to store in the db for display.
*/
this.handleTweet = function(tweet, dbPrefix) {
var self = this;
// get the interesting words
var words = tweet.text.substr(8).split(/[^a-zA-Z]/);
underscore.each(words, function(word) {
if (word.length > 2) {
word = word.toLowerCase();
self.db.zscore(config.dbkey.WORD_SCORES, word, function(err, res) {
if (err) {
return;
}
if (res > 1000) {
var key = dbPrefix + ":" + tweet.id + ":words";
self.db.zadd(key, res, word);
self.db.expire(key, 36000); // 10 hours
}
});
}
});
// only keep the required fields
var little_user = underscore.pick(tweet.user, 'screen_name');
var little_tweet = underscore.pick(tweet, 'id', 'text', 'created_at');
var my_tweet = $.extend(little_tweet, {user: little_user});
// lpush message - newest <==> oldest
self.db.lpush(dbPrefix, JSON.stringify(my_tweet));
self.db.ltrim(dbPrefix, 0, 99); // keep the leftmost (newest) 100 messages
};
/**
* Start listening to the twitter stream.
*/
this.initTwitterStream = function() {
var self = this;
console.log("Initializing twitter stream");
this.data.isRunning = true;
// https://dev.twitter.com/docs/api/1/post/statuses/filter
this.nTwitterApi.stream('statuses/filter', {track:'I wonder,I think'}, function(stream) {
stream.on('data', function (data) {
if (data.text.match(config.search.TWITTER_STREAM_REGEX)) {
if (data.text.match(config.search.PRIORITY_REGEX)) {
console.log("PRIORITY TWEET! =================================");
console.log("incoming priority msg: ", data.created_at, data.text.substring(0,50));
self.handleTweet(data, config.dbkey.MSG_PRIORITY);
} else if (self.data.acceptMsg === true) {
console.log("incoming msg: ", data.created_at, data.text.substring(0,50));
self.handleTweet(data, config.dbkey.MSG);
}
self.data.acceptMsg = false;
}
});
stream.on('error', function (error) {
// Handle a programming (?) error
console.warn("TWITTER ERROR", error);
self.data.isRunning = false;
});
stream.on('end', function (response) {
// Handle a disconnection
console.warn("TWITTER END");
self.data.isRunning = false;
});
stream.on('destroy', function (response) {
// Handle a 'silent' disconnection from Twitter, no end/error event fired
console.warn("TWITTER DESTROY");
self.data.isRunning = false;
});
self.twitterStream = stream;
});
};
this.restartTwitterStream = function() {
this.data.isRunning = false;
if (this.twitterStream != null) {
this.twitterStream.destroy();
}
this.initTwitterStream();
};
/**
* Manually run a twitter search.
*
* Unused except when requested by the client explicitly.
*/
this.searchTwitter = function() {
var self = this;
console.log("searchTwitter received");
var deferred = new $.Deferred();
var searchString = encodeURIComponent('"I wonder"');
console.log("search for ", searchString);
this.nTwitterApi.search(searchString, {rpp: 100}, function(err, data) {
found = 0;
underscore.chain(data.results)
.filter(function(result) {
// only want tweets that /start with/ I wonder
return result.text.substr(0,8) === "I wonder";
})
// .reverse() // reverse the default order - we want old to new
.each(function(result) {
found++;
self.handleTweet(data, config.dbkey.MSG);
});
console.log("twitter search for I wonder found " + found + " messages.");
// return the current length of the queue
self.db.llen(config.dbkey.MSG, function (err, res) {
deferred.resolve(res);
});
});
return deferred.promise();
};
/**
* Run a flickr search for a tag.
*/
this.searchFlickr = function(tag, score) {
var self = this;
// console.log("search flickr: tag=" + tag + ", score=" + score);
var searchOpts = {
tags: tag,
//sort: "interestingness-desc",
//sort: "date-posted-desc",
sort: "relevance",
};
this.flickrApi.photos.search(searchOpts, function(error, results) {
if (error || !results) {
console.warn("Flickr search for " + tag + " failed.");
return;
}
self.data.photo_words_score = score;
self.data.photo_current_word = tag;
self.data.photo_idx = 0;
self.data.photos = [];
console.log("flickr search for " + tag + " found " + results.photo.length + " photos.");
underscore.each(results.photo, function(result) {
var filename = result.id + "_" + result.secret + ".jpg";
var url = "http://farm" + result.farm +
".staticflickr.com/" + result.server +
"/" + filename;
self.data.photos.push({url: url, filename: filename});
});
});
};
this._nextTwitter = function(dbPrefix) {
var self = this;
var deferred = new $.Deferred();
// rpop message (oldest message)
this.db.rpop(dbPrefix, function(err, res) {
if (res === null) {
// priority queue will usually be empty - this logging adds noise
// console.warn(" - nextTwitter has nothing to show right now");
deferred.reject();
return;
}
var result = JSON.parse(res);
result.words = [];
// look for the scored words
var key = dbPrefix + ":" + result.id + ":words";
self.db.zrevrange(key, 0, -1, "WITHSCORES", function(err, words) {
if (words.length > 2) {
var score = parseInt(words[1]) + parseInt(words[3]);
self.searchFlickr(words[0] + "," + words[2], score);
} else if (words.length > 0) {
var score = parseInt(words[1]);
self.searchFlickr(words[0], score);
} else {
// do nothing
// self.data.photo_current_word = null;
}
while (!underscore.isEmpty(words)) {
var score = words.pop();
var word = words.pop();
var msg = word + " (" + score + ")";
result.words.push(msg);
}
deferred.resolve(result);
});
});
return deferred;
};
this.nextPriorityTwitter = function() { return this._nextTwitter(config.dbkey.MSG_PRIORITY); };
this.nextTwitter = function() { return this._nextTwitter(config.dbkey.MSG); };
/**
* Get the next photo.
*/
this.nextFlickr = function() {
if (this.data.photos.length === 0 || this.data.photo_current_word === null) {
console.warn(" - nextFlickr has nothing to show right now.");
return;
}
// random
var idx = utils.getRandomInt(0, this.data.photos.length - 1);
// in order
//var idx = this.data.photo_idx++;
return {
url: this.data.photos[idx].url,
filename: this.data.photos[idx].filename,
word: this.data.photo_current_word,
};
};
/**
* Get the next panel for display.
*/
this.nextDisplayPosition = function() {
var screen = utils.getRandomInt(0, config.ui.NUM_SCREENS-1) + 1;
var panel = utils.getRandomInt(0, config.ui.NUM_PANELS-1);
return {displayScreen: screen, displayPanel: panel};
};