Skip to content

Instantly share code, notes, and snippets.

@realinit
Created March 26, 2020 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realinit/a5ea5417390c276d9b9d2c1bd2a121fa to your computer and use it in GitHub Desktop.
Save realinit/a5ea5417390c276d9b9d2c1bd2a121fa to your computer and use it in GitHub Desktop.
get corona virus real time update using NodeJS
var express = require("express");
var app = express();
var request = require("request");
var axios = require("axios");
var cheerio = require("cheerio");
var db = require("quick.db");
var cors = require("cors");
app.use(cors());
app.use("/", express.static("www"));
var getall = setInterval(async () => {
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/");
if (response.status !== 200) {
console.log("ERROR");
}
} catch (err) {
return null;
}
// to store parsed data
const result = {};
// get HTML and parse death rates
const html = cheerio.load(response.data);
html(".maincounter-number").filter((i, el) => {
let count = el.children[0].next.children[0].data || "0";
count = parseInt(count.replace(/,/g, "") || "0", 10);
// first one is
if (i === 0) {
result.cases = count;
} else if (i === 1) {
result.deaths = count;
} else {
result.recovered = count;
}
});
db.set("all", result);
console.log("Updated The Cases", result);
}, 150000);
var getcountries = setInterval(async () => {
let response;
try {
response = await axios.get("https://www.worldometers.info/coronavirus/");
if (response.status !== 200) {
console.log("Error", response.status);
}
} catch (err) {
return null;
}
// to store parsed data
const result = [];
// get HTML and parse death rates
const html = cheerio.load(response.data);
const countriesTable = html("table#main_table_countries_today");
const countriesTableCells = countriesTable
.children("tbody")
.children("tr")
.children("td");
// NOTE: this will change when table format change in website
const totalColumns = 10;
const countryColIndex = 0;
const casesColIndex = 1;
const todayCasesColIndex = 2;
const deathsColIndex = 3;
const todayDeathsColIndex = 4;
const curedColIndex = 5;
const activeColIndex = 6;
const criticalColIndex = 7;
const casesPerOneMillionColIndex = 8;
const deathsPerOneMillionColIndex = 9;
// minus totalColumns to skip last row, which is total
for (let i = 0; i < countriesTableCells.length - totalColumns; i += 1) {
const cell = countriesTableCells[i];
// get country
if (i % totalColumns === countryColIndex) {
let country =
cell.children[0].data ||
cell.children[0].children[0].data ||
// country name with link has another level
cell.children[0].children[0].children[0].data ||
cell.children[0].children[0].children[0].children[0].data ||
"";
country = country.trim();
if (country.length === 0) {
// parse with hyperlink
country = cell.children[0].next.children[0].data || "";
}
result.push({ country: country.trim() || "" });
}
// get cases
if (i % totalColumns === casesColIndex) {
let cases = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].cases = parseInt(
cases.trim().replace(/,/g, "") || "0",
10
);
}
// get today cases
if (i % totalColumns === todayCasesColIndex) {
let cases = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].todayCases = parseInt(
cases.trim().replace(/,/g, "") || "0",
10
);
}
// get deaths
if (i % totalColumns === deathsColIndex) {
let deaths = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].deaths = parseInt(
deaths.trim().replace(/,/g, "") || "0",
10
);
}
// get today deaths
if (i % totalColumns === todayDeathsColIndex) {
let deaths = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].todayDeaths = parseInt(
deaths.trim().replace(/,/g, "") || "0",
10
);
}
// get cured
if (i % totalColumns === curedColIndex) {
let cured = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].recovered = parseInt(
cured.trim().replace(/,/g, "") || 0,
10
);
}
// get active
if (i % totalColumns === activeColIndex) {
let cured = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].active = parseInt(
cured.trim().replace(/,/g, "") || 0,
10
);
}
// get critical
if (i % totalColumns === criticalColIndex) {
let critical = cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].critical = parseInt(
critical.trim().replace(/,/g, "") || "0",
10
);
}
// get total cases per one million population
if (i % totalColumns === casesPerOneMillionColIndex) {
let casesPerOneMillion =
cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].casesPerOneMillion = parseInt(
casesPerOneMillion.trim().replace(/,/g, "") || "0",
10
);
}
// get total deaths per one million population
if (i % totalColumns === deathsPerOneMillionColIndex) {
let deathsPerOneMillion =
cell.children.length != 0 ? cell.children[0].data : "";
result[result.length - 1].deathsPerOneMillion = parseInt(
deathsPerOneMillion.trim().replace(/,/g, "") || "0",
10
);
}
}
db.set("countries", result);
console.log("Updated The Countries", result);
}, 150000);
app.get("/", async function(request, response) {
let a = await db.fetch("all");
response.send(
`${a.cases} cases are reported of the COVID-19 Novel Coronavirus strain<br> ${a.deaths} have died from it <br>\n${a.recovered} have recovered from it <br> Get the endpoint /all to get information for all cases <br> get the endpoint /countries for getting the data sorted country wise`
);
});
var listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});
app.get("/all/", async function(req, res) {
let all = await db.fetch("all");
res.send(all);
});
app.get("/countries/", async function(req, res) {
let countries = await db.fetch("countries");
res.send(countries);
});
app.get("/countries/:country", async function(req, res) {
let countries = await db.fetch("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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment