-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterRulesFetch.js
129 lines (107 loc) · 2.64 KB
/
twitterRulesFetch.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
const http = require('http');
const path = require('path');
const express = require('express');
const socketIo = require('socket.io');
const needle = require('needle');
const config = require('dotenv').config();
const TOKEN = process.env.BEARER_TOKEN;
const PORT = process.env.PORT || 3030;
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, './', 'client', 'index.html'));
});
const rulesURL = 'https://api.twitter.com/2/tweets/search/stream/rules';
const streamULR = 'https://api.twitter.com/2/tweets/search/stream?tweet.fields=public_metrics&expansions=author_id';
// const rules = [
// {
// value: 'dog has:images -is:retweet',
// tag: 'dog pictures'
// },
// {
// value: 'cat has:images -grumpy',
// tag: 'cat pictures'
// // 'from': 'twitterdev' OR from:twitterapi -from:twitter
// // lang: uk, en
// }
// ];
const rules = [ { value: 'from:romanprotoliuk' } ];
// need three seperate functionssda
// 1 to get rules
// Get stream rules
const getRules = async () => {
const response = await needle('get', rulesURL, {
headers: {
Authorization: `Bearer ${TOKEN}`
}
});
// shows rules
console.log(response.body);
return response.body;
};
// 2 to set rules
// Set stream rules
const setRules = async () => {
const data = {
add: rules
};
const response = await needle('post', rulesURL, data, {
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${TOKEN}`
}
});
return response.body;
};
// 3 delete rules
// Delete stream rules
const deleteRules = async (rules) => {
if (!Array.isArray(rules.data)) {
return null;
}
const ids = rules.data.map((rule) => rule.id);
const data = {
delete: {
ids: ids
}
};
const response = await needle('post', rulesURL, data, {
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${TOKEN}`
}
});
return response.body;
};
const streamTweets = (socket) => {
const stream = needle.get(streamULR, {
headers: {
Authorization: `Bearer ${TOKEN}`
}
});
stream.on('data', (data) => {
try {
const json = JSON.parse(data);
console.log(json);
socket.emit('tweet', json);
} catch (error) {}
});
};
io.on('connection', async () => {
console.log('client connected...');
let currentRules;
try {
// GET all stream rules
currentRules = await getRules();
// DELETE all stream rules
await deleteRules(currentRules);
// SET rules based on array above
await setRules();
} catch (error) {
console.log(error);
process.exit(1);
}
streamTweets(io);
});
server.listen(PORT, () => console.log(`Listening on port ${PORT}`));