-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
54 lines (40 loc) · 1.71 KB
/
scraper.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
const axios = require('axios');
const cheerio = require('cheerio');
require('dotenv').config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER;
const myPhoneNumber = process.env.MY_PHONE_NUMBER;
/// PRODUCT INFORMATION
const desiredPrice = 8000;
const url =
`https://www.amazon.com.br/Notebook-Legion-R7-5800H-RTX3050-82QJ0000BR/dp/B09PZGR4MR/ref=sr_1_51?` +
`__mk_pt_BR=ÅMÅŽÕÑ&crid=6F3TPYLDRV93&keywords=notebook&qid=1658101968&sprefix=notebook%2Caps%2C233&sr=8-51&ufe=app_do%3Aamzn1.fos.25548f35-0de7-44b3-b28e-0f56f3f96147`;
////////////////////////////////////////////////////////////////
const client = require('twilio')(accountSid, authToken);
const handle = setInterval(scrape, 100000);
const product = { name: "", price: "", link: "" };
async function scrape() {
const encoded = encodeURI(url)
const { data } = await axios.get(encoded).catch(err => console.log(err));
const cheer = cheerio.load(data);
const item = cheer("div#dp-container");
product.name = cheer(item).find("h1 span#productTitle").text();
product.link = url;
const price = cheer(item).find("span .a-price-whole")
.first().text().replace(/[,.]/g, "");
const priceNum = parseFloat(price);
product.price = priceNum;
if (priceNum < desiredPrice) {
client.messages.create({
body: `Price of ${product.name} just went to ${price}. Get it on ${product.link}`,
from: twilioPhoneNumber,
to: myPhoneNumber
})
.then((message) => {
console.log(message);
clearInterval(handle);
})
}
}
scrape()