This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
app.js
executable file
·191 lines (181 loc) · 9.48 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
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
const axios = require('axios')
const argv = require('minimist')(process.argv.slice(2));
const { format } = require('date-fns');
const isMatch = require('date-fns/isMatch')
const sound = require("sound-play");
const path = require("path");
const notificationSound = path.join(__dirname, "sounds/beep.wav");
const defaultInterval = 10; // interval between pings in minutes
const appointmentsListLimit = 2 // Increase/Decrease it based on the amount of information you want in the notification.
const defaultKeepAlive = false;
const defaultType = 'all';
let timer = null;
const sampleUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
const baseUrl = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/'
checkParams();
function checkParams() {
if (argv.help) {
console.error('Refer documentation for more details');
} else if (argv._ && argv._.length && argv._.includes('run')) {
if (argv.key && typeof argv.key !== 'string') {
console.error('Please provide a valid IFTTT Webook API Key by appending --key=<IFTTT-KEY> to recieve mobile notification \nRefer documentation for more details');
return;
} else if (argv.hook && typeof argv.hook !== 'string') {
console.error('Please provide a valid IFTTT Webook Name Key by appending --hook=<IFTTT-WEBHOOK-NAME> to recieve mobile notification \nRefer documentation for more details');
return;
} else if (argv.hook && !argv.key || !argv.hook && argv.key) {
console.error('Please provide both IFTTT Webook Name Key and IFTTT Webhook Key to recieve mobile notification \nRefer documentation for more details');
return;
} else if (!argv.age) {
console.error('Please provide your age by appending --age=<YOUR-AGE> \nRefer documentation for more details');
return;
} else if (argv.age && argv.age < 18) {
console.error('Please provide an age greater than or equal to 18');
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.interval && argv.interval < 1) {
// these APIs are subject to a rate limit of 100 API calls per 5 minutes per IP
console.error('Please provide an interval greater than or equal to 1 minutes');
return;
} else if (argv.date && !isMatch(argv.date, 'dd-MM-yyyy')) {
console.error('Please provide date in dd-mm-yyyy format');
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' && argv.vaccine.toLowerCase() !== 'sputnik' )) {
console.error('Please provide vaccine param as COVAXIN or COVISHIELD or SPUTNIK');
return;
}
else if ((argv['keep-alive'] && typeof argv['keep-alive'] !== 'string') && (argv['keep-alive'].toLowerCase() !== 'true' && argv['keep-alive'].toLowerCase() !== 'false')) {
console.error('Please set keep-alive param as true or false');
return;
} else if (
argv['type'] &&
typeof argv['type'] !== 'string' &&
argv['type'].toLowerCase() !== 'free' &&
argv['type'].toLowerCase() !== 'paid' &&
argv['type'].toLowerCase() !== 'all'
) {
console.error('Please set type param as free, paid or all');
return;}
else {
const params = {
vaccine: argv.vaccine === 'spuntik' ? argv.vaccine + ' v' : argv.vaccine, // vaccine = COVISHIELD , COVAXIN, SPUTNIK
dose: argv.dose, // dose = 1, 2
key: argv.key,
hook: argv.hook,
age: argv.age,
districtId: argv.district,
interval: argv.interval || defaultInterval,
appointmentsListLimit: argv.appts || appointmentsListLimit,
date: argv.date,
pin: argv.pin,
keepAlive: argv['keep-alive'] ? argv['keep-alive'].toLowerCase() === 'true' : defaultKeepAlive,
type: argv['type'] ? argv['type'].toLowerCase() : defaultType,
}
console.log('\nCowin Pinger started succesfully\n');
console.log(`Date= ${params.date || format(new Date(), 'dd-MM-yyyy')}`);
console.log(`Age= ${params.age}`);
console.log(`Dose= ${params.dose === 1 ? 'First Dose' : 'Second Dose'}`);
params.vaccine && console.log(`Vaccine= ${params.vaccine.toUpperCase()}`);
params.type && console.log(`Type= ${params.type.toUpperCase()}`);
if (params.pin) {
console.log(`Pincode= ${params.pin}`);
} else {
console.log(`District ID= ${params.districtId}`);
}
console.log(`Time interval= ${params.interval} minutes (default is 10)`);
console.log(`Appointment Count= ${params.appointmentsListLimit} (default is 2)`);
if (params.hook && params.key) {
console.log(`IFTTT API Key= ${params.key || "not configured"}`);
console.log(`IFTTT Hook Name= ${params.hook || "not configured"}`);
} else {
console.log('\nMake sure to turn up the volume to hear the notifcation sound')
}
console.log('\n\n')
scheduleCowinPinger(params);
}
} else {
console.log('\nInvalid command\n\nRun `cowin-pinger run` with all required params to start pinging cowin portal\nRefer documentation for instructions on how to run package\n');
}
}
function scheduleCowinPinger(params) {
let pingCount = 0;
timer = setInterval(() => {
console.clear();
pingCount += 1;
pingCowin(params);
console.log("Ping Count - ", pingCount);
}, params.interval * 60000);
}
function pingCowin({ key, hook, age, districtId, appointmentsListLimit, date, pin, vaccine, dose, keepAlive, type }) {
// get current date on every iteration if not custom date
date = date || format(new Date(), 'dd-MM-yyyy')
let url = pin ? `${baseUrl}calendarByPin?pincode=${pin}&date=${date}` : `${baseUrl}calendarByDistrict?district_id=${districtId}&date=${date}`
const ageLimit = age >= 18 && age < 45 ? 18 : 45;
axios.get(url, { headers: { 'User-Agent': sampleUserAgent } }).then((result) => {
const { centers } = result.data;
let isSlotAvailable = false;
let dataOfSlot = "";
let appointmentsAvailableCount = 0;
if (centers.length) {
centers.forEach(center => {
if (type !== center.fee_type || type !== 'all') {
return;
}
center.sessions.forEach((session => {
if (session.min_age_limit === ageLimit && session.available_capacity > 0) {
if (dose === 1 && session.available_capacity_dose1 <= 0) {
return;
}
if (dose === 2 && session.available_capacity_dose2 <= 0) {
return;
}
if (vaccine && vaccine.toLowerCase() !== session.vaccine.toLowerCase()) {
return;
}
isSlotAvailable = true
appointmentsAvailableCount++;
if (appointmentsAvailableCount <= appointmentsListLimit) {
dataOfSlot = `${dataOfSlot}\n[${center.pincode}] - Slot for ${session.available_capacity} is available: ${center.name} on ${session.date}`;
}
}
}))
});
if ((appointmentsAvailableCount - appointmentsListLimit) > 0) {
dataOfSlot = `${dataOfSlot}\n${appointmentsAvailableCount - appointmentsListLimit} more slots available...`
}
}
if (isSlotAvailable) {
if (hook && key) {
axios.post(`https://maker.ifttt.com/trigger/${hook}/with/key/${key}`, { value1: dataOfSlot }).then(() => {
console.log(dataOfSlot);
sound.play(notificationSound);
console.log('Sent Notification to Phone')
if (!keepAlive) {
console.log('Stopping Pinger...')
clearInterval(timer);
}
});
} else {
console.log(dataOfSlot);
sound.play(notificationSound, 1);
console.log('Slots found')
if (!keepAlive) {
console.log('Stopping Pinger...')
clearInterval(timer);
}
}
}
}).catch((err) => {
console.log("Error: " + err.message);
});
}