Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 NEW: Added vaccines chart with -v as option. Also changed the api version to v3 #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ const getCountryChart = require('./utils/getCountryChart.js');
const getBar = require('./utils/getBar.js');
const getWorldwide = require('./utils/getWorldwide.js');
const getCountries = require('./utils/getCountries.js');
const getVaccines = require('./utils/getVaccines');
const {
style,
single,
colored,
singleStates,
coloredStates,
borderless
borderless,
vaccinesTable,
coloredVaccinesTable
} = require('./utils/table.js');

// Cli.
Expand All @@ -40,33 +43,41 @@ const log = cli.flags.log;
const bar = cli.flags.bar;
const minimal = cli.flags.minimal;
const json = cli.flags.json;
const options = { sortBy, limit, reverse, minimal, chart, log, json, bar };
const vaccines = cli.flags.vaccines;
const options = { sortBy, limit, reverse, minimal, chart, vaccines, log, json, bar };

(async () => {
// Init.
await init(minimal || json);
const spinner = ora({ text: '' });
input === 'help' && (await cli.showHelp(0));
const states = input === 'states' ? true : false;
const states = input === 'states';
const country = states ? '' : input;

// Table
const head = xcolor ? single : colored;
const headStates = xcolor ? singleStates : coloredStates;
const headVaccines = xcolor ? vaccinesTable : coloredVaccinesTable;
const border = minimal ? borderless : {};
const OutputFormat = json ? JsonOutput : Table;
const output = !states
? new OutputFormat({ head, style, chars: border })
: new OutputFormat({ head: headStates, style, chars: border });
let output;
if(states){
output = new OutputFormat({ head: headStates, style, chars: border });
}else if(vaccines){
output = new OutputFormat({ head: headVaccines, style, chars: border });
}else{
output = new OutputFormat({ head, style, chars: border });
}

// Display data.
spinner.start();
const lastUpdated = await getWorldwide(output, states, json);
const lastUpdated = await getWorldwide(output, states,vaccines, json);
await getCountry(spinner, output, states, country, options);
await getStates(spinner, output, states, options);
await getVaccines(spinner, output, options);
await getCountries(spinner, output, states, country, options);
await getCountryChart(spinner, country, options);
await getBar(spinner, country, states, options);

theEnd(lastUpdated, states, minimal || json);
theEnd(lastUpdated, states, vaccines ,minimal || json);
})();
7 changes: 7 additions & 0 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = meow(
${yellow(`-g`)}, ${yellow(`--log`)} Print logarithmic chart
${yellow(`-x`)}, ${yellow(`--xcolor`)} Single colored output
${yellow(`-m`)}, ${yellow(`--minimal`)} Minimalistic CLI output
${yellow(`-v`)}, ${yellow(`--vaccine`)} Prints current vaccine data
${yellow(`-j`)}, ${yellow(`--json`)} Output JSON only data

Examples
Expand All @@ -28,6 +29,7 @@ module.exports = meow(
${green(`corona`)} ${cyan(`china`)} ${yellow(`--chart`)}
${green(`corona`)} ${cyan(`china`)} ${yellow(`--chart`)} ${yellow(`--log`)}
${green(`corona`)} ${yellow(`--sort`)} ${cyan(`cases-today`)}
${green(`corona`)} ${yellow(`-v`)}
${green(`corona`)} ${yellow(`-s`)} ${cyan(`critical`)}

❯ You can also run command + option at once:
Expand Down Expand Up @@ -78,6 +80,11 @@ module.exports = meow(
default: false,
alias: 'm'
},
vaccines: {
type: 'boolean',
default: false,
alias: 'v'
},
json: {
type: 'boolean',
default: false,
Expand Down
4 changes: 2 additions & 2 deletions utils/getBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = async (
logScale = x => (x === 0 ? undefined : Math.log(x));
}

const statesURL = `https://corona.lmao.ninja/v2/states`;
const countriesURL = `https://corona.lmao.ninja/v2/countries`;
const statesURL = `https://corona.lmao.ninja/v3/covid-19/states`;
const countriesURL = `https://corona.lmao.ninja/v3/covid-19/countries`;

const [err, res] = await to(
axios.get(states ? statesURL : countriesURL)
Expand Down
7 changes: 3 additions & 4 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module.exports = async (
output,
states,
countryName,
{ sortBy, limit, reverse, bar, json }
{ sortBy, limit, reverse, bar, vaccines, json }
) => {
if (!countryName && !states && !bar) {
if (!countryName && !states && !bar && !vaccines) {
sortValidation(sortBy, spinner);
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/countries`)
axios.get(`https://corona.lmao.ninja/v3/covid-19/countries`)
);
handleError(`API is down, try again later.`, err, false);
let allCountries = response.data;
Expand All @@ -35,7 +35,6 @@ module.exports = async (

// Limit.
allCountries = allCountries.slice(0, limit);

// Push selected data.
allCountries.map((oneCountry, count) => {
output.push([
Expand Down
2 changes: 1 addition & 1 deletion utils/getCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const handleError = require('cli-handle-error');
module.exports = async (spinner, table, states, countryName, options) => {
if (countryName && !states && !options.chart) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/countries/${countryName}`)
axios.get(`https://corona.lmao.ninja/v3/covid-19/countries/${countryName}`)
);
exitCountry(err, spinner, countryName);
err && spinner.stopAndPersist();
Expand Down
2 changes: 1 addition & 1 deletion utils/getCountryChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const contrib = require('blessed-contrib');
module.exports = async (spinner, countryName, { chart, log }) => {
if (countryName && chart) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/historical/${countryName}`)
axios.get(`https://corona.lmao.ninja/v3/covid-19/historical/${countryName}`)
);
handleError(`API is down, try again later.`, err, false);
if (response.status === 404) {
Expand Down
2 changes: 1 addition & 1 deletion utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = async (
if (states && !bar) {
sortStatesValidation(sortBy, spinner);
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/states`)
axios.get(`https://corona.lmao.ninja/v3/covid-19/states`)
);
handleError(`API is down, try again later.`, err, false);
let allStates = response.data;
Expand Down
6 changes: 3 additions & 3 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const numberFormat = require('./numberFormat');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');

module.exports = async (table, states, json) => {
module.exports = async (table, states, vaccines, json) => {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/all`)
axios.get(`https://corona.lmao.ninja/v3/covid-19/all`)
);
handleError(`API is down, try again later.`, err, false);

const allData = response.data;
const format = numberFormat(json);

// Don't print coz for states we still need that data of updated data.
if (!states) {
if (!states && !vaccines) {
table.push([
`→`,
`Worldwide`,
Expand Down
20 changes: 20 additions & 0 deletions utils/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ module.exports = {
`Critical`,
`Per Million`
],
vaccinesTable: [
`#`,
`Candidate`,
`Mechanism`,
`TrialPhase`,
`Institutions`,
],
coloredVaccinesTable: [
`#`,
`Candidate`,
`${red(`Mechanism`)}`,
`${green(`TrialPhase`)}`,
`${yellow(`Institutions`)}`,
],
colored: [
`#`,
`Country`,
Expand Down Expand Up @@ -86,5 +100,11 @@ module.exports = {
deaths: 'deaths',
'deaths-today': 'todayDeaths',
active: 'active'
},
sortingVaccineKeys:{
candidate: 'candidate',
mechanism: 'mechanism',
trialPhase: 'trialPhase',
institutions: 'institutions'
}
};
22 changes: 19 additions & 3 deletions utils/theEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,27 @@ ${dim(`❯ `)}${cyan(`Per Million:`)} Affected patients per million
`)
);

module.exports = async (lastUpdated, states, minimal) => {
const infoVaccins = () =>
console.log(
dim(`
\n${sym.info} ${cyan(`KEY:`)}
${dim(`❯ `)}${cyan(`Candidate:`)} Name of the vaccine
${dim(`❯ `)}${cyan(`Mechanism:`)} What mechanism the vaccine uses
${dim(`❯ `)}${cyan(`TrialPhase:`)} Current trialPhase of vaccine
${dim(`❯ `)}${cyan(`Institutions:`)} Institution which tests the vaccine
`)
);

module.exports = async (lastUpdated, states, vaccines, minimal) => {
if (minimal) return console.log();
console.log(dim(`${sym.info} ${cyan(`Last Updated:`)} ${lastUpdated}`));
states && infoStates();
!states && infoCountries();
if(states){
infoStates();
}else if(vaccines){
infoVaccins();
}else{
infoCountries();
}
console.log(
`\n${sym.success} ${dim(
`Star the repo for updates → https://git.io/corona-cli`
Expand Down