-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtweets.js
94 lines (61 loc) · 1.85 KB
/
tweets.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
require('dotenv').load();
var fs = require('fs');
var Twitter = require('node-tweet-stream')
var stream = new Twitter({
consumer_key: process.env.TW_KEY,
consumer_secret: process.env.TW_SECRET,
token: process.env.TW_TOKEN,
token_secret: process.env.TW_TOKEN_SECRET
});
var data = JSON.parse(fs.readFileSync('links.json'));
data.forEach(function(item){
item.count = 0;
});
var urls = data.map(function(obj){ return obj.href });
var url = require('url');
var urls_to_track = urls.map(function(link) {
var parsed_url = url.parse(link)
if ( parsed_url.host) {
var hostname = parsed_url.hostname.replace('www.', '')
var trimmed_url = hostname + parsed_url.path.replace(new RegExp('\/$', 'g'), '')
}
else {
trimmed_url = 'news.ycombinator.com/' + link
}
return trimmed_url
})
.filter(function(url) {
return url.length < 60
})
.join(',')
console.log(urls_to_track);
stream.track(urls_to_track);
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
io.on('connection', function (socket) {
// send the current content
socket.emit('urls', data);
});
stream.on('tweet', function(t){
var tweet_urls = (t.entities.urls||[]).map(function(url){
return url.display_url
});
console.log(t.text, tweet_urls);
// update the original object
data.filter(function(item){
return tweet_urls.filter(function(display_url){
return item.href.match(display_url)
}).length
}).forEach(function(item){
item.count = (item.count||0) + 1;
});
io.emit('urls', data);
});
stream.on('error', function (err) {
console.log('Oh no')
})
http.listen(3000);
console.log("http://localhost:3000")