-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
180 lines (168 loc) · 7.36 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
const request = require("request");
const argv = require('minimist')(process.argv.slice(2));
const sound = require("sound-play");
const path = require("path");
const { format } = require('date-fns');
const notificationSound = path.join(__dirname, "alert-sound/beep.wav");
var noOfAttempt = 0;
var timeoutCount = 0;
console.log(
`
#################################
### COWIN NOTIFIER ###
### Version: 1.0.8 ###
### Author: Divyansh Singh ###
#################################
`
);
checkParams();
function checkParams() {
if (argv.help) {
console.error('Refer documentation for more details');
} else if (argv._ && argv._.length && argv._.includes('run')) {
if (!argv.age) {
console.error('Please provide your age by appending --age=<YOUR-AGE> \nRefer documentation for more details');
return;
} else if (!argv.district && !argv.pin) {
console.error('Please provide either district-id or pincode by appending --district=<DISTRICT-ID> or --pin=<PINCODE> \nRefer documentation for more details');
return;
} else if (argv.pin && argv.pin.toString().length !== 6) {
console.error('Pincode must be a 6 digit number \nRefer documentation for more details');
return;
} else if (!argv.dose || (argv.dose && argv.dose !== 1 && argv.dose !== 2)) {
console.error('Please mention if your require first dose or second dose by passing --dose=1 or --dose=2 \n');
return;
}
else if ((argv.vaccine && typeof argv.vaccine !== 'string') || (argv.vaccine && argv.vaccine.toLowerCase() !== 'covishield' && argv.vaccine.toLowerCase() !== 'covaxin')) {
console.error('Please provide vaccine param as COVISHIELD or COVAXIN');
return;
}
else if (argv.mob && argv.mob.toString().length !== 10) {
console.error('mobile no. must be a 10 digit number \nRefer documentation for more details');
return;
}
else if ((argv.cost && typeof argv.cost !== 'string') || (argv.cost && argv.cost.toLowerCase() !== 'free' && argv.cost.toLowerCase() !== 'paid')){
console.error('Please provide cost param as FREE or PAID');
return;
}
else if ((argv.keep_searching && typeof argv.keep_searching !== 'string') || (argv.keep_searching && argv.keep_searching.toLowerCase() !== 'true' && argv.keep_searching.toLowerCase() !== 'false')){
console.error('Please provide keep_searching param as True or False');
return;
}
else {
const params = {
vaccine: argv.vaccine,
dose: argv.dose,
age: argv.age,
districtId: argv.district,
date: format(new Date(), 'dd-MM-yyyy'),
pin: argv.pin,
cost: argv.cost,
keep_searching: argv.keep_searching
}
console.log('\nCowin vaccine availability notifier started succesfully\n');
console.log(`Date = ${params.date}`);
console.log(`Age = ${params.age}`);
console.log(`Dose = ${params.dose === 1 ? 'First Dose' : 'Second Dose'}`);
params.vaccine && console.log(`Vaccine = ${params.vaccine.toUpperCase()}`);
params.cost && console.log(`Cost = ${params.cost.toUpperCase()}`);
if (params.pin) {
console.log(`Pincode = ${params.pin}`);
} else {
console.log(`District ID = ${params.districtId}`);
}
console.log('\nMake sure to turn up the volume to hear the notifcation sound')
console.log('\n\n')
checkVaccineAvailabilty(params);
}
} else {
console.log('\nInvalid command\n\nRun `cowin-notifier run` with all required params to start pinging cowin portal\nRefer documentation for instructions on how to run package\n');
}
}
var getOTP = function(){
const apiBaseURL = 'https://cdn-api.co-vin.in/api/v2/auth/public/';
var options = {
method: 'POST',
url: apiBaseURL+"generateOTP",
headers:
{
'Content-Type': 'application/json'
},
body: { mobile: argv.mob },
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log("OTP sent. Check your phone.")
});
}
function checkVaccineAvailabilty({ age, districtId, date, pin, vaccine, dose, cost }){
var availableCenters = {
count: 0,
centers: []
};
const apiBaseUrl = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/'
let url = pin ? `${apiBaseUrl}calendarByPin?pincode=${pin}&date=${date}` : `${apiBaseUrl}calendarByDistrict?district_id=${districtId}&date=${date}`
let isSlotAvailable = false;
var options = {
method: 'GET',
url: url,
headers:
{
'Content-Type': 'application/json'
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
if( body.centers ){
for(var i = 0; i < body.centers.length; i++){
var center = body.centers[i];
for(var j = 0; j < center.sessions.length; j++){
var session = center.sessions[j];
if( session.min_age_limit <= age && session.available_capacity > 0){
if( (dose === 1 && session.available_capacity_dose1 > 0) || (dose === 2 && session.available_capacity_dose2 > 0) ){
if( !vaccine || ( vaccine && vaccine.toLowerCase() == session.vaccine.toLowerCase() ) ){
if( !cost || ( cost && cost.toLowerCase() == center.fee_type.toLowerCase() ) ){
isSlotAvailable = true;
availableCenters.centers.push({
name: center.name,
address: center.address,
cost: center.fee_type,
date: session.date,
available_capacity_dose1: session.available_capacity_dose1,
available_capacity_dose2: session.available_capacity_dose2,
vaccine: session.vaccine
});
availableCenters.district = center.district_name;
availableCenters.count++;
}
}
}
}
}
}
if( isSlotAvailable == true ){
console.log(`Slot(s) found at below centers with availabilty as of : ${format( new Date(), "dd-MM-yyyy hh:mm:ss")}`);
console.log(availableCenters);
notify( { age, districtId, date, pin, vaccine, dose, cost } );
}
else {
noOfAttempt++;
console.log("fetching again.. attempt no. " + noOfAttempt);
setTimeout(() => { checkVaccineAvailabilty({ age, districtId, date, pin, vaccine, dose, cost }) }, 30000); // call with 30 seconds delay.
}
}
else{
timeoutCount++;
console.log("timeoutCount:", timeoutCount);
setTimeout(() => { checkVaccineAvailabilty({ age, districtId, date, pin, vaccine, dose, cost }) }, 300000); // call with 5 mints delay.
}
})
}
notify = function ( { age, districtId, date, pin, vaccine, dose, cost } ){
sound.play(notificationSound);
if(argv.mob) getOTP();
if(argv.keep_searching && argv.keep_searching.toLowerCase() == "true") setTimeout(() => { checkVaccineAvailabilty({ age, districtId, date, pin, vaccine, dose, cost }) }, 30000);
}