This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (62 loc) · 1.75 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
try {
var Twit = require('twit');
var express = require('express.io');
} catch(err) {
console.error("No has instalado las dependencias necesarias!");
console.log("Ejecuta `npm install` antes de correr este programa.");
exit(1);
}
var app = express();
var fs = require('fs');
var os = require('os');
app.use("/", express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.http().io();
if (!fs.existsSync(__dirname+'/config.json')) {
console.error('Necesitas crear un archivo config.json en este carpeta antes de correr este programa');
exit(1);
}
var config = require(__dirname+'/config.json');
var T = new Twit(config.twitter);
var stream = T.stream('statuses/filter', {track: config.hashtags.join(',')});
var parsed = function(tweet) {
media = null;
if (tweet.entities.media) {
tweet.entities.media.forEach(function(el, index){
if (!media && el.type == 'photo') {
media = el.media_url;
}
});
}
var text = tweet.text;
if (tweet.retweeted_status) {
var st = tweet.retweeted_status;
text = "RT @"+st.user.screen_name+": "+st.text;
}
text = text.replace(/\#([\wáéíóúñü]+)/ig, '<strong>#$1</strong>');
return {
user: {
name: tweet.user.screen_name,
pic: tweet.user.profile_image_url
},
text: text,
media: media
};
};
stream.on('tweet', function(tweet){
var t = parsed(tweet);
app.io.broadcast('tweet', t);
});
app.get('/', function(req,res){
res.render('index');
});
app.get('/control', function(req,res){
res.render('control');
});
app.io.route('switch', function(req) {
req.io.broadcast('go-'+req.data.screen, req.data.data);
});
config.port = config.port || 6547;
app.listen(config.port);
console.log("Corriendo en http://"+os.hostname()+":"+config.port);