-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathamazonPriceMonitor.js
70 lines (63 loc) · 1.96 KB
/
amazonPriceMonitor.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
const { chromium } = require('playwright');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
const url =
'https://www.amazon.com/TP-Link-AC1750-Smart-WiFi-Router/dp/B079JD7F7G/ref=sr_1_5?crid=2JDZXNQPT05L9';
const getCurrentPrice = async (page) => {
await page.reload();
let currentPrice = await page.evaluate(() => {
const $el = document.querySelector('#priceblock_dealprice');
const dollarPrice = $el ? $el.innerText : 0;
return Number(dollarPrice.replace(/[^0-9.-]+/g, ''));
});
return currentPrice;
};
const sendNotification = async (price) => {
const { USER_EMAIL, GAMIL_PASSWORD, TO_EMAIL } = process.env;
if (![USER_EMAIL, GAMIL_PASSWORD, TO_EMAIL].every((e) => !!e)) {
console.log('Required Credentials :(((');
return false;
}
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: USER_EMAIL,
pass: GAMIL_PASSWORD,
},
});
let textToSend = 'Price dropped to ' + price;
let htmlText = `your item price is dropped you can check <a href=\"${url}\">here</a>`;
let info = await transporter.sendMail({
from: `Price Tracker ${USER_EMAIL}`,
to: TO_EMAIL,
subject: 'Price dropped to ' + price,
text: textToSend,
html: htmlText,
});
console.log('Message sent: %s', info.messageId);
return info;
};
(async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(url);
const job = new CronJob(
'* */30 * * * *',
async () => {
//runs every 30 minutes in this config
const currentPrice = await getCurrentPrice(page);
if (currentPrice < 59) {
console.log('BUY!!!! ' + currentPrice);
const result = await sendNotification(currentPrice);
if (result) job.stop();
}
},
null,
true, // start
null, // timeZone
null,
true, // runOnInit
);
job.start();
})();