Skip to content

Instantly share code, notes, and snippets.

@HugoLiconV
Created October 1, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoLiconV/22bf21cfdc2b17d3c6f57cb00f6b0eba to your computer and use it in GitHub Desktop.
Save HugoLiconV/22bf21cfdc2b17d3c6f57cb00f6b0eba to your computer and use it in GitHub Desktop.
const axios = require('axios');
const cheerio = require('cheerio');
const cron = require('node-cron');
const productPage =
'https://www.cyberpuerta.mx/Computo-Hardware/Monitores/Monitores/Monitor-Gamer-Curvo-ASUS-ROG-Strix-XG35VQ-LED-35-Quad-HD-Ultra-Wide-FreeSync-100Hz-HDMI-Negro-Gris-Rojo.html?nosto=shop_api_home0_1';
const selector = '.priceText';
const desiredPrice = 15000;
async function getHTML(url) {
const { data } = await axios.get(url).catch(() => {
console.log("Couldn't get the page ☹️");
});
return data;
}
const currencyStringToNumber = string => Number(string.replace(/[^0-9.-]+/g, ''));
function scrapPrice(html) {
const $ = cheerio.load(html);
const price = $(selector)
.text()
.trim();
return price;
}
cron.schedule('0 * * * *', async () => {
console.log('running a task every hour ⏲️');
const html = await getHTML(productPage).catch(console.log);
const currentPrice = currencyStringToNumber(scrapPrice(html));
if (currentPrice < desiredPrice) {
console.log('Congratulations! you just saved some bucks 💵');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment