-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter-display.js
More file actions
69 lines (60 loc) · 1.89 KB
/
twitter-display.js
File metadata and controls
69 lines (60 loc) · 1.89 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
var express = require('express');
var Twitter = require('twitter');
var Tweet = require('./Tweet.js');
function TwitterDisplay (options, hashtag, app, server){
this.params = ['text','user','entities', 'extended_entities'];
this.twitter = new Twitter(options);
this.app = app;
this.listeTweets = [];
this.hashtag = hashtag;
app.use("/twitter-display", express.static(__dirname + '/dist'));
var self = this;
this.twitter.get('/search/tweets', {q: this.hashtag}, function(error, tweets, response){
for(var i = 0; i < tweets.statuses.length; i++)
{
var thetweet = tweets.statuses[i];
if(thetweet.entities)
{
self.twitter.get('statuses/show', {id: thetweet.id_str}, function(error, tweets, response){
var tweet = new Tweet(tweets);
var toSend = tweet.toSend(self.params);
self.listeTweets.push(toSend);
});
}
else
{
var tweet = new Tweet(thetweet);
var toSend = tweet.toSend(self.params);
self.listeTweets.push(toSend);
}
}
});
var io = require('socket.io').listen(server);
this.twitter.stream('statuses/filter', {track: this.hashtag}, function(stream) {
stream.on('data', function(tweet) {
if(tweet.entities.media)
{
self.twitter.get('statuses/show', {id: tweet.id_str}, function(error, tweets, response){
var tweet = new Tweet(tweets);
var toSend = tweet.toSend(self.params);
self.listeTweets.push(toSend);
io.sockets.emit('newtweet', toSend);
});
}
else
{
var aTweet = new Tweet(tweet);
var toSend = aTweet.toSend(self.params);
self.listeTweets.push(toSend);
io.sockets.emit('newtweet', toSend);
}
});
stream.on('error', function(error) {
throw error;
});
});
app.get('/tweetsdisplay',function(req,res){
res.send({tweets: self.listeTweets});
})
}
module.exports = TwitterDisplay;