forked from divakarvenu/Twitterstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (81 loc) · 3.04 KB
/
index.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
var Twit = require('twit');
const express = require('express');
const app = express();
const server = require('http').Server(app);
var io = require('socket.io')(server);
var fs = require('fs');
const config = require('./config.js').config;
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
function formatTweet(tweet) {
let tweetbody = {
'id': tweet.id,
'text': tweet.text,
'userScreenName': "@" + tweet.user.screen_name,
'userImage': tweet.user.profile_image_url_https ? tweet.user.profile_image_url_https : '',
'userDescription': tweet.user.description,
}
try {
if(tweet.entities.media[0].media_url_https) {
tweetbody['image'] = tweet.entities.media[0].media_url_https;
}
if(tweet.entities.media.length) {
tweetbody['images'] = tweet.entities.media.map(val => {
// console.log(val);
let sizes = val.sizes.medium;
if(!sizes) sizes = val.sizes.large;
return {
width: sizes.w,
height: sizes.h,
url: val.media_url_https
};
});
}
} catch(err) { }
return tweetbody;
}
io.on('connection', function(socket) {
T.get('search/tweets', { q: '(#babymetal_fanart OR #babymetalfanart) filter:images -filter:retweets', count: 200 }, function(err, data, response) {
var tweetArray=[];
// console.log('raw tweets', data.statuses);
for (let index = 0; index < data.statuses.length; index++) {
const tweet = data.statuses[index];
// var tweetbody = {
// 'id': tweet.id,
// 'text': tweet.text,
// 'userScreenName': "@" + tweet.user.screen_name,
// 'userImage': tweet.user.profile_image_url_https,
// 'userDescription': tweet.user.description,
// }
// try {
// if(tweet.entities.media[0].media_url_https) {
// tweetbody['image'] = tweet.entities.media[0].media_url_https;
// }
// if(tweet.entities.media.length) {
// tweetbody['images'] = tweet.entities.media.map(val => {
// // console.log(val);
// let sizes = val.sizes.medium;
// if(!sizes) sizes = val.sizes.large;
// return {
// width: sizes.w,
// height: sizes.h,
// url: val.media_url_https
// };
// });
// }
// } catch(err) { }
const tweetbody = formatTweet(tweet);
tweetArray.push(tweetbody);
}
io.emit('allTweet',tweetArray)
})
var stream = T.stream('statuses/filter', { track: ['#babymetal_fanart', '#babymetalfanart'] })
stream.on('tweet', function (tweet) {
io.emit('tweet',{ 'tweet': formatTweet(tweet) });
})
});
var T = new Twit(config);
// listen for requests :)
const listener = server.listen(config.port, function() {
console.log('Your app is listening on port ' + listener.address().port);
});