-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (44 loc) · 1.57 KB
/
index.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
const puppeteer = require('puppeteer');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
const url = 'https://www.amazon.in/Apple-MacBook-Air-13-3-inch-MQD32HN/dp/B073Q5R6VR/';
async function configureBrowser() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
return page;
}
async function checkPrice(page){
await page.reload();
let priceID = await page.evaluate(() => document.querySelector('#priceblock_ourprice').innerHTML);
let currentPrice = Number(priceID.replace(/[^0-9.]+/g,""));
if(currentPrice < 54000){
console.log("You can buy it now, the current price is now "+currentPrice);
sendNotif(currentPrice);
}
}
async function sendNotif(currentPrice){
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
});
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Price drop alert!',
text: `You can buy Macbook Air now , Its price has been dropped to ${currentPrice}INR. Check ${url}`
};
let info = await transporter.sendMail(mailOptions);
console.log(`Message sent to ${info.messageId}`);
}
async function init(){
const page = await configureBrowser();
let scheduleJob = new CronJob('* */1 * * * *',function(){ //Read CronJob pattern for more info
checkPrice(page);
});
scheduleJob.start();
}
init();