-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.js
81 lines (71 loc) · 1.96 KB
/
track.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
78
79
80
81
require("dotenv").config();
const puppeteer = require("puppeteer");
const $ = require("cheerio").default;
const CronJob = require("cron").CronJob;
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
// Price CSS Selector : #corePrice_desktop .a-price.a-text-price.a-size-medium.apexPriceToPay > .a-offscreen
const url = process.argv[2]; // node track <url>
const minPrice = process.argv[3]; // node track <url> <minPrice>
async function configureBrowser() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
return page;
}
async function checkPrice(page) {
try {
await page.reload();
let html = await page.evaluate(() => document.body.innerHTML);
// console.log(html);
$(
"#corePrice_desktop .a-price.a-text-price.a-size-medium.apexPriceToPay > .a-offscreen",
html
).each(function () {
let ruppeePrice = $(this).text();
// console.log(ruppeePrice);
let price = parseInt(ruppeePrice.replace("₹", "").replace(",", ""));
console.log(price);
if (price < minPrice) {
console.log(`🛍 Price on ${url} is less than ${minPrice}`);
sendEmail(
`🛍 Price Is Low!`,
`Price on ${url} is less than ${minPrice}`
);
}
});
} catch (err) {
sendEmail(
`🛍 Price Check Failed`,
`Price Check Failed on ${url} with error ${err.message}`
);
throw err;
}
}
function sendEmail(subject, body) {
const email = {
to: "taxoha4598@tourcc.com",
from: "milind.mishra4@gmail.com",
subject: subject,
text: body,
html: body,
};
return sgMail.send(email);
}
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();
}
startTracking();