-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
263 lines (209 loc) · 8.67 KB
/
app.js
File metadata and controls
263 lines (209 loc) · 8.67 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const nodemailer = require('nodemailer');
const cron = require('node-cron');
const moment = require('moment');
const path = require('path');
const mongodb = require('mongodb');
const https = require('https');
var dbConn = mongodb.MongoClient.connect('mongoDB Atlas account info');
let userData;
let deletedIds=[];
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/public/html/home.html");
});
app.get("/contactus", function (req, res) {
res.sendFile(__dirname + "/public/html/contact-me.html");
});
app.get("/notify", function (req, res) {
res.sendFile(__dirname + "/public/html/vaccinenotify.html");
})
app.get("/vaccine-info", function (req, res) {
res.sendFile(__dirname + "/public/html/vaccine_info.html");
})
app.get("/unsubscribe",function(req,res){
res.sendFile(__dirname + "/public/html/unsubscribe.html");
})
app.post("/success",function(req,res){
res.redirect("/");
})
// app.post("/failure",function(req,res){
// res.redirect("/notify");
// })
app.post('/unsubscribed-successfully',function(req,res){
mongodb.MongoClient.connect('mongoDB Atlas account info',(err,client) => {
var db = client.db('db_name');
delete req.body._id; // for safety reasons
db.collection('db').deleteMany({
"email" : req.body.email
});
});
console.log(req.body.email);
console.log(typeof(req.body.email));
res.sendFile(__dirname + "/public/html/unsubscribed-successfully.html");
});
app.post('/post-feedback', function (req, res) {
mongodb.MongoClient.connect('mongoDB Atlas account info', (err, client) => {
var db = client.db('db_name');
delete req.body._id; // for safety reasons
db.collection('db').insertOne(req.body);
});
console.log(JSON.stringify(req.body));
res.sendFile(__dirname + "/public/html/successfulRegistration.html");
});
async function main() {
try {
cron.schedule('* * * * *', async () => {
checkAvailability();
});
} catch (e) {
console.log('an error occured: ' + JSON.stringify(e, null, 2));
throw e;
}
}
async function checkAvailability() {
let datesArray = await fetchNext1Days();
await getUserData();
// console.log("I'm here");
// console.log(userData);
if(typeof userData !== 'undefined'){
for(let i=0;i<userData.length;i++){
datesArray.forEach(date =>{
let ok = getSlotsForDate(date,userData[i]);
});
}
}
}
async function fetchNext1Days() {
let dates = [];
let today = moment();
for (let i = 0; i < 1; i++) {
let dateString = today.format('DD-MM-YYYY')
dates.push(dateString);
today.add(1, 'day');
}
return dates;
}
async function getUserData() {
mongodb.MongoClient.connect('mongoDB Atlas account info', (err, client) => {
var db = client.db('db_name');
db.collection('db').find({}).toArray().then(function (feedbacks) {
userData = (feedbacks);
console.log(userData);
// console.log(typeof(userData));
});
});
// return userData;
}
function getSlotsForDate(DATE, element) {
let ok = false;
console.log(element.district);
var options = {
'method': 'GET',
'url': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id='+ Number(element.district) + '&date='+DATE,
'headers': {
'Host': 'cdn-api.co-vin.in',
'Accept-Language': 'en_US',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
};
request(options, function(error,response){
if (error) {
// console.log(typeof(error));
throw new Error(error);
}
let validSlots = [];
let data = JSON.parse(response.body);
//var str = response.body;
// let str=response.body;
// str=str.trim();
// let data = JSON.parse(JSON.stringify(str));
// console.log(data);
console.log(typeof(data));
for(var i = 0; i < data.centers.length; ++i) {
for (var j = 0; j < data.centers[i].sessions.length; ++j) {
let availability = data.centers[i].sessions[j].available_capacity;
let minAge = data.centers[i].sessions[j].min_age_limit;
// console.log(availability);
if (availability > 0) {
validSlots.push(data.centers[i]);
}
}
}
console.log(validSlots.length);
if(validSlots.length > 0) {
console.log("Vaccination Centres Found");
notifyMe(validSlots, element.email);
}
});
// return ok;
}
async function notifyMe(validSlots, email) {
let slotDetails = JSON.stringify(validSlots, null, '\t');
//console.log(validSlots[0]);
let html=`<h5>Vaccination Centres : </h5>`;
html+=`<p>If you want to unsubscribe click on the link below the table. </p>`;
html+=`<table style="border-collapse: collapse; width: 75%;">
<thead style=" padding-top: 12px; padding-bottom: 12px; text-align: center; background-color: #009879; color: white; ">
<th style="border: 1px solid #ddd; padding: 8px;">Centre Name</th>
<th style="border: 1px solid #ddd; padding: 8px;">Vaccine</th>
<th style="border: 1px solid #ddd; padding: 8px;">Address</th>
<th style="border: 1px solid #ddd; padding: 8px;">PinCode</th>
<th style="border: 1px solid #ddd; padding: 8px;">Date</th>
<th style="border: 1px solid #ddd; padding: 8px;">Availability Dose 1</th>
<th style="border: 1px solid #ddd; padding: 8px;">Availability Dose 2</th>
<th style="border: 1px solid #ddd; padding: 8px;">Minimum Age</th>
<th style="border: 1px solid #ddd; padding: 8px;">Fee Type</th>
</thead>
<tbody id='data-body'>`;
for(let i=0;i<validSlots.length;i++){
for(let j=0;j<validSlots[i].sessions.length;j++){
if(validSlots[i].sessions[j].available_capacity===0)continue;
html+=`<tr style=" padding-top: 12px; padding-bottom: 12px; text-align: center;">`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].name+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].sessions[j].vaccine+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].address+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].pincode+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].sessions[j].date+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].sessions[j].available_capacity_dose1+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].sessions[j].available_capacity_dose2+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].sessions[j].min_age_limit+`</td>`;
html+=`<td style="border: 1px solid #ddd; padding: 8px;">`+validSlots[i].fee_type+`</td>`;
html+=`</tr>`;
}
}
html+=`</tbody>`;
html+=`</table>`;
html+=`<a href="https://vaccine-info-2021.herokuapp.com/unsubscribe">Click here to unsubscribe</a>`;
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'vaccineinfo2021@gmail.com',
pass: 'password'
}
});
var mailOptions = {
from: 'vaccineinfo2021@gmail.com',
to: email,
subject: 'Vaccine Available-Hurry Up! Book Fast',
html: html
};
// text:'Following Slots are Available Right now! Please Book Fast otherwise it would be filled immediately\n' + slotDetails,
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
// console.log(slotDetails);
};
const PORT = process.env.PORT || 3000;
app.listen(PORT, function () {
console.log("Server Started on Port" + PORT);
});
main().then(() => { console.log('Vaccine availability checker started.'); });