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

Added CLI support for API calling for 3 parameters AGE PINCODE AND DAYS #76

Open
wants to merge 4 commits into
base: main
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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PINCODE=
EMAIL=
APPLICATION_PASSWORD=
AGE=

QuUANTITY=1
#Example:
#PINCODE=201301
#[email protected]
Expand Down
58 changes: 58 additions & 0 deletions CLI/Vaccinator
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env node
const axios=require('axios');
const chalk=require('chalk');
const moment=require('moment');
const url = "https://quotes.rest/qod"
const getAvailability=require('../services/getAvailabilityonDate');
help =() => {
console.log(chalk.white(`./logger help # Use It for asking help \n
./logger AGE PINCODE NEXTDAYS # pass exactly 3 parameters in the given order for displaying vaccine availability based on the filter\n`));
}
errorLog=(error) => {
console.log(chalk.red(`${error}`));
}
// console.log(chalk.blue(`${(process.argv)[2]}`));

if((process.argv)[2]=="help") {
help();
}
callAPI = async (PINCODE,DAYS,QUANTITY, AGE) => {
let today=moment();
for(let i=0 ;i <10;i++) {
let DATE = today.format('DD-MM-YYYY');
let response = await getAvailability(PINCODE,DATE,QUANTITY,AGE);
if(response.status=='error') {
console.log(chalk.red('error occured try again!'));
}
else {
console.log(chalk.blue(`Date:${DATE}`));
const slots=response.data.validSlots;
if(slots.length == 0) {
console.log( chalk.bgRed("No free Slot Available"));

}
else {
console.log(chalk.yellow("Total Slots Available :\t")+chalk.bgBlue(slots.length));
slots.map((slot) => {
console.log(chalk.yellow("Vaccine :\t")+ chalk.green(slot.vaccine));
console.log(chalk.yellow("Fees :\t") + chalk.green(slot.fee));
console.log(chalk.yellow("Timings are : \t")+chalk.blue(slot.slots.length));
slot.slots.map((sl) => {
console.log(chalk.blue(sl));
});
});
}
}
today.add(1,'day');
}
}
if(process.argv.length==5) {
const DAYS=process.argv[4];
const AGE=process.argv[2];
const PINCODE=process.argv[3];
const QUANTITY=1;

callAPI(PINCODE,DAYS,QUANTITY,AGE);
}

// prompt verison I have to learn from here https://scotch.io/tutorials/building-cli-applications-with-nodejs. and implement it.'
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
"description": "",
"main": "vaccineNotifier.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node vaccineNotifier.js"
},
"author": "[email protected]",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^3.0.0",
"cron": "^1.8.2",
"dotenv": "^8.2.0",
"moment": "^2.29.1",
"node-cron": "^3.0.0",
"pm2": "^4.5.6"
"pm2": "^4.5.6",
"requests": "^0.3.0",
"save": "^2.4.0"
},
"bin": {
"outside": "bin/outside"
},
"devDependencies": {
"nodemailer": "^6.6.0"
Expand Down
26 changes: 26 additions & 0 deletions services/getAvailabilityonDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const axios=require('axios');
const request=require('request');
getAvailability = async (PINCODE,DATE,QUANTITY, AGE) => {
const config = {
method: 'get',
url: 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=' + PINCODE + '&date=' + DATE,
headers: {
'accept': 'application/json',
'Accept-Language': 'hi_IN',
"Access-Control-Allow-Origin": "*",
'Host': 'cdn-api.co-vin.in',
'User-Agent':'Axios 0.21.1'
}
};
try {
const slots=await axios(config);
const sessions = slots.data.sessions;
const validSlots = sessions.filter(slot => slot.min_age_limit <= AGE && slot.available_capacity >= QUANTITY)
return {status:'success',data:{date:DATE, validSlots: validSlots}};
}
catch(err) {
console.log(err);
return {status:'error'}
}
}
module.exports= getAvailability;
19 changes: 7 additions & 12 deletions vaccineNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ To close the app, run: pm2 stop vaccineNotifier.js && pm2 delete vaccineNotifier
const PINCODE = process.env.PINCODE
const EMAIL = process.env.EMAIL
const AGE = process.env.AGE

const QUANTITY=process.env.QUANTITY;
async function main(){
try {
cron.schedule('* * * * *', async () => {
cron.schedule('* * * * * *', async () => {
await checkAvailability();
});
} catch (e) {
Expand All @@ -30,7 +30,6 @@ async function main(){
}

async function checkAvailability() {

let datesArray = await fetchNext10Days();
datesArray.forEach(date => {
getSlotsForDate(date);
Expand All @@ -52,7 +51,7 @@ function getSlotsForDate(DATE) {
let sessions = slots.data.sessions;
let validSlots = sessions.filter(slot => slot.min_age_limit <= AGE && slot.available_capacity > 0)
console.log({date:DATE, validSlots: validSlots.length})
if(validSlots.length > 0) {
if(validSlots.length >= QUANTITY) {
notifyMe(validSlots);
}
})
Expand All @@ -61,18 +60,16 @@ function getSlotsForDate(DATE) {
});
}

async function

notifyMe(validSlots){
async function notifyMe(validSlots) {
let slotDetails = JSON.stringify(validSlots, null, '\t');
notifier.sendEmail(EMAIL, 'VACCINE AVAILABLE', slotDetails, (err, result) => {
if(err) {
console.error({err});
}
})
});
};

async function fetchNext10Days(){
async function fetchNext10Days() {
let dates = [];
let today = moment();
for(let i = 0 ; i < 10 ; i ++ ){
Expand All @@ -83,6 +80,4 @@ async function fetchNext10Days(){
return dates;
}


main()
.then(() => {console.log('Vaccine availability checker started.');});
main().then(() => {console.log('Vaccine availability checker started.');});