forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
136 lines (125 loc) · 4.35 KB
/
app.js
File metadata and controls
136 lines (125 loc) · 4.35 KB
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
// node modules
const express = require('express'),
app = express(),
util = require('./bin/util'),
axios = require('axios'),
covid19 = require('./lib/cli'),
pkg = require('./package.json'), // package.json info
apiBaseURL = "https://corona.lmao.ninja", // NovelCOVID API
port = process.env.port || 7070; // set port
// global route for covid19 tracker
app.get('/', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19globaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});
// global historical chart
app.get(['/history/all/:chartType(cases|deaths)?', '/history/'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
chartType = req.params.chartType || 'cases',
history = await axios.get(`${apiBaseURL}/v2/historical/all?lastdays=all`),
h = history.data;
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.historyGlobalTracker(
data.cases, data.deaths,
data.recovered, data.updated,
h, chartType
));
return null;
}
return next();
});
// for cmd and powershell
app.get(['/plain','/cmd','/basic'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plainglobaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});
// help options
app.get(['/help','/manual','/cmd/help','/plain/help','/basic/help'], async (req, res, next) => {
const userAgent = req.headers['user-agent'];
if (util.isCommandline(userAgent)) {
await res.send(covid19.help());
return null;
}
return next();
});
// by country route for covid19 tracker
app.get('/:country', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19countrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
d.updated
));
return null;
}
return next();
});
// by country route for covid19 tracker
app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plaincountrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
d.updated
));
return null;
}
return next();
});
// historical chart by country
app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
chartType = req.params.chartType || 'cases',
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
s = summary.data,
h = history.data;
if (util.isCommandline(userAgent)) {
await res.send(
covid19.historyCountryTracker(
s.country, s.cases, s.todayCases,
s.deaths, s.todayDeaths, s.recovered,
s.active, s.critical, s.casesPerOneMillion,
s.updated, h, chartType
)
);
return null;
}
return next();
});
app.get('*', (req, res) => res.send(`
Welcome to COVID-19 Tracker CLI v${pkg.version} by Waren Gonzaga\n
Please visit: https://warengonza.ga/covid19-tracker-cli
\n`));
app.listen(port, () => console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`));