forked from disease-sh/API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
86 lines (76 loc) · 2.52 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
var express = require('express');
var app = express();
var axios = require("axios");
var cheerio = require("cheerio");
var cors = require('cors');
const config = require('./config.json');
const Redis = require('ioredis');
const scraper = require('./scraper');
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.keys;
const execAll = () => {
scraper.getCountries(keys, redis);
scraper.getAll(keys, redis);
scraper.getStates(keys, redis);
scraper.jhuLocations(keys, redis);
scraper.historical.historical(keys, redis);
};
execAll()
setInterval(execAll, config.interval);
app.get("/", async function (request, response) {
response.redirect('https://github.com/novelcovid/api');
});
var listener = app.listen(config.port, function () {
console.log("Your app is listening on port " + listener.address().port);
});
app.get("/all/", async function (req, res) {
let all = JSON.parse(await redis.get(keys.all))
res.send(all);
});
app.get("/countries/", async function (req, res) {
let sort = req.query.sort;
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("/states/", async function (req, res) {
let states = JSON.parse(await redis.get(keys.states))
res.send(states);
});
app.get("/jhucsse/", async function (req, res) {
let data = JSON.parse(await redis.get(keys.jhu))
res.send(data);
});
app.get("/historical/", async function (req, res) {
let data = JSON.parse(await redis.get(keys.historical))
res.send(data);
});
app.get("/historical/:country", async function (req, res) {
let data = JSON.parse(await redis.get(keys.historical))
const countryData = await scraper.historical.getHistoricalCountryData(data, req.params.country.toLowerCase());
res.send(countryData);
});
app.get("/countries/:country", async function (req, res) {
let countries = JSON.parse(await redis.get(keys.countries))
let country = countries.find(
e => e.country.toLowerCase().includes(req.params.country.toLowerCase())
);
if (!country) {
res.send("Country not found");
return;
}
res.send(country);
});
app.get("/invite/", async function (req, res) {
res.redirect("https://discordapp.com/oauth2/authorize?client_id=685268214435020809&scope=bot&permissions=537250880")
});
app.get("/support/", async function (req, res) {
res.redirect("https://discord.gg/EvbMshU")
});