-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
161 lines (134 loc) · 3.66 KB
/
server.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const express = require("express");
const bodyParser = require("body-parser");
const util = require("util");
const request = require("request");
const path = require("path");
const socketIo = require("socket.io");
const http = require("http");
const app = express();
const port = process.env.PORT || 5000;
const post = util.promisify(request.post);
const get = util.promisify(request.get);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = http.createServer(app);
const io = socketIo(server);
const CONSUMER_KEY = process.env.TWITTER_CONSUMER_KEY;
const CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET;
let timeout = 0;
const bearerTokenURL = new URL("https://api.twitter.com/oauth2/token");
const streamURL = new URL(
"https://api.twitter.com/labs/1/tweets/stream/filter?format=detailed&expansions=author_id"
);
const rulesURL = new URL(
"https://api.twitter.com/labs/1/tweets/stream/filter/rules"
);
const errorMessage = {
title: "Please Wait",
detail: "Waiting for new jobs to be posted..."
};
const sleep = async delay => {
return new Promise(resolve => setTimeout(() => resolve(true), delay));
};
async function bearerToken(auth) {
const requestConfig = {
url: bearerTokenURL,
auth: {
user: CONSUMER_KEY,
pass: CONSUMER_SECRET
},
form: {
grant_type: "client_credentials"
}
};
const response = await post(requestConfig);
return JSON.parse(response.body).access_token;
}
app.get("/rules", async (req, res) => {
const token = await bearerToken({ CONSUMER_KEY, CONSUMER_SECRET });
const requestConfig = {
url: rulesURL,
auth: {
bearer: token
},
json: true
};
try {
const response = await get(requestConfig);
if (response.statusCode !== 200) {
throw new Error(response.body.error.message);
}
res.send(response);
} catch (e) {
res.send(e);
}
});
app.post("/rules", async (req, res) => {
const token = await bearerToken({ CONSUMER_KEY, CONSUMER_SECRET });
const requestConfig = {
url: rulesURL,
auth: {
bearer: token
},
json: req.body
};
try {
const response = await post(requestConfig);
if (response.statusCode === 200 || response.statusCode === 201) {
res.send(response);
} else {
throw new Error(response);
}
} catch (e) {
res.send(e);
}
});
const streamTweets = (socket, token) => {
const config = {
url: streamURL,
auth: {
bearer: token
},
timeout: 31000
};
const stream = request.get(config);
stream
.on("data", data => {
try {
const json = JSON.parse(data);
if (json.connection_issue) {
socket.emit("error", json);
reconnect(stream, socket, token);
} else {
socket.emit("tweet", json);
}
} catch (e) {
socket.emit("heartbeat");
}
})
.on("error", error => {
// Connection timed out
socket.emit("error", errorMessage);
reconnect(stream, socket, token);
});
};
const reconnect = async (stream, socket, token) => {
timeout++;
stream.abort();
await sleep(2 ** timeout * 1000);
streamTweets(socket, token);
};
io.on("connection", async socket => {
try {
const token = await bearerToken({ CONSUMER_KEY, CONSUMER_SECRET });
io.emit("connect", "Client connected");
const stream = streamTweets(io, token);
} catch (e) {}
});
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
server.listen(port, () => console.log(`Listening on port ${port}`));