-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
192 lines (173 loc) · 6.25 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
192
"use strict";
const CronJob = require('cron').CronJob;
const fs = require('fs');
const axios = require('axios');
require('dotenv').config()
const nodemailer = require("nodemailer");
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const mailSender = async () => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: process.env.USER_EMAIL,
pass: process.env.USER_PASSWORD,
}
});
const dataPrepare = require('./dataPrepare');
const mailBody = await dataPrepare.apiFetch();
console.log('Mail Body', mailBody);
if (Object.keys(mailBody).length === 0) {
return;
}
if (Object.keys(mailBody).length !== 0) {
fs.writeFile('./output.json', JSON.stringify(mailBody), () => console.log('WRITTEN TO FILE'))
await writeDataToCSV(mailBody);
}
const data = fs.readFileSync('./recipients.txt', 'utf8');
if (data === '') {
console.error('NO EMAILS SPECIFIED, please specify email addresses in recipients.txt\nWith emails like, [email protected], [email protected]')
process.exit(0);
}
// send mail with defined transport object
let d = new Date()
let currentHour = d.getHours();
if (currentHour >= 1 && currentHour <= 7) // Send ALEXA notification only during day time. 7 AM to 12:59 AM
{
console.log(`IT IS NIGHT 😪 DID NOT SEND NOTIFICATION TO ALEXA!`)
} else {
const districts = dataPrepare.getCenterNames();
const totalDistricts = districts.length;
let otherLocations = '';
// const
for (const [index, district] of districts.entries()) {
const shorten = district.split(" ")[0] + district.split(" ")[1]; // Taking first two words
if (totalDistricts > 3 && index >= 3) {
otherLocations = otherLocations.concat(' and few other locations.');
break;
} else {
otherLocations = otherLocations.concat(`${shorten}, `);
}
}
if (otherLocations.length > 0) {
await sendAlexaNotification(otherLocations);
console.log(`SENT NOTIFICATION TO REGISTERED ALEXA DEVICE!`)
}
}
let currentTime = d.toLocaleString('en-US', {
timeZone: 'Asia/Kolkata'
});
let info = await transporter.sendMail({
from: '"Suryashi IT 🖥" <[email protected]>', // sender address
to: `${data.toString()}`, // list of receivers
subject: `💉 CoWin Vaccines Available - ${currentTime}`, // Subject line
// text: JSON.stringify(mailBody, null, '\t'), // plain text body
html: "<h1>Hi, Vaccines are available in your area 🚑. Please check the attachment 🖇 to this email for more information!</h1>", // html body
attachments: [{
filename: 'Vaccine Information.csv',
path: './output.csv',
contentType: 'application/vnd.ms-excel',
// contentType: 'application/json'
}]
});
console.log("Message sent: %s", info.messageId);
// Message sent: <[email protected]>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
const pinCodeToDistrict = (obj) => {
const items = new Set(); // Set to avoid duplicate entries
for (const area in obj)
items.add(pincodeDirectory.lookup(area)[0].districtName);
return [...items];
}
const writeDataToCSV = async (mailBody) => {
const csvWriter = createCsvWriter({
append: false,
path: './output.csv',
header: [{
id: 'pincode',
title: 'PIN'
}, {
id: 'date',
title: 'DATE'
}, {
id: 'name',
title: 'CENTER NAME'
}, {
id: 'vacancy',
title: 'VACANCY'
}, {
id: 'vacancydose1',
title: 'VACANCY DOSE 1'
}, {
id: 'vacancydose2',
title: 'VACANCY DOSE 2'
}, {
id: 'min_age',
title: 'MINIMUM AGE'
}, {
id: 'vaccine',
title: 'VACCINE'
}, {
id: 'fee',
title: 'FEE TYPE'
},]
});
const records = [];
for (const pin in mailBody) {
for (const {
info,
name,
fee_type
}
of mailBody[pin]) {
for (const {
date,
min_age,
vaccine,
available_capacity,
available_capacity_dose1,
available_capacity_dose2
}
of info) {
records.push({
pincode: pin,
date,
name,
vacancy: available_capacity,
vacancydose1: available_capacity_dose1,
vacancydose2: available_capacity_dose2,
min_age,
vaccine,
fee: fee_type
})
console.log(pin, date, name, available_capacity, available_capacity_dose1, available_capacity_dose2, min_age, vaccine, fee_type);
}
}
}
csvWriter.writeRecords(records) // returns a promise
.then(() => {
console.log('[ WRITTEN CSV FILE ]');
});
}
const sendAlexaNotification = async (places) => {
const token = process.env.ALEXA_TOKEN;
const buildMessage = encodeURIComponent(`Vaccination available in ${places}. Check email for more information.`);
const apiUrl = `https://api.notifymyecho.com/v1/NotifyMe?notification=${buildMessage}&accessCode=${token}`;
try {
await axios.post(apiUrl);
} catch (e) {
console.error(e);
}
}
const minutes = 20;
const job = new CronJob(`*/${minutes} * * * *`, async () => {
console.log(`------- JOB STARTED (ITERATING IN ${minutes} MINUTE(S)) 🚀 -------\n`)
await mailSender().catch(console.error);
console.log(`\n----------- JOB DONE ✅ -----------`)
}, null, true, 'Asia/Kolkata');
job.start();