Created
May 21, 2020 22:05
-
-
Save robv/17c010e429042f46c9ff02ae68eecc20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
var accountSid = ''; // Your Account SID from www.twilio.com/console | |
var authToken = ''; // Your Auth Token from www.twilio.com/console | |
var twilio = require('twilio'); | |
var client = new twilio(accountSid, authToken); | |
var inStock = []; | |
const inStockSelector = '.pager .amount'; | |
async function checkStock(url2check,word) { | |
inStock[word] = await inStock[word] ? inStock[word] : 0; | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.goto(url2check); | |
await page.waitFor(500); | |
await page.waitForSelector(inStockSelector); | |
const results = await page.$(inStockSelector); | |
const stockFilter = await page.$('.remove-x'); | |
const inStockText = await results.evaluate(element => element.innerText); | |
const currentStock = await stockFilter == null ? 0 : parseInt(inStockText.split(' ')[0]); | |
console.log('Current stock for ' + word + ': ' + currentStock); | |
if (currentStock != inStock[word]) { | |
inStock[word] = currentStock; | |
await client.messages.create({ | |
body: 'Go buy your ' + word + ', the stock just changed to ' + inStock[word] + '! ' + url2check, | |
to: '+', // Text this number | |
from: '+' // From a valid Twilio number | |
}) | |
.then((message) => console.log(message.sid)); | |
} | |
await browser.close(); | |
setTimeout(checkStock, 180000, url2check, word); | |
} | |
checkStock('https://www.roguefitness.com/weightlifting-bars-plates/bumpers?is_salable[0]=1','weights'); | |
checkStock('https://www.roguefitness.com/strength-equipment/strength-training/benches?is_salable[0]=1','bench'); | |
checkStock('https://www.roguefitness.com/weightlifting-bars-plates/barbells?is_salable[0]=1','barbell'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment