-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
166 lines (149 loc) · 4.85 KB
/
routes.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
const express = require('express');
const router = express.Router();
const db = require('./utils/db');
const logger = require('./utils/logger').logger;
const twitter = require('./twitter');
const config = require('./config');
router.get('/stats', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let statsCol = db.get().collection("stats");
statsCol.find({}).toArray((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send(result);
});
});
router.get('/stats/reset', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let statsCol = db.get().collection("stats");
statsCol.drop((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send({msg: "stats reset OK"});
});
});
router.get('/tweets', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let concoursTweets = db.get().collection("concoursTweets");
concoursTweets.find({}).sort({ _id: -1 }).limit(10).toArray((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send(result);
});
});
router.get('/tweets/reset', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let concoursTweets = db.get().collection("concoursTweets");
concoursTweets.drop((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send({msg: "concoursTweets reset OK"});
});
});
router.get('/tweets/dropAll', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let concoursTweets = db.get().collection("concoursTweets");
concoursTweets.drop((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send({msg: "concoursTweets reset OK"});
});
});
router.get('/startStream', (req, res) => {
res.setHeader('Content-Type', 'application/json');
twitter.startConcoursStream();
res.status(200).send({msg: "startStream OK"});
});
router.get('/stopStream', (req, res) => {
res.setHeader('Content-Type', 'application/json');
twitter.stopConcoursStream();
res.status(200).send({msg: "stopStream OK"});
});
router.get('/startConcours', (req, res) => {
res.setHeader('Content-Type', 'application/json');
twitter.startConcours();
res.status(200).send({msg: "startConcours OK"});
});
router.get('/stopConcours', (req, res) => {
res.setHeader('Content-Type', 'application/json');
twitter.stopConcours();
res.status(200).send({msg: "stopConcours OK"});
});
router.get('/config', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let cfg = config;
delete cfg.dbUrl;
delete cfg.dbName;
res.status(200).send(cfg);
});
router.post('/config', (req, res) => {
let concoursInterval = req.body.concoursInterval;
let pause = req.body.pause;
let nbConcoursTweetsPerInterval = req.body.nbConcoursTweetsPerInterval;
let nbRandomTweetsPerInterval = req.body.nbRandomTweetsPerInterval;
let stream = req.body.stream;
let concours = req.body.concours;
let msg = [];
let configHasChanged = false;
if(concoursInterval && concoursInterval != config.concoursInterval){
msg.push("concoursInterval is updated (from " + config.concoursInterval + "min to " + concoursInterval + "min)");
config.concoursInterval = concoursInterval;
configHasChanged = true;
}
if(pause && pause != config.pause){
msg.push("pause is updated (from " + config.pause + "min to " + pause + "min)");
config.pause = pause;
configHasChanged = true;
}
if(nbConcoursTweetsPerInterval && nbConcoursTweetsPerInterval != config.nbConcoursTweetsPerInterval){
msg.push("nbConcoursTweetsPerInterval is updated (from " + config.nbConcoursTweetsPerInterval + " to " + nbConcoursTweetsPerInterval + ")");
config.nbConcoursTweetsPerInterval = nbConcoursTweetsPerInterval;
configHasChanged = true;
}
if(nbRandomTweetsPerInterval && nbRandomTweetsPerInterval != config.nbRandomTweetsPerInterval){
msg.push("nbRandomTweetsPerInterval is updated (from " + config.nbRandomTweetsPerInterval + " to " + nbRandomTweetsPerInterval + ")");
config.nbRandomTweetsPerInterval = nbRandomTweetsPerInterval;
configHasChanged = true;
}
if(configHasChanged){
logger.info("Config has changed:");
msg.forEach((m) => {
logger.info(m);
});
}
if(config.stream != stream){
if(stream && !config.stream){
twitter.startConcoursStream();
}
else if(!stream && config.stream) {
twitter.stopConcoursStream();
}
config.stream = stream;
}
if(config.concours != concours){
if(concours && !config.concours){
twitter.startConcours();
}
else if(!concours && config.concours) {
twitter.stopConcours();
}
config.concours = concours;
}
else if(config.concours && config.concours == concours && configHasChanged) {
logger.info("Restarting because config has changed.");
twitter.stopConcours();
twitter.startConcours();
}
res.status(200).send({msg: "New bot configuration updated."});
});
module.exports = router;