-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (85 loc) · 2.73 KB
/
index.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
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const express = require("express");
const cors = require('cors');
const countryDataURL = "https://www.mohfw.gov.in/";
const stateDataURL = "https://www.mohfw.gov.in/data/datanew.json"
const port = 4000;
const app = express();
app.use(cors());
app.use(express.json());
const expirationTime = 5 // minutes;
const covidData = {
countryData : {},
stateData : {}
};
const extractFirstNumber = (s) => s.split('(')[0];
const extractSecondNumber = (s) => s.split('(')[1].slice(0,-1);
async function getCountryData() {
let countryData = [];
try {
const { data } = await axios.get(countryDataURL);
const $ = cheerio.load(data);
const statsDiv = $(".site-stats-count");
const dataArr = $(statsDiv).children("ul").children("li").children(".mob-show").children("strong");
dataArr.each((i, el) => {
countryData.push($(el).text());
});
}
catch (e) {
console.log(`Error scraping the country data: ${e} `);
}
covidData.countryData = {
lastFetched : Math.floor(Date.now()/60000),
data: {
"active": extractFirstNumber(countryData[0]),
"cured": extractFirstNumber(countryData[1]),
"deaths": extractFirstNumber(countryData[2]),
"new_active": extractSecondNumber(countryData[0]),
"new_cured": extractSecondNumber(countryData[1]),
"new_death": extractSecondNumber(countryData[2]),
}
};
}
async function getStateData() {
try {
const { data } = await axios.get(stateDataURL);
covidData.stateData = {
lastFetched : Math.floor(Date.now()/60000),
data
};
} catch (e) {
console.log(`Error while fetching state wise data: ${e}`);
}
}
//Web API
app.get('/data', async (req, res) => {
console.log('Received request to fetch covid data ')
try {
const currTime = Math.floor(Date.now()/60000);
if (
!covidData.countryData.data ||
( covidData.countryData.currTime &&
currTime - covidData.countryData.lastFetched > expirationTime
)
)
await getCountryData();
if (
!covidData.stateData.data ||
( covidData.stateData.currTime &&
currTime - covidData.stateData.lastFetched > expirationTime
)
)
await getStateData();
res.send(covidData);
} catch (e) {
res.status(500).json(e);
}
});
app.listen(port, () =>
{
getCountryData();
getStateData();
console.log(`Server running, listening request on port ${port}`)
});