Skip to content

Instantly share code, notes, and snippets.

@24Ryou
Created July 4, 2022 07:35
Show Gist options
  • Save 24Ryou/81091e540dc6ee55ee0ad0d63a98ef7b to your computer and use it in GitHub Desktop.
Save 24Ryou/81091e540dc6ee55ee0ad0d63a98ef7b to your computer and use it in GitHub Desktop.
// "dependencies": {
// "axios": "^0.27.2",
// "cheerio": "^1.0.0-rc.12",
// "dotenv": "^16.0.1",
// "twilio": "^3.78.0"
// }
// need a .env file with:
// TWILIO_ACCOUNT_SID = "your account sid";
// TWILIO_AUTH_TOKEN = "your token";
// packages
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 clinet = require("twilio")(accountSid, authToken);
const url = "put url here";
const product = { name:"", price:"", link:""};
// set interval
const handle = setInterval(scrape,2000);
async function scrape(){
// fetch the data
const {data} = await axios.get(url);
// load up the html
const $ = cheerio.load(data);
const item = $("div#dp-container");
// extract the data that we need
product.name = $(item).find("h1 span#productTitle").text();
product.link = url;
const price = $(item)
.find("span .a-price-whole")
.first()
.text()
.replace(/[,.]/g,"");
const priceNum = parseInt(price);
product.price = priceNum;
// send an sms
if (priceNum < 1000) {
clinet.messages.create({
body: 'the price of ${poduct.name} went below ${price}',
from: "+92484685546",
to: "+8585484654"
})
.then((message) => {
console.log(message);
clearInterval(handle);
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment