-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.js
84 lines (62 loc) · 1.84 KB
/
tweet.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
(function() {
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
var stream;
//Remove debug messages
io.set('log level',1);
server.listen(9999);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/client.js', function (req, res) {
res.sendfile(__dirname + '/client.js');
});
app.get('/functions.js', function (req, res) {
res.sendfile(__dirname + '/functions.js');
});
io.sockets.on('connection', function (socket) {
fetch_tweets(io);
//socket.emit('new_tweet', "aeiou");
socket.on('reply', function (data) {
console.log("Evaluating:"+data);
if ( data == 1000){
kill_stream();
}
});
});
function kill_stream() {
console.log("Trying to close stream");
stream.destroy();
}
function fetch_tweets(io) {
var twitter = require('ntwitter');
var config = require('./config.json');
var twit = new twitter({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token_key: config.access_token_key,
access_token_secret: config.access_token_secret
});
console.log("Request for tweets. ");
twit.stream('statuses/sample', function(s) {
stream = s;
stream.on('data', function (data) {
//conn.write(data.text);
io.sockets.emit('new_tweet',data.text);
});
});
}
var stringToColour = function(str) {
if (str){
// str to hash
for (var i = 0, hash = 0; i < str.length; hash = str.charCodeAt(i++) + ((hash << 5) - hash));
// int/hash to hex
for (var i = 0, colour = "#"; i < 3; colour += ("00" + ((hash >> i++ * 8) & 0xFF).toString(16)).slice(-2));
}
else {
colour="#00FF00"
}
return colour;
}
})();