-
Notifications
You must be signed in to change notification settings - Fork 639
/
updateCheck.js
70 lines (65 loc) · 2.18 KB
/
updateCheck.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
const axios = require('axios'),
{ config } = require('./routes/instances'),
logger = require('./utils/logger');
const now = new Date(),
rangeExtension = 1.5,
worldometersRange = 2,
jhuRange = 1000 * 60 * 60 * 24 * rangeExtension;
const endpoints = {
'covid-19': {
// 20 minutes
all: (data) => data.updated && now - new Date(data.updated) - (config.worldometersInterval * worldometersRange),
countries: (data) => data[0].updated && now - new Date(data[0].updated) - (config.worldometersInterval * worldometersRange),
continents: (data) => data[0].updated && now - new Date(data[0].updated) - (config.worldometersInterval * worldometersRange),
states: (data) => data[0].updated && now - new Date(data[0].updated) - (config.worldometersInterval * worldometersRange),
// 1.5 days
jhucsse: (data) => data[0].updated && now - new Date(data[0].updatedAt) - jhuRange,
// 1.5 days
'jhucsse/counties': (data) => data[0].updated && now - new Date(data[0].updatedAt) - jhuRange
},
influenza: []
};
const sendWebhook = async (data) => {
try {
await axios.post(process.env.UPDATE_WEBHOOK, data);
} catch (err) {
console.log(err);
}
};
const checkOutOfDate = async () => {
for (const disease of Object.keys(endpoints)) {
for (const [endpoint, checker] of Object.entries(endpoints[disease])) {
logger.info(`checking ${endpoint}`);
try {
const res = await axios.get(`https://disease.sh/v3/${disease}/${endpoint}`);
let delta;
// eslint-disable-next-line no-unused-expressions
if ((delta = checker(res.data)) > 0) {
logger.info('OUT OF DATE - sending Webhook message');
await sendWebhook({
embeds: [
{
title: `${disease}/${endpoint} - Out of Date`,
description: `${(delta / 1000 / 60 / 60).toFixed(1)} hours`,
url: `https://disease.sh/v3/${disease}/${endpoint}`
}
]
});
}
} catch (err) {
logger.info('ERROR - sending Webhook message');
await sendWebhook({
embeds: [
{
title: `${disease}/${endpoint} - Error`,
url: `https://disease.sh/v3/${disease}/${endpoint}`,
description: err.message || 'No message'
}
]
});
}
}
}
process.exit();
};
checkOutOfDate();