forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cores.js
226 lines (218 loc) · 6.83 KB
/
cores.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const got = require('got');
const cheerio = require('cheerio');
const { logger } = require('../middleware/logger');
const REDDIT_CORES = 'https://old.reddit.com/r/spacex/wiki/cores';
const API = process.env.SPACEX_API;
const KEY = process.env.SPACEX_KEY;
const HEALTHCHECK = process.env.CORES_HEALTHCHECK;
/**
* Update cores
* @return {Promise<void>}
*/
module.exports = async () => {
try {
const cores = await got.post(`${API}/cores/query`, {
json: {
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
});
const result = await got(REDDIT_CORES);
const $ = cheerio.load(result.body);
// Active Cores Table
const scrapedActive = [];
$('div.md:nth-child(2) > table:nth-child(13) > tbody:nth-child(2) > tr').each((index, element) => {
if (index === 0) return true;
const tds = $(element).find('td');
const coreSerial = $(tds[0]).text() || null;
const coreStatus = $(tds[5]).text().replace(/\[source\]/gi, '').trim() || null;
if (!coreSerial && !coreStatus) return true;
const tableRow = {
coreSerial,
coreStatus,
};
return scrapedActive.push(tableRow);
});
if (!scrapedActive.length) {
throw new Error('No active cores found');
}
const activeUpdates = scrapedActive.map(async (row) => {
const coreId = cores.docs.find((core) => core.serial === row.coreSerial);
if (coreId?.id) {
await got.patch(`${API}/cores/${coreId.id}`, {
json: {
last_update: row.coreStatus,
status: 'active',
},
headers: {
'spacex-key': KEY,
},
});
}
});
await Promise.all(activeUpdates);
logger.info('Active cores updated');
const inactive = $('div.md:nth-child(2) > table:nth-child(16) > tbody:nth-child(2)').text();
const inactiveRow = inactive.split('\n').filter((v) => v !== '');
const inactiveCores = inactiveRow.filter((value, index) => index % 6 === 0);
if (!inactiveCores.length) {
throw new Error('No inactive cores found');
}
const inactiveStatus = inactiveRow.filter((value, index) => (index + 1) % 6 === 0).map((x) => x.replace(/\[source\]/gi, ''));
const inactiveUpdates = inactiveCores.map(async (coreSerial, index) => {
const coreId = cores.docs.find((core) => core.serial === coreSerial);
if (coreId?.id) {
await got.patch(`${API}/cores/${coreId.id}`, {
json: {
last_update: inactiveStatus[parseInt(index, 10)],
status: 'inactive',
},
headers: {
'spacex-key': KEY,
},
});
}
});
await Promise.all(inactiveUpdates);
logger.info('Inactive cores updated');
const lost = $('div.md:nth-child(2) > table:nth-child(20) > tbody:nth-child(2)').text();
const lostRow = lost.split('\n').filter((v) => v !== '');
const lostCores = lostRow.filter((value, index) => index % 7 === 0);
if (!lostCores.length) {
throw new Error('No lost cores found');
}
const lostStatus = lostRow.filter((value, index) => (index + 1) % 7 === 0).map((x) => x.replace(/\[source\]/gi, ''));
const lostUpdates = lostCores.map(async (coreSerial, index) => {
const coreId = cores.docs.find((core) => core.serial === coreSerial);
if (coreId?.id) {
let status;
if (lostStatus[parseInt(index, 10)].match(/expended/i)) {
status = 'expended';
} else {
status = 'lost';
}
await got.patch(`${API}/cores/${coreId.id}`, {
json: {
last_update: lostStatus[parseInt(index, 10)],
status,
},
headers: {
'spacex-key': KEY,
},
});
}
});
await Promise.all(lostUpdates);
logger.info('Lost cores updated');
const reuseUpdates = cores.docs.map(async (core) => {
if (!core?.id) return;
const [rtlsAttempts, rtlsLandings, asdsAttempts, asdsLandings] = await Promise.all([
got.post(`${API}/launches/query`, {
json: {
query: {
upcoming: false,
cores: {
$elemMatch: {
core: core.id,
landing_type: 'RTLS',
landing_attempt: true,
},
},
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
throwHttpErrors: false,
}),
got.post(`${API}/launches/query`, {
json: {
query: {
upcoming: false,
cores: {
$elemMatch: {
core: core.id,
landing_type: 'RTLS',
landing_attempt: true,
landing_success: true,
},
},
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
throwHttpErrors: false,
}),
got.post(`${API}/launches/query`, {
json: {
query: {
upcoming: false,
cores: {
$elemMatch: {
core: core.id,
landing_type: 'ASDS',
landing_attempt: true,
landing_success: true,
},
},
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
throwHttpErrors: false,
}),
got.post(`${API}/launches/query`, {
json: {
query: {
upcoming: false,
cores: {
$elemMatch: {
core: core.id,
landing_type: 'ASDS',
landing_attempt: true,
landing_success: true,
},
},
},
options: {
pagination: false,
},
},
resolveBodyOnly: true,
responseType: 'json',
throwHttpErrors: false,
}),
]);
await got.patch(`${API}/cores/${core.id}`, {
json: {
reuse_count: (core.launches.length > 0) ? core.launches.length - 1 : 0,
rtls_attempts: rtlsAttempts.totalDocs,
rtls_landings: rtlsLandings.totalDocs,
asds_attempts: asdsAttempts.totalDocs,
asds_landings: asdsLandings.totalDocs,
},
headers: {
'spacex-key': KEY,
},
});
});
await Promise.all(reuseUpdates);
logger.info('Core reuse updated');
if (HEALTHCHECK) {
await got(HEALTHCHECK);
}
} catch (error) {
console.log(`Cores Error: ${error.message}`);
}
};