-
Notifications
You must be signed in to change notification settings - Fork 8
/
vaccine-hunter.js
100 lines (87 loc) · 2.54 KB
/
vaccine-hunter.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
const dotenv = require('dotenv')
const cron = require('node-cron')
const express = require('express')
const nodemailer = require('nodemailer')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
// Setup important stuff
const app = express()
dotenv.config()
const md_recipients = '[email protected]'
const va_recipients = '[email protected]'
// Create mail transporter.
// Highly recommend to avoid using your personal email
// account for this step. Quick solutions on the internet
// may guide you towards lowering security measures.
// Consider using a new separate developer account to
// avoid any risk to your personal email account.
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: true,
auth: {
user: process.env.SMTP_AUTH_USERNAME,
pass: process.env.SMTP_AUTH_PASSWORD,
},
})
const hunt = async (state) => {
if (!state) {
console.error('State is required')
return
}
try {
const { stdout, stderr } =
state === 'MD'
? await exec('sh scripts/md-hunt.sh')
: await exec('sh scripts/va-hunt.sh')
if (stdout) {
const data =
state === 'MD'
? JSON.parse(stdout).responsePayloadData.data['MD']
: JSON.parse(stdout).responsePayloadData.data['VA']
return data
.map((p) => `City: ${p.city}, Total Available: ${p.totalAvailable} \n`)
.toString()
}
} catch (err) {
console.error(err)
}
}
cron.schedule('*/15 * * * *', function () {
console.log('---------------------')
console.log('Running Hunter')
// Check Maryland and email
hunt('MD').then((res) => {
let messageOptions = {
from: process.env.FROM,
to: process.env.MD_RECIPIENTS,
subject: 'Maryland CVS Vaccine Availability',
text: res,
}
transporter.sendMail(messageOptions, function (error, info) {
if (error) {
throw error
} else {
console.log('Maryland email successfully sent!')
}
})
})
// Check Virginia and email
hunt('VA').then((res) => {
let messageOptions = {
from: process.env.FROM,
to: process.env.VA_RECIPIENTS,
subject: 'Virginia CVS Vaccine Availability',
text: res,
}
transporter.sendMail(messageOptions, function (error, info) {
if (error) {
throw error
} else {
console.log('Virginia email successfully sent!')
}
})
})
})
app.listen(process.env.PORT)
console.log(`Vaccine hunter has started on port ${process.env.PORT}`)