-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (79 loc) · 2.06 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
'use strict';
var express = require('express');
var config = require('config');
var app = express();
var MAX_TWEETS_NB = 50;
app.configure(function () {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.enable('trust proxy');
});
/**
* ROUTES
*/
app.get('/', function (req, res) {
res.send('Everything is fine.');
});
/**
* LET'S GO
*/
var server = app.listen(3000);
console.log('Listening on port 3000');
/**
* SOCKET.IO
*/
var io = require('socket.io').listen(server);
io.configure(function () {
io.set('log level', 1);
});
io.sockets.on('connection', function (socket) {
socket.emit('tweets', tweets);
});
/**
* TWITTER
*/
var Twit = require('twit');
var t = new Twit({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token: config.access_token_key,
access_token_secret: config.access_token_secret
});
var tweets;
initSearch(function (statuses) {
tweets = statuses;
});
function initSearch(callback) {
//var search = 'coupure+courant OR coupure+courrant OR coupure+electricite';
var search = 'lady gaga';
t.get('search/tweets', { q: search, result_type: 'recent', count: MAX_TWEETS_NB }, function (err, reply) {
if (err) return console.log(err);
var statuses = [];
reply.statuses.forEach(function (status) {
statuses.push(formatTweet(status));
});
callback(statuses);
});
}
// var track = 'coupure courant,coupure courrant,coupure lectricit';
// var stream = t.stream('statuses/filter', { track: track });
// stream.on('tweet', function (tweet) {
// tweet = formatTweet(tweet);
// tweets = [tweet].concat(tweets).slice(0, MAX_TWEETS_NB);
// io.sockets.emit('tweet', tweet);
// });
function formatTweet(tweet) {
if (tweet.entities.media) {
console.log(tweet.entities.media[0].sizes);
}
return {
id: tweet.id_str,
text: tweet.text,
created_at: tweet.created_at,
user_id: tweet.user.id_str,
user_name: tweet.user.screen_name,
user_img: tweet.user.profile_image_url,
coords: tweet.coordinates
};
}