This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twinding.js
executable file
·107 lines (85 loc) · 2.27 KB
/
twinding.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
var twinding = {},
io = require('socket.io').listen(8080),
twitter = require('ntwitter'),
clients,
history,
historyLength,
twit,
DEBUG;
// Helper function
logDebug(data) {
if (DEBUG && DEBUG===1) {
console.log(data);
}
}
clients = [];
history = [];
historyLength = 20;
twit = {};
DEBUG = 0;
twinding.start = function (config) {
if (twit && twit.search) {
logDebug("WARNING: Server already started");
return;
}
if (config.history) {
historyLength = config.history;
}
if (config.debug) {
DEBUG = 1;
}
twit = new twitter(config.twitter);
io.sockets.on('connection', function (socket) {
clients.push(socket);
history.forEach(function(elem) {
socket.emit('tweet',elem);
});
socket.on('disconnect', function () {
clients.remove(socket);
});
});
};
initHistory = function(search) {
if (!twit.search) {
logDebug("ERROR: twitter Object not setup");
return;
}
twit.search(search, {'include_entities': 'true'}, function(err, data) {
logDebug(data);
data.results.reverse();
data.results.forEach(function(elem) {
prepareSearchData(elem,function(d) {
pushHistory(d);
});
});
});
};
startStream = function (search) {
twit.stream('statuses/filter', {track: search}, function(stream) {
stream.on('data', function (data) {
logDebug(data);
prepareStreamData(data, function (d) {
pushHistory(data);
clients.forEach(function(client) {
client.emit('tweet',data);
});
});
});
});
};
prepareStreamData = function (data, callback) {
// TODO: reassamble data to new data object to unify
// data between streaming and normal API
callback(data);
}
prepareSearchData = function (data, callback) {
// TODO: reassamble data to new data object to unify
// data between streaming and normal API
callback(data);
}
pushHistory = function (data) {
history.push(data);
if(history.length > historyLength) {
history.splice(0,1);
}
}