This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinding-server.js
132 lines (108 loc) · 3.67 KB
/
dinding-server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Node Server [app]
* Twitter Wall
*/
'use strict';
var fs = require('fs');
var express = require('express');
var twitter = require('twitter');
var io = require('socket.io');
var http = require('http');
var config = require(__dirname + '/config.json');
var utils = require(__dirname + '/settings/utils.js');
var rights = require(__dirname + '/settings/rights.js')(config);
var twit = new twitter({
consumer_key: config.twitter.consumer_key,
consumer_secret: config.twitter.consumer_secret,
access_token_key: config.twitter.access_token_key,
access_token_secret: config.twitter.access_token_secret
});
// Init
var app = express();
var server = http.createServer(app);
// Express settings
app
.use(express.static(__dirname + '/public'))
.set('view options', {
layout: 'layout'
})
.set('views', __dirname + '/views')
.set('view engine', 'hbs');
// Routes
app.get('/', function(req, res) {
res.render('index');
});
// ### SOCKET.IO ####
var socket = require('socket.io').listen(server);
// Socket settings
socket.on('connection', function(sock) {
sock.on('beginTransmisson', function() {
// console.log('Ok! Got your request. Will send some data soon.');
// First, send hashtag data
sock.emit('hashtags', config.app.hashtags);
app.twSearch(config.app.hashtags, sock);
});
});
// Functions used by twit
app.twSearch = function(keyword, sock) {
// console.log('## Search started ##', keyword);
for (var i = 0; i < config.app.hashtags.length; i++) {
twit.get('search/tweets', {q: config.app.hashtags[i]}, function(err, data) {
if (!err && data && data.statuses) {
// console.log('found ', data);
data.statuses = data.statuses.reverse();
for (var tweet in data.statuses) {
data.statuses[tweet].CHECK = rights.checkUser({
username : data.statuses[tweet].from_user,
userid : data.statuses[tweet].from_user_id,
tweetisok : false
}, 'search');
if (data.statuses[tweet].CHECK.tweetisok) {
data.statuses[tweet].text = utils.parseHasTags(data.statuses[tweet].text);
data.statuses[tweet].text = utils.parseLinks(data.statuses[tweet].text);
sock.emit('tweetSearch', data.statuses[tweet]);
} else {
console.log('!= ' + data.statuses[tweet].from_user + ' =! blocked!!');
}
}
} else {
console.log('err', err);
}
});
}
app.twStream(sock);
};
app.twStream = function(sock) {
twit.stream('statuses/filter', {track: 'bcef15'}, function(stream) {
stream.on('data', function(data) {
if (data && data.text) {
data.CHECK = rights.checkUser({
username : data.user.screen_name,
userid : data.user.id,
description : data.user.description,
followerscount: data.user.followers_count,
friendscount : data.user.friends_count,
created : data.user.created_at,
tweetisok : false
});
if (data.CHECK.tweetisok) {
data.text = utils.parseHasTags(data.text);
data.text = utils.parseLinks(data.text);
// Send data to sender
sock.emit('tweet', data);
// console.log('ein tweet ein tweet!');
// send data to all other clients exept sender
sock.broadcast.emit('tweet', data);
} else {
console.log('!= ' + data.user.screen_name + ' =! blocked!!');
}
}
});
stream.on('error', function(err) {
console.log('_ stream error', err);
});
});
};
// Listen on port
server.listen(config.app.port);
console.log('Server started on port ' + config.app.port);