-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
81 lines (65 loc) · 2.25 KB
/
server.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
require('node-jsx').install({extension: '.jsx'});
require("babel/register");
var koa = require('koa'),
Router = require('koa-router'),
React = require('react'),
serve = require('koa-static'),
mount = require('koa-mount'),
fs = require('fs'),
thunkify = require('thunkify-wrap'),
favicon = require('koa-favicon'),
compress = require("koa-compress"),
Twit = require("twit");
var T = new Twit({
consumer_key: 'XIa0eK7I6GwG39LQiJxhtgXip',
consumer_secret: 'AvymuinH9aQtkqewS05CTipo9Iw6tTUjUffVTsjSjI98V1fiaf',
access_token: '130719275-m5rCvuZKae859YXiKYrwrJqJ9FeDT3gS0KGl5hoG',
access_token_secret: '5nXNkesuj8oDZphQzQtIyboDyreSMHONAPzEEFmibRu0R'
});
var stream = T.stream('statuses/filter', {track: 'javascript'});
var STATIC_FILES_MAP = {};
var SERVE_OPTIONS = {maxAge: 365 * 24 * 60 * 60};
//create our app
var app = koa();
app.use(favicon(__dirname + '/images/favicon.ico'));
//mount our static middleware
app.use(mount('/dist', serve(__dirname + '/dist', {defer: true})));
//render our index.html
var views = require("co-views");
var render = views("views", {map: {html: 'swig'}});
app.use(mount("/", function *( next ) {
//ignore api and static routes
if ( this.path.startsWith("/dist") ) {
return yield next;
}
this.body = yield render("index", {name: "test"});
}));
var server = require('http').Server(app.callback());
var io = require('socket.io')(server);
var isGoodTweet = function( tweet ) {
return !tweet.retweeted;
}
var weightTheTweet = function(tweet){
var points = 0;
points = parseInt(tweet.user.followers_count) + parseInt(tweet.retweet_count) + parseInt(tweet.favorite_count);
if(tweet.user.verified){
points += 5000;
}
return points;
};
io.on('connection', function(socket){
console.log('user connected');
stream.on('tweet', function(tweet) {
if (isGoodTweet(tweet)) {
socket.emit('tweet', {
text: tweet.text,
points: weightTheTweet(tweet),
name: tweet.user.name,
url: "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str
});
}
});
});
//start the app
var port = process.env.PORT || 3000;
server.listen(port);