|
314
|
1 |
var fs = require('fs'), |
|
|
2 |
https = require('https'), |
|
|
3 |
io = require('socket.io') |
|
|
4 |
.listen(8000) |
|
|
5 |
keyword = "Bieber", |
|
|
6 |
tweets = [], |
|
|
7 |
tweet_ids = [], |
|
|
8 |
annkw = { |
|
|
9 |
'positive' : '++', |
|
|
10 |
'negative' : '--', |
|
|
11 |
'reference' : '==', |
|
|
12 |
'question' : '??' |
|
|
13 |
} |
|
|
14 |
|
|
|
15 |
function textids(object) { |
|
|
16 |
for (var key in object) { |
|
|
17 |
if (key.substr(-2) == 'id') { |
|
|
18 |
object[key] = object[key + '_str']; |
|
|
19 |
delete object[key + '_str']; |
|
|
20 |
} |
|
|
21 |
} |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
var fd = fs.createWriteStream('tweets.txt'); |
|
|
25 |
|
|
|
26 |
req = https.request({ |
|
|
27 |
host: "stream.twitter.com", |
|
|
28 |
path: "/1/statuses/filter.json", |
|
|
29 |
method: "POST", |
|
|
30 |
headers: { |
|
|
31 |
'Authorization': 'Basic cmFwaHY6N3czMzdMZkMyM2dF', |
|
|
32 |
'Content-Type': 'application/x-www-form-urlencoded' |
|
|
33 |
} |
|
|
34 |
}, function(res) { |
|
|
35 |
console.log('STATUS: ' + res.statusCode); |
|
|
36 |
console.log('HEADERS: ' + JSON.stringify(res.headers)); |
|
|
37 |
res.setEncoding('utf8'); |
|
|
38 |
res.on('data', function(chunk) { |
|
|
39 |
newdata = chunk.split('\r\n'); |
|
|
40 |
try { |
|
|
41 |
for (var i in newdata) { |
|
|
42 |
if (newdata[i].length > 0) { |
|
|
43 |
tweet = JSON.parse(newdata[i]); |
|
|
44 |
annotations = []; |
|
|
45 |
for (var a in annkw) { |
|
|
46 |
if (tweet.text.indexOf(annkw[a]) != -1) { |
|
|
47 |
annotations.push(a); |
|
|
48 |
} |
|
|
49 |
} |
|
|
50 |
tweet.annotations = annotations; |
|
|
51 |
tweets.push(tweet); |
|
|
52 |
textids(tweet); |
|
|
53 |
tweet_ids.push(tweet.id_str); |
|
|
54 |
io.sockets.emit('newtweet', tweet); |
|
|
55 |
} |
|
|
56 |
} |
|
|
57 |
fd.write(chunk); |
|
|
58 |
} |
|
|
59 |
catch(err) { |
|
|
60 |
console.log(err); |
|
|
61 |
} |
|
|
62 |
}); |
|
|
63 |
}); |
|
|
64 |
|
|
|
65 |
req.write('track=' + encodeURIComponent(keyword)); |
|
|
66 |
req.end(); |
|
|
67 |
io.set('log level', 0); |
|
|
68 |
io.sockets.on('connection', function(socket) { |
|
|
69 |
socket.emit('tweets', tweets.slice(-10)); |
|
|
70 |
socket.on('tweetsbefore', function(data) { |
|
|
71 |
tweetpos = tweet_ids.indexOf(data); |
|
|
72 |
socket.emit('oldtweets', tweets.slice(0, tweetpos).slice(-10)); |
|
|
73 |
}); |
|
|
74 |
}); |