visit
Pre-requisite – We are assuming you have installed Node.js and npm, If not you can install from
So as a first step lets initialize our project using commandnpm init
#! /usr/bin/env node
"bin": {
"cowin": "./bin/index.js"
}
{
"name": "vaccli",
"version": "1.0.0",
"description": "CLI vaccination slots",
"main": "bin/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Nabheet",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^4.1.1",
"commander": "^7.2.0",
"inquirer": "^8.0.0",
"node-notifier": "^9.0.1",
"tty-table": "^4.1.3"
},
"bin": {
"cowin": "./bin/index.js"
}
}
Thank God for looking at site they have provided us with . 50% of work is done. Now, all we need to do is, consume this data and work as per our need. Let's now think about what our CLI will do.
npm install axios chalk commander inquirer node-notifier tty-table
// Common configuration data to be used by all functions.
exports.config = {
headers: { "User-Agent": "Axios - console app" },
};
exports.options = {
borderStyle: "solid",
borderColor: "blue",
headerAlign: "center",
align: "left",
color: "white",
truncate: "...",
width: "90%",
};
const axios = require('axios');
const table = require("tty-table");
const { config,options } = require('./config');
// function to return list of all states
module.exports = function() {
axios
.get("//cdn-api.co-vin.in/api/v2/admin/location/states", config)
.then((response) => {
// table formatter
let header = [
{
value: "state_id",
headerColor: "cyan",
alias: "State ID",
color: "white",
align: "left",
width: 40,
},
{
value: "state_name",
alias: "State",
headerColor: "cyan",
color: "white",
align: "left",
width: 40,
},
];
const out = table(header, response.data.states, options).render();
console.table(out);
})
.catch((error) => {
console.log(error);
});
};
const axios = require('axios');
const table = require("tty-table");
const { config,options } = require('./config');
// Function which take stateid as input and return all the formatted districts
module.exports = function(stateid) {
axios
.get(
`//cdn-api.co-vin.in/api/v2/admin/location/districts/${stateid}`,
config
)
.then((response) => {
// Table header specific formatting
let header = [
{
value: "district_id",
headerColor: "cyan",
alias: "District ID",
color: "white",
align: "left",
width: 40,
},
{
value: "district_name",
alias: "District",
headerColor: "cyan",
color: "white",
align: "left",
width: 40,
},
];
// Output the results.
const out = table(header, response.data.districts, options).render();
console.table(out);
})
.catch((error) => {
console.log(error);
});
};
const axios = require('axios');
const table = require("tty-table");
const chalk = require("chalk");
const notifier = require("node-notifier");
var inquirer = require("inquirer");
const { config,options } = require('./config');
// function to check slots.
module.exports = function(district) {
//Input prompt for getting what age you want to check records.
inquirer
.prompt([
{
type: "list",
name: "choice",
message: "Which Age group?",
choices: [
{
name: "View All",
value: "",
},
{
name: "45 Plus",
value: "45",
},
{
name: "18 - 45 ",
value: "18",
},
],
},
])
.then((answers) => {
const date = new Date();
var todaysDate = `${date.getDate()}-${String(
date.getMonth() + 1
).padStart(2, "0")}-${date.getFullYear()}`;
console.log(
chalk.underline.bgRed.bold(`Showing Slots from - ${todaysDate}`)
);
axios
.get(
`//cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=${district}&date=${todaysDate}`,
config
)
.then((response) => {
let finalData = [];
let districtName;
response.data.centers.forEach((item) => {
item.sessions.forEach((session) => {
districtName = item.district_name;
// based on user age choice filter the data
if (answers.choice == "") {
let data = {
Center: item.name,
Address: item.address,
Date: session.date,
FreeSlots: session.available_capacity,
Age: session.min_age_limit,
};
finalData.push(data);
} else if (
answers.choice == "18" &&
session.min_age_limit == "18"
) {
let data = {
Center: item.name,
Address: item.address,
Date: session.date,
FreeSlots: session.available_capacity,
Age: session.min_age_limit,
};
finalData.push(data);
} else if (
answers.choice == "45" &&
session.min_age_limit == "45"
) {
let data = {
Center: item.name,
Address: item.address,
Date: session.date,
FreeSlots: session.available_capacity,
Age: session.min_age_limit,
};
finalData.push(data);
}
});
});
console.log(
chalk.underline.bgGreen.bold(`District - ${districtName}`)
);
switch (answers.choice) {
case "":
console.log(chalk.underline.bgBlue.bold(`All ages`));
break;
case "45":
console.log(chalk.underline.bgBlue.bold(`45+ Age`));
break;
case "18":
console.log(chalk.underline.bgBlue.bold(`18-45 Age`));
break;
default:
break;
}
// table formatting
let header = [
{
value: "Center",
headerColor: "cyan",
color: "white",
align: "left",
width: 40,
},
{
value: "Address",
headerColor: "cyan",
color: "white",
align: "left",
width: 40,
},
{
value: "Date",
headerColor: "cyan",
color: "white",
align: "left",
width: 15,
},
{
value: "FreeSlots",
headerColor: "cyan",
color: "white",
align: "left",
width: 20,
},
{
value: "Age",
headerColor: "cyan",
color: "white",
align: "left",
width: 20,
},
];
const out = table(header, finalData, options).render();
console.table(out);
notifier.notify({
title: "Vaccination Slots Available",
subtitle: "Daily Maintenance",
message: "Immediately go and check Vaccination slots!",
wait: true,
});
})
.catch((error) => {
console.log(error);
});
})
.catch((error) => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
};
#! /usr/bin/env node
const program = require("commander");
// import all functions
const districts = require('../util/districts');
const states = require('../util/states');
const slots = require('../util/slots');
// adding different cli options,commands and passing callback functions in actions
program.option("-a, --available", "Output If slots available");
program
.command("states")
.description("Get all State Codes and descriptions.")
.action(states);
program
.command("district <stateid>")
.description("Get all district of a State")
.action(districts);
program
.command("slots <districtid>")
.description("Get slots for the district")
.action(slots);
program.parse();
npm install -g .
cowin states
cowin districts 12
cowin slots 187
Previously published at