-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
194 lines (163 loc) · 4.46 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require('dotenv').load();
// init project
const express = require('express');
const app = express();
const axios = require('axios');
const Twit = require('twit');
const config = require('./config.js');
console.log(config);
let T = new Twit(config.twitter);
const onTweet = require('./onTweet.js');
const getUnanswered = require('./getUnanswered.js');
const topFollowers = require('./topFollowers.js');
const sendEmail = require('./sendEmail.js');
const getTokens = require('./renew-token/getTokens.js');
let queue = [];
let streamHasStarted = false;
let isRunning = false;
let updatingKeys = false;
const processQueue = async _ => {
if (isRunning) {
console.log('There is a tweet being processed');
return;
}
if (queue.length === 0 || updatingKeys) {
return;
}
isRunning = true;
const tweet = queue.shift();
try {
await onTweet(tweet);
} catch (error) {
if (error.code === 89) {
console.log('Invalid token, stopping process');
checkConnection();
return;
}
console.error('There was an error:', error.message);
queue.push(tweet);
console.log('Retrying everything');
}
isRunning = false;
processQueue();
};
const processUnanswered = async _ => {
isRunning = true;
try {
const unanswered = await getUnanswered();
queue = queue.concat(unanswered);
} catch (error) {
console.log(error);
}
isRunning = false;
processQueue();
};
const checkConnection = async _ => {
try {
console.log('Checking connection..');
const tweet = await T.get('statuses/show', { id: '1027869816542175232' });
console.log('Connection Ok');
} catch (error) {
queue = [];
console.error('Connection error, trying to update keys...');
await updateT();
}
};
const updateT = async _ => {
if (updatingKeys) {
console.log('Already updating keys');
return;
}
try {
axios.get(`https://ahorro-click-telegram-bot.glitch.me/?message=Actualizando las claves`);
updatingKeys = true;
const tokens = await getTokens();
updatingKeys = false;
let keys = config.twitter;
keys.access_token = tokens.accessToken;
keys.access_token_secret = tokens.tokenSecret;
config.setTwitterKeys(keys);
T = new Twit(config.getTwitterKeys());
streamHasStarted = false;
processUnanswered();
startStream();
} catch (error) {
axios.get(`https://ahorro-click-telegram-bot.glitch.me/?message=No pude`);
console.log('Could not update keys. Aborting..');
console.log(error);
updatingKeys = false;
return;
}
};
const startStream = async _ => {
try {
const stream = T.stream('statuses/filter', { track: ['@AhorroClick'] });
streamHasStarted = true;
console.log('Stream started');
stream.on('error', error => {
console.log(error.message);
checkConnection();
});
stream.on('tweet', async tweet => {
queue = [tweet, ...queue];
try {
processQueue();
} catch (error) {
console.error('We broke everything:', error);
}
});
} catch (error) {
console.log(error);
await updateT();
}
};
app.get('/', async function(req, res) {
if (streamHasStarted) {
console.log('I already had a stream running');
checkConnection();
res.send('OK, already running');
return;
}
startStream();
res.send('OK');
});
app.get('/processUnanswered', async (req, res) => {
const pwd = req.query.pwd;
if (pwd !== process.env.PASSWORD) {
return;
}
await processUnanswered();
res.send('Unanswered tweets were processed');
});
app.get('/topFollowers', async (req, res) => {
const pwd = req.query.pwd;
if (pwd !== process.env.PASSWORD) {
console.error('An attempt to post without password was made');
res.send('WRONG PASSWORD');
return;
}
try {
const status = await topFollowers();
console.log('Posting Top Followers: ', status);
await T.post('statuses/update', { status: status });
console.log('Posted');
} catch (error) {
console.error(`Couldn't post top followers`, error);
}
res.send('Top followers posted');
});
app.get('/renewToken', async (req, res) => {
const tokens = await getTokens();
console.log({ tokens });
res.send('done');
});
app.get('/queue', (req, res) => {
res.send(`Quedan ${queue.length} para procesar`);
});
app.get('/resetQueue', (req, res) => {
console.log('Resetting queue');
queue = [];
});
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});