Skip to content

Instantly share code, notes, and snippets.

@paramaggarwal
Last active May 24, 2021 08:35
Show Gist options
  • Save paramaggarwal/9664f386c3c34204ad6948db6b26fdd2 to your computer and use it in GitHub Desktop.
Save paramaggarwal/9664f386c3c34204ad6948db6b26fdd2 to your computer and use it in GitHub Desktop.
Node.js script to check for CoWin Vaccine API and speak available slots

Queries the API every 5 seconds and uses the macOS say command to announce the available slots.

{
"name": "vaccine",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Param Aggarwal",
"license": "ISC",
"dependencies": {
"superagent": "^6.1.0"
}
}
const superagent = require("superagent");
const { exec } = require("child_process");
const DISTRICT = 294; // find it on vaccineslot.in
const INTERVAL = 7; // seconds
const VACCINE = "COVAXIN"; // null: any or "COVISHIELD", "COVAXIN"
const WHEN = 1; // 0: today, 1: tomorrow
function run(command) {
return new Promise(function (resolve, reject) {
exec(command, (error, stdout) => {
if (error) return reject(error);
resolve(stdout.trim());
});
});
}
let announcements = Promise.resolve();
function check(date) {
return superagent
.get(
"https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict"
)
.query({
district_id: DISTRICT,
date: date,
})
.then((res) => {
return res.body.centers
.filter((c) => c.fee_type === "Paid")
.map((c) => {
return c.sessions
.filter((s) => !VACCINE || s.vaccine === VACCINE)
.filter((s) => s.available_capacity > 0)
.map((s) => ({
name: c.name,
age: s.min_age_limit,
dose1: s.available_capacity_dose1,
dose2: s.available_capacity_dose2,
}));
})
.flat();
})
.then((slots) => {
if (slots.length) {
slots.forEach((s) => {
if (s.age === 18) {
const message =
s.dose1 + " slots available at " + s.name + " for " + s.age + "+";
console.log(message);
announcements = announcements.then(() => run(`say "${message}"`));
} else if (s.age === 45) {
const message =
s.dose2 + " slots available at " + s.name + " for " + s.age + "+";
console.log(message);
announcements = announcements.then(() => run(`say "${message}"`));
}
});
} else {
console.log("Last checked at " + new Date().toLocaleString());
}
})
.catch((err) => {
console.error(err.status);
})
.then(() => {
setTimeout(() => {
check(date);
}, INTERVAL * 1000);
});
}
const date = new Date(Date.now() + WHEN * (1000 * 60 * 60 * 24))
.toLocaleString()
.slice(0, 10)
.replace("/", "-")
.replace("/", "-", "g");
console.log("Checking slots for " + date);
check(date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment