-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (53 loc) · 2.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const puppeteer = require('puppeteer');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
const url = 'https://www.amazon.in/New-Apple-iPhone-11-64GB/dp/B08L8C1NJ3/ref=sr_1_1_sspa?dchild=1&keywords=iphone+12&qid=1635070861&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzQVhHREdVOFQySFRCJmVuY3J5cHRlZElkPUEwNTM3NDYxMlFFT1g4U0RYT0VKNiZlbmNyeXB0ZWRBZElkPUEwMDYzODI4S0pKQjhaTFUwVjJHJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==';
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 price = await page.evaluate(
() => document.getElementById("priceblock_ourprice").innerText
);
let currentPrice = parseFloat(price.slice(1).replace(",", ""));
if(currentPrice < 45000){
sendNotification(currentPrice);
}
}
async function startTracking() {
const page = await configureBrowser();
let job = new CronJob('* */30 * * * *', function() { //runs every 30 minutes in this config
checkPrice(page);
}, null, true, null, null, true);
job.start();
}
async function sendNotification(price) {
//console.log(price);
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'mypass@14'
}
});
let textToSend = 'Dont wait just grab it asap ' + price;
let htmlText = `<a href=\"${url}\">Link</a>`;
let info = await transporter.sendMail({
from: '"Price Tracker" <[email protected]',
to: "[email protected]",
subject: 'Price dropped to ' + " " + price,
text: textToSend,
html: htmlText
});
console.log("Message sent: %s", info.messageId);
}
startTracking();
// async function monitor(){
// let page=await configureBrowser();
// await checkPrice(page);
// }
// monitor();