This repository has been archived by the owner on Jun 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
219 lines (201 loc) · 6.66 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
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
// Third-party modules
var express = require('express');
var app = express();
var request = require('request');
var WebSocket = require('ws');
var moment = require('moment');
var pg = require('pg');
// Middleware and Express settings
app.disable('x-powered-by'); // Remove Express's HTTP header
// Use gzip compression on requests:
// http://www.senchalabs.org/connect/compress.html
app.use(require('compression')());
var entries = [];
app.get('/', function (req, res) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.json(entries);
});
var server = app.listen(process.env.PORT || 8001, function () {
console.log('App listening on port %s', server.address().port);
});
var buttonURL = 'http://www.reddit.com/r/thebutton';
var flairClass = function (seconds) {
if (seconds > 51) {
return 'flair-press-6';
}
if (seconds > 41) {
return 'flair-press-5';
}
if (seconds > 31) {
return 'flair-press-4';
}
if (seconds > 21) {
return 'flair-press-3';
}
if (seconds > 11) {
return 'flair-press-2';
}
return 'flair-press-1';
};
var saveEntries = function (callback) {
pg.connect(process.env.DATABASE_URL, function (err, client, done) {
if (err) {
console.error('Save DB connection error:', err);
if (callback) {
callback(err);
}
return;
}
client.query(
'UPDATE entry_saves SET data = $1 WHERE id = 1;',
[JSON.stringify(entries)],
function (err) {
done();
if (err) {
console.error('Error saving entries to DB.', err);
} else {
console.log('Saved ' + entries.length + ' entries to DB.');
}
if (callback) {
callback(err);
}
}
);
});
};
var addTime = function (seconds, clicks) {
entries = entries.slice((-1 * process.env.MAX_ENTRIES || -1e3) + 1).concat({
seconds: seconds,
time: moment().valueOf(),
color: flairClass(seconds),
clicks: clicks
});
saveEntries();
};
var setupWebSocket = function (websocketUrl) {
var currentParticipants;
var previousSecondsLeft;
var previousParticipants;
console.log('Setting up WebSocket...');
var socket = new WebSocket(websocketUrl);
socket.on('close', function () {
console.log('WebSocket closed. Searching for a new URL...');
setTimeout(findWebSocket, 0);
});
socket.on('message', function (data) {
/* jshint camelcase: false */
// disabling camelcase since reddit uses underscore style here
/*
sample tick data:
{
"type": "ticking",
"payload": {
"participants_text": "608,802",
"tick_mac": "50e7a9fd2e4c8feae6851884f91d65908cceb06b",
"seconds_left": 60.0,
"now_str": "2015-04-06-04-08-07"
}
}
*/
var packet = JSON.parse(data);
if (packet.type !== 'ticking') {
return;
}
var tick = packet.payload;
currentParticipants = parseInt(
tick.participants_text.replace(/,/g, ''),
10
);
if (previousParticipants &&
previousParticipants < currentParticipants) {
// the second argument calculates how many people
// clicked this time. multiple clicks apparently all count for
// the same number.
addTime(
previousSecondsLeft,
(currentParticipants - previousParticipants)
);
}
previousSecondsLeft = tick.seconds_left;
previousParticipants = currentParticipants;
});
socket.on('open', function () {
console.log('Listening to the WebSocket messages.');
});
};
var findWebSocket = function () {
console.log('Finding WebSocket URL...');
request(buttonURL, function (error, response, body) {
if (error || response.statusCode !== 200) {
console.log('Reddit server error. Trying again in five seconds.');
setTimeout(findWebSocket, 5e3);
}
var websocketUrlRegex = /"(wss:\/\/[^"]+)/g;
var matches = websocketUrlRegex.exec(body);
if (matches && matches[1]) {
console.log('WebSocket URL found: ', matches[1]);
setupWebSocket(matches[1]);
} else {
console.log('No WebSocket URL found in Reddit response. ' +
'Trying again in five seconds.');
setTimeout(findWebSocket, 5e3);
}
});
};
pg.connect(process.env.DATABASE_URL, function (err, client, done) {
if (err) {
console.error('Statup DB connection error:', err);
process.kill(process.pid, 'SIGTERM');
return;
}
client.query(
'SELECT data FROM entry_saves WHERE id = 1;',
function (err, result) {
if (err || !result.rows.length) {
if (err) {
console.error('Query error:', err);
} else {
console.log('No rows of data found.');
}
if (!result.rows.length) {
client.query(
'INSERT INTO entry_saves (id, data) SELECT 1, $1 ' +
'WHERE NOT EXISTS ' +
'(SELECT 1 FROM entry_saves WHERE id=1);',
['[]'],
function () {
console.log('Saved empty data to DB.');
}
);
}
done(client);
findWebSocket();
return;
}
done();
entries = result.rows[0].data;
console.log('Found ' + entries.length + ' rows of data.');
findWebSocket();
}
);
});
pg.on('error', function () {
console.error('DB connection terminated!');
});
var saveEntriesAndExit = function (code) {
console.log('Shutting down by ' + code + '. Saving ' +
entries.length + ' rows to DB.');
saveEntries(function(err) {
if (err) {
console.error('Error saving on shutdown:', err);
} else {
console.log('Data saved to DB.');
}
pg.end();
process.kill(process.pid, code);
});
};
process.once('SIGTERM', function () { saveEntriesAndExit('SIGTERM'); });
process.once('SIGUSR2', function () { saveEntriesAndExit('SIGUSR2'); });
process.once('SIGINT', function () { saveEntriesAndExit('SIGINT'); });