-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (62 loc) · 2.3 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
require('dotenv').config();
const express = require('express');
const app = express();
const Nightmare = require('nightmare'); //nightmare will help to find the url and price value in the url block
const nightmare = Nightmare({ waitTimeout : 200000 });
const nodemailer = require('nodemailer');
const port = process.env.port || 3000;
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Configure Nodemailer with SMTP settings using environment variables
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER, // Use environment variable for email
pass: process.env.EMAIL_PASS // Use environment variable for password
}
});
// Function to send email using Nodemailer
function sendEmail(userEmail, url, price) {
const mailOptions = {
from: process.env.EMAIL_USER,
to: userEmail,
subject: 'Price has dropped',
text: `The price on ${url} has dropped below ${price}`,
html: `<strong>The price on ${url} has dropped below ${price}</strong>`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Email sending failed:', error);
} else {
console.log('Email sent:', info.response);
}
});
}
const checkPrice = async (url, price, userEmail) => {
try {
console.log(url);
let priceInString = await nightmare
.goto(url)
.wait('.a-offscreen')
.evaluate(() => document.getElementsByClassName('a-price-whole')[0].innerText)
.end();
console.log(priceInString);
let priceInValue = parseFloat(priceInString.replace(',', ''));
if (priceInValue < price) {
console.log(`New Price: ${price}`);
console.log(`Actual Price: ${priceInValue}`);
await sendEmail(userEmail, url, price);
console.log('Email has been sent');
}
else {
console.log('Price is still higher');
}
} catch (err) {
console.log(err);
}
}
app.post('/products', (req, res) => {
console.log(req.body.email + ' sent a request');
checkPrice(req.body.prodUrl, req.body.price, req.body.email);
});
app.listen(port, () => console.log(`Server listening on port ${port}`));