Skip to content

Commit 3ad5700

Browse files
committed
Initial Commit
0 parents  commit 3ad5700

10 files changed

+2319
-0
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
USER_EMAIL = [email protected]
2+
USER_PASSWORD = suryashi@1234

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:14
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
CMD [ "nodemon", "app.js" ]

app.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
3+
4+
const CronJob = require('cron').CronJob;
5+
const fs = require('fs');
6+
require('dotenv').config()
7+
const nodemailer = require("nodemailer");
8+
const dataPrepare = require('./dataPrepare');
9+
10+
const mailSender = async () => {
11+
let transporter = nodemailer.createTransport({
12+
host: 'smtp.gmail.com',
13+
port: 587,
14+
secure: false,
15+
requireTLS: true,
16+
auth: {
17+
user: process.env.USER_EMAIL,
18+
pass: process.env.USER_PASSWORD,
19+
}
20+
});
21+
22+
const mailBody = dataPrepare.masterRecord;
23+
console.log(mailBody);
24+
25+
if (Object.keys(mailBody).length === 0) {
26+
return;
27+
}
28+
29+
const data = fs.readFileSync('./recipients.txt', 'utf8');
30+
if (data === '') {
31+
console.error('NO EMAILS SPECIFIED, please specify email addresses in recipients.txt\nWith emails like, [email protected], [email protected]')
32+
process.exit(0);
33+
}
34+
// send mail with defined transport object
35+
let d = new Date()
36+
let currentTime = d.toLocaleString('en-US', {timeZone: 'Asia/Kolkata'});
37+
let info = await transporter.sendMail({
38+
from: '"Ankur Paul 👻" <[email protected]>', // sender address
39+
to: `${data.toString()}`, // list of receivers
40+
subject: `💉 CoWin Vaccines Available - ${currentTime}`, // Subject line
41+
text: JSON.stringify(mailBody, null, '\t'), // plain text body
42+
43+
// html: "<h1>Hi!</h1>", // html body
44+
});
45+
46+
console.log("Message sent: %s", info.messageId);
47+
// Message sent: <[email protected]>
48+
49+
// Preview only available when sending through an Ethereal account
50+
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
51+
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
52+
}
53+
54+
var job = new CronJob('* * * * *', async () => {
55+
console.log('------- JOB STARTED (ITERATING IN 1 MINUTE) 🚀 ------- ')
56+
mailSender().catch(console.error);
57+
}, null, true, 'Asia/Kolkata');
58+
59+
job.start();

csvparser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// First I want to read the file
2+
const fs = require('fs')
3+
4+
const text = fs.readFileSync('./pincodes.csv', 'utf8')
5+
const pincodes = text.split(",");
6+
7+
module.exports= pincodes;

dataPrepare.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const csvparsed = require('./csvparser');
2+
3+
const axios = require('axios')
4+
5+
const masterRecord = {};
6+
7+
const apiFetch = async () => {
8+
try {
9+
for (const pin of csvparsed) {
10+
const apiEndpoint = `https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=${pin}&date=03-05-2021`
11+
const {data} = await axios.get(apiEndpoint);
12+
// console.log(data)
13+
const dataHolder = [];
14+
for (const [index, {name, center_id, sessions}] of data.centers.entries()) {
15+
const info = [];
16+
17+
for (const {available_capacity, date} of sessions) {
18+
if (available_capacity > 0) {
19+
info.push({name, date, available_capacity});
20+
// masterRecord.push({name, info: {date, available_capacity}});
21+
}
22+
// console.log(`AVAILABLE `, available_capacity, ' ON ', date)
23+
}
24+
if (info.length > 0)
25+
dataHolder.push({name, info})
26+
}
27+
if (dataHolder.length > 0)
28+
masterRecord[pin] = dataHolder
29+
}
30+
return masterRecord;
31+
// console.log(masterRecord)
32+
} catch (e) {
33+
console.error(e)
34+
}
35+
}
36+
apiFetch().then(r => {
37+
});
38+
module.exports = {masterRecord};

0 commit comments

Comments
 (0)