-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
64 lines (52 loc) · 1.75 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
const puppeteer = require('puppeteer');
const $ = require('cheerio');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
const url = 'https://www.amazon.fr/Koicaxy-Lampe-murale-forme-nuage/dp/B08JSTSLJN/';
const configBrowser = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
return page;
}
const priceCheck = async (page) => {
await page.reload();
let html = await page.evaluate(() => document.body.innerHTML);
// console.log(html);
$('#color_name_0_price', html).each(function() {
let dollarPrice = $(this).text();
// console.log(dollarPrice);
let currentPrice = Number(dollarPrice.replace(/[^0-9.-]+/g,""));
if (currentPrice < 300) {
console.log("BUY!!!! " + currentPrice);
sendNotification(currentPrice);
}
});
}
const startTracking = async () =>{
const page = await configBrowser();
let job = new CronJob('* */30 * * * *', function() { //runs every 30 minutes in this config
priceCheck(page);
}, null, true, null, null, true);
job.start();
}
const sendNotification = async (price) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '*****@gmail.com',
pass: '*****'
}
});
let textToSend = 'Price dropped to ' + price;
let htmlText = `<a href=\"${url}\">Link</a>`;
let info = await transporter.sendMail({
from: '"Price Tracker" <[email protected]>',
to: "*******@gmail.com",
subject: 'Price dropped to ' + price,
text: textToSend,
html: htmlText
});
console.log("Message sent: %s", info.messageId);
}
startTracking();