-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (66 loc) · 2.18 KB
/
app.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
'use strict';
var _ = require('lodash');
var Twitter = require('twitter');
var countries = require('./countries.json');
var app = require('express')();
var redisConfig = {
host: '192.168.59.103',
port: 6379
};
var url = require('url').parse(process.env.REDISCLOUD_URL || '');
if(url.hostname !== null) {
redisConfig = {
host: url.hostname,
port: url.port
};
}
var redis = require('redis').createClient(redisConfig.port, redisConfig.host);
if(url.hostname !== null) {
redis.auth(url.auth.split(':')[1]);
}
var twitter = new Twitter({
consumer_key: process.env.twitter_consumer_key || require('./twitter.json')[0],
consumer_secret: process.env.twitter_consumer_secret || require('./twitter.json')[1],
access_token_key: process.env.twitter_access_token_key || require('./twitter.json')[2],
access_token_secret: process.env.twitter_access_token_secret || require('./twitter.json')[3]
});
var regExp = new RegExp('(?:' + (countries.join('|')) + ')', 'g');
var scanCountries = function(str) {
var matches = [];
var idx = null;
regExp.lastIndex = 0;
while (idx = regExp.exec(str)) {
matches.push(idx[0]);
}
return matches;
};
var args = ['country_score', 1, null];
var foundCountries = [];
twitter.stream('statuses/filter', {track: countries.join(',')}, function(stream) {
stream.on('data', function(tweet) {
foundCountries = _.uniq(scanCountries(tweet.text));
_.forEach(foundCountries, function(val) {
args[2] = val;
redis.zincrby(args, function(err) {
if(err) {
console.log(err);
} else {
redis.publish('stream.score_updates', args[2]);
}
});
// console.log(foundCountries);
});
});
console.log('feeder running');
stream.on('error', function(error) {
throw error;
});
});
// Bind web port for heroku not to crash
app.set('port', (process.env.PORT || 5000));
app.all('/', function(req, res){
res.send('Hello World!');
});
app.listen(app.get('port'), function() {
console.log('Node app is running at localhost:' + app.get('port'));
});