-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCowinApp.js
104 lines (91 loc) · 3.15 KB
/
CowinApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const https = require('https');
const say = require('say');
const { exec } = require("child_process");
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
const makeHitEveryXSecs = 10;
function makeApiHit(pincode, age) {
let date = GetTomorrowDate();
console.log('Hitting the API');
console.log(`https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pincode}&date=${date}`)
https.get(`https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pincode}&date=${date}`, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let availableSlots = GetAvailableSlots(data);
let slotsForEnteredAge = availableSlots.filter(s => s.age == age);
console.log(slotsForEnteredAge)
if(slotsForEnteredAge.length > 0) {
notify(slotsForEnteredAge, age);
setInterval(()=> {
notify(slotsForEnteredAge, age);
}, 6000);
}
else {
console.log('Hitting again');
setTimeout(() => {
makeApiHit(pincode, age);
}, makeHitEveryXSecs*1000);
}
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
function notify(slotsForEnteredAge, age) {
if(slotsForEnteredAge.length > 0) {
let message = `Found ${slotsForEnteredAge.length} slots for ${age} years old, please check`;
say.speak(message);
}
}
function GetAvailableSlots(data) {
let availableSlots = [];
let cowinData = JSON.parse(data);
let centers = cowinData.centers;
let sessionCount = 0;
for(let i = 0; i < centers.length; i++) {
let center = centers[i];
if(center.sessions != undefined && center.sessions != null) {
for(let j = 0; j < center.sessions.length; j++) {
let session = center.sessions[j];
if(session.available_capacity > 0) {
availableSlots.push({
centerName: center.name,
address: center.address,
capacity: session.available_capacity,
age: session.min_age_limit,
vaccine: session.vaccine,
slots: session.slots
});
}
sessionCount++;
}
}
}
console.log(`Centers processed : ${centers.length}, sessions processed: ${sessionCount}`)
//availableSlots = [{age: 18}, {age: 18}, {age: 45}]
return availableSlots;
}
function GetTomorrowDate() {
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
const day = tomorrow.getDate();
const month = tomorrow.getMonth() + 1;
const year = tomorrow.getFullYear();
console.log(day + "-" + month + "-" + year);
return day + "-" + month + "-" + year;
}
readline.question(`Enter PinCode:`, pin => {
readline.question(`Enter Age:`, age => {
readline.close()
// Start
makeApiHit(pin, age);
});
});