-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (85 loc) · 2.68 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
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 3000));
app.set('consumer_key', (process.env.consumer_key || require('./config.json').consumer_key));
app.set('consumer_secret', (process.env.consumer_secret || require('./config.json').consumer_secret));
app.set('access_token', (process.env.access_token || require('./config.json').access_token));
app.set('access_token_secret', (process.env.access_token_secret || require('./config.json').access_token_secret));
var server = app.listen(app.get('port'), function () {
console.log('App listening on port ' + app.get('port'));
});
var io = require('socket.io')(server);
var Twit = new require('twit')({
consumer_key: app.get('consumer_key'),
consumer_secret: app.get('consumer_secret'),
access_token: app.get('access_token'),
access_token_secret: app.get('access_token_secret')
});
var hashtag = '', stream, users;
/************************* API BEGINS ***********************/
//track a hashtag
var track = express.Router();
track.post('/:hashtag', function(req, res) {
// stop tracking a hashtag if any
if (hashtag !== '') {
handleStopRequest();
}
hashtag = req.params.hashtag;
handleStreamRequest();
res
.set('Access-Control-Allow-Origin', '*')
.status(200)
.end();
});
app.use('/twitterstream/track', track);
//untrack a hashtag
var untrack = express.Router();
untrack.post('/:hashtag', function(req, res) {
handleStopRequest();
res
.set('Access-Control-Allow-Origin', '*')
.status(200)
.end();
});
app.use('/twitterstream/untrack', untrack);
/************************** API ENDS ************************/
// SocketIO connection & disconect event
users = 0;
io.on('connection', function (socket) {
console.log('User connected!! Users: ', ++users);
socket.on('disconnect', function () {
console.log('User desconnected!! Users: ', --users);
});
});
/**
* handle the track of a hashtag
*/
function handleStreamRequest() {
console.log('Track request: #' + hashtag);
stream = Twit.stream('statuses/filter', { track: '#' + hashtag });
// connect event
stream.on('connect', function () {
console.log('Successfully connected to Twitter API for: #' + hashtag);
});
// disconnect event
stream.on('disconnect', function () {
console.log('Disconnected');
});
// error event
stream.on('error', function (err) {
console.log(err);
});
// tweet event
stream.on('tweet', function (tweet) {
// send event to the client throught SocketIO
io.emit('tweet', {
hashtag: hashtag,
tweet: tweet
});
});
}
function handleStopRequest() {
console.log('Untrack request: #' + hashtag);
stream.stop();
hashtag = '';
}