diff --git a/.env b/.env index 5c482aa..2766bde 100644 --- a/.env +++ b/.env @@ -2,7 +2,7 @@ PINCODE= EMAIL= APPLICATION_PASSWORD= AGE= - +QuUANTITY=1 #Example: #PINCODE=201301 #EMAIL=mytestemail@gmail.com diff --git a/CLI/Vaccinator b/CLI/Vaccinator new file mode 100755 index 0000000..9aa266d --- /dev/null +++ b/CLI/Vaccinator @@ -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.' \ No newline at end of file diff --git a/package.json b/package.json index 158624e..f199cf9 100644 --- a/package.json +++ b/package.json @@ -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": "sharmakartikey54@gmail.com", "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" diff --git a/services/getAvailabilityonDate.js b/services/getAvailabilityonDate.js new file mode 100644 index 0000000..98d1b0b --- /dev/null +++ b/services/getAvailabilityonDate.js @@ -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; \ No newline at end of file diff --git a/vaccineNotifier.js b/vaccineNotifier.js index 72f7043..2e1f202 100644 --- a/vaccineNotifier.js +++ b/vaccineNotifier.js @@ -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) { @@ -30,7 +30,6 @@ async function main(){ } async function checkAvailability() { - let datesArray = await fetchNext10Days(); datesArray.forEach(date => { getSlotsForDate(date); @@ -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); } }) @@ -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 ++ ){ @@ -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.');});