forked from StartupWeekendBrest/TweetWall
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (83 loc) · 2.36 KB
/
server.js
File metadata and controls
104 lines (83 loc) · 2.36 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
var Twitter = require('node-tweet-stream'),
printit = require('printit'),
morgan = require('morgan'),
_ = require('lodash'),
express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io')(server),
State = require('./state.js'),
utils = require('./utils.js');
// Load config
var config = require('./config.json');
// Modules init
var t = new Twitter({
consumer_key: config.twitter.consumer_key,
consumer_secret: config.twitter.consumer_secret,
token: config.twitter.token,
token_secret: config.twitter.token_secret
}),
log = printit({
prefix: 'TweetWall',
date: true
});
// Create a state object which will manage the battle's history
var state = new State('./back.json');
// Process battle config
config.battle = _.map(config.battle, function (hash)
{
return hash.toLowerCase();
});
_.forEach(config.battle, function (hashtag)
{
if(state.isEmpty(hashtag)) {
// If we counted no tweet for a hashtag, retrieve them with the Twitter API
utils.countPreviousTweets(hashtag, function(tweets) {
// Once the tweets have been retrieved, notify the user to update
// its counter
state.updateCounter(hashtag, tweets.length);
io.to('clients').emit('battle', state.battle);
})
}
t.track(hashtag);
});
// Handlers
io.on('connection', function(socket) {
utils.socketHandler(socket, state);
});
t.on('tweet', function(tweet) {
utils.tweetHandler(tweet, function(hashtag, processedTweet) {
state.updateCounter(hashtag, 1);
// Notify the user
io.to(hashtag).emit('tweet', processedTweet);
io.to('clients').emit('battle', state.battle);
});
});
// Start saving routines
state.autoSaveCurrentState();
state.autoRoll(function(battle) {
io.to('clients').emit('roll', battle);
});
// Server Setup
var port = process.env.PORT || 3000;
server.listen(port, function () {
log.info('Server listening at port ' + port);
});
//app.use(morgan('dev'));
app.use(express.static(__dirname + '/public'));
app.use(function logErrors (err, req, res, next) {
console.error(err.stack);
next(err);
});
app.use(function clientErrorHandler (err, req, res, next) {
if (req.xhr) {
res.status(500).send({error: 'Something blew up!'});
}
else {
next(err);
}
});
app.use(function errorHandler (err, req, res, next) {
res.status(500);
res.render('error', {error: err});
});