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

Add support for multiple pin codes #30

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 @@ -4,7 +4,7 @@ APPLICATION_PASSWORD=
AGE=

#Example:
#PINCODE=201301
#PINCODE=560099,560100
#[email protected]
#APPLICATION_PASSWORD=askdjklj11aklsj
#AGE=53
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "vaccineNotifier.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node startvaccineNotifier.js"
},
"author": "[email protected]",
"license": "ISC",
Expand Down
39 changes: 24 additions & 15 deletions vaccineNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,37 @@ async function checkAvailability() {
})
}

function getSlotsForDate(DATE) {
let config = {
const getConfig = (pincode, date) => {
return {
method: 'get',
url: 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=' + PINCODE + '&date=' + DATE,
url: 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=' + pincode.trim() + '&date=' + date,
headers: {
'accept': 'application/json',
'Accept-Language': 'hi_IN'
}
};
}

axios(config)
.then(function (slots) {
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) {
notifyMe(validSlots);
}
})
.catch(function (error) {
console.log(error);
});
async function getSlotsForDate(DATE) {
const PINCODES_ARR = PINCODE.split(',');

let promisesArr = PINCODES_ARR.map((pincode) => axios(getConfig(pincode, DATE))
.then(function (slots) {
let sessions = slots.data.sessions;
let validSlots = sessions.filter(slot => slot.min_age_limit <= AGE && slot.available_capacity > 0)
console.log({date:DATE, pincode, validSlots: validSlots.length})
if(validSlots.length > 0) {
return notifyMe(validSlots);
}
return Promise.resolve();
})
.catch(function (error) {
console.log(error);
return Promise.reject();
}));
const results = await Promise.all(promisesArr.map(p => p.catch(e => e)));
const invalidResults = results.filter(result => result instanceof Error);
invalidResults.forEach(e => console.error(e));
}

async function
Expand Down