-
Notifications
You must be signed in to change notification settings - Fork 333
/
vaccineNotifier.js
88 lines (74 loc) · 2.41 KB
/
vaccineNotifier.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
require('dotenv').config()
const moment = require('moment');
const cron = require('node-cron');
const axios = require('axios');
const notifier = require('./notifier');
/**
Step 1) Enable application access on your gmail with steps given here:
https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor&visit_id=637554658548216477-2576856839&rd=1
Step 2) Enter the details in the file .env, present in the same folder
Step 3) On your terminal run: npm i && pm2 start vaccineNotifier.js
To close the app, run: pm2 stop vaccineNotifier.js && pm2 delete vaccineNotifier.js
*/
const PINCODE = process.env.PINCODE
const EMAIL = process.env.EMAIL
const AGE = process.env.AGE
async function main(){
try {
cron.schedule('* * * * *', async () => {
await checkAvailability();
});
} catch (e) {
console.log('an error occured: ' + JSON.stringify(e, null, 2));
throw e;
}
}
async function checkAvailability() {
let datesArray = await fetchNext10Days();
datesArray.forEach(date => {
getSlotsForDate(date);
})
}
function getSlotsForDate(DATE) {
let config = {
method: 'get',
url: 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=' + PINCODE + '&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
notifyMe(validSlots){
let slotDetails = JSON.stringify(validSlots, null, '\t');
notifier.sendEmail(EMAIL, 'VACCINE AVAILABLE', slotDetails, (err, result) => {
if(err) {
console.error({err});
}
})
};
async function fetchNext10Days(){
let dates = [];
let today = moment();
for(let i = 0 ; i < 10 ; i ++ ){
let dateString = today.format('DD-MM-YYYY')
dates.push(dateString);
today.add(1, 'day');
}
return dates;
}
main()
.then(() => {console.log('Vaccine availability checker started.');});