-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
109 lines (92 loc) · 2.46 KB
/
app.js
File metadata and controls
109 lines (92 loc) · 2.46 KB
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
// module dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var util = require('util');
var twitter = require('twitter');
var twit = new twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
});
var tweets = [];
var currentTweet = "";
var movesHistogram = {
up: 0,
down: 0,
left: 0,
right: 0
};
// all environments
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 1000*60*60*24*14 }));
var server = http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.on('poll', function (data) {
currentTweet.histogram = movesHistogram;
socket.emit('update', currentTweet);
});
});
setInterval(getNextTweet, 2000);
function getNextTweet() {
if (tweets.length > 0) {
currentTweet = tweets.pop();
updateHistogram();
return;
}
twit.search('up OR down OR left OR right', function (data) {
if (!data) return;
tweets = data.statuses.map(function (t) {
var info = findText(t.text);
if (info === false) {
return "false";
}
return {
text: t.text,
user: t.user.screen_name,
dir: info.dir,
start: info.start,
id: t.id_str
};
});
tweets.filter(function (t) {
return t !== "false" && t.text && t.user && t.dir;
});
currentTweet = tweets.pop();
updateHistogram();
});
}
function updateHistogram() {
if (!currentTweet.dir) return;
movesHistogram[currentTweet.dir]++;
}
function findText(text) {
var text = text.toLowerCase();
var dir;
var start;
if (text.indexOf('up') !== -1) {
dir = 'up';
start = text.indexOf('up');
} else if (text.indexOf('down') !== -1) {
dir = 'down';
start = text.indexOf('down');
} else if (text.indexOf('left') !== -1) {
dir = 'left';
start = text.indexOf('left');
} else if (text.indexOf('right') !== -1) {
dir = 'right';
start = text.indexOf('right');
} else {
return false;
}
return {dir: dir, start: start};
}