forked from disease-sh/API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (128 loc) · 4.35 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
const express = require('express');
const cors = require('cors');
const Redis = require('ioredis');
const config = require('./config.json');
const scraper = require('./scraper');
const countryMap = require('./funcs/countryMap');
const countryUtils = require('./utils/country_utils');
const app = express();
app.use(cors());
// create redis instance :O
const redis = new Redis(config.redis.host, {
password: config.redis.password,
port: config.redis.port
});
const { keys } = config;
const execAll = () => {
scraper.getWorldometers.getCountries(keys, redis);
scraper.getAll(keys, redis);
scraper.getStates(keys, redis);
scraper.jhuLocations.jhudata(keys, redis);
scraper.jhuLocations.jhudataV2(keys, redis);
scraper.historical.historicalV2(keys, redis);
};
execAll();
setInterval(execAll, config.interval);
app.get('/', async (request, response) => {
response.redirect('https://github.com/novelcovid/api');
});
const listener = app.listen(config.port, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});
app.get('/invite', async (req, res) => {
/* eslint max-len: off */
res.redirect('https://discordapp.com/oauth2/authorize?client_id=685268214435020809&scope=bot&permissions=537250880');
});
app.get('/support', async (req, res) => {
res.redirect('https://discord.gg/EvbMshU');
});
// API endpoints
app.get('/all', async (req, res) => {
const all = JSON.parse(await redis.get(keys.all));
res.send(all);
});
app.get('/states', async (req, res) => {
const states = JSON.parse(await redis.get(keys.states));
res.send(states);
});
app.get('/jhucsse', async (req, res) => {
const data = JSON.parse(await redis.get(keys.jhu));
res.send(data);
});
app.get('/countries', async (req, res) => {
const { sort } = req.query;
let countries = JSON.parse(await redis.get(keys.countries));
if (sort) {
countries = countries.sort((a, b) => a[sort] > b[sort] ? -1 : 1);
}
res.send(countries);
});
app.get('/countries/:query', async (req, res) => {
const countries = JSON.parse(await redis.get(keys.countries));
const { query } = req.params;
/* eslint-disable-next-line no-restricted-globals */
const isText = isNaN(query);
const country = countries.find((ctry) => {
if (isText) {
// either name or ISO
const standardizedCountryName = countryMap.standardizeCountryName(query.toLowerCase());
// check for strict param
if (req.query.strict) {
return req.query.strict.toLowerCase() === 'true'
? ctry.country.toLowerCase() === standardizedCountryName
: ctry.country.toLowerCase().includes(standardizedCountryName);
}
return (
(ctry.countryInfo.iso3 || 'null').toLowerCase() === query.toLowerCase()
|| (ctry.countryInfo.iso2 || 'null').toLowerCase() === query.toLowerCase()
|| ((query.length > 3 || countryUtils.isCountryException(query.toLowerCase()))
&& ctry.country.toLowerCase().includes(standardizedCountryName))
);
}
// number, must be country ID
return ctry.countryInfo._id === Number(query);
});
if (country) {
res.send(country);
return;
}
// adding status code 404 not found and sending response
res.status(404).send({ message: 'Country not found or doesn\'t have any cases' });
});
app.get('/v2/historical', async (req, res) => {
const data = JSON.parse(await redis.get(keys.historical_v2));
data.forEach(item => {
delete item.countryInfo;
});
res.send(data);
});
app.get('/v2/historical/all', async (req, res) => {
const data = JSON.parse(await redis.get(keys.historical_v2));
const allData = await scraper.historical.getHistoricalAllDataV2(data);
res.send(allData);
});
app.get('/v2/historical/:query/:province?', async (req, res) => {
const data = JSON.parse(await redis.get(keys.historical_v2));
const { query, province } = req.params;
const countryData = await scraper.historical.getHistoricalCountryDataV2(
data,
query.toLowerCase(),
province && province.toLowerCase()
);
if (countryData) {
res.send(countryData);
} else {
res.status(404).send({ message: 'Country not found or doesn\'t have any historical data' });
}
});
app.get('/v2/jhucsse', async (req, res) => {
const data = JSON.parse(await redis.get(keys.jhu_v2));
res.send(data);
});
// deprecated
app.get('/historical', async (req, res) => {
res.send({ message: 'Deprecated, use /v2/historical' });
});
app.get('/historical/:country', async (req, res) => {
res.send({ message: 'Deprecated, use /v2/historical' });
});