Skip to content

Instantly share code, notes, and snippets.

@jacyhung
Forked from muffix/unifi_scrape.py
Last active September 30, 2022 23:16
Show Gist options
  • Save jacyhung/fbecae4f58a6691fbf0b0b21b3c19030 to your computer and use it in GitHub Desktop.
Save jacyhung/fbecae4f58a6691fbf0b0b21b3c19030 to your computer and use it in GitHub Desktop.
Quick-and-dirty scraper that checks for the availability of a product in the Unifi store
"""
Original script: https://gist.github.com/muffix/8556c9a9e915b0d5e3f25aedd60c8947
Quick-and-dirty scraper that checks for the availability of a product in the Unifi store
I was unable to get the original script working, so I modified it. Instead of checking for an "add to cart" button, it
checks whether or not there is an "in stock" badge on the product page. I'm also using discord webhook to ping me whenever
the item comes in stock.
Requirements:
beautifulsoup4==4.10.0
requests==2.26.0
requests_html==0.10.0
tenacity==8.0.1
discord-webhook==0.17.0
"""
TARGET_URL = "https://store.ui.com/collections/unifi-protect/products/unifi-protect-g3-instant-camera"
WEBHOOK_URL = (
"<Replace with your Discord webhook URL>"
)
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
def notify():
webhook = DiscordWebhook(url=WEBHOOK_URL, content='@everyone Product is in stock!')
response = webhook.execute()
@retry(wait=wait_fixed(30))
def scrape():
page = requests.get(TARGET_URL)
soup = BeautifulSoup(page.content, "html.parser")
for div in soup.find(id="productDescriptionAndWizard").find_all("div"):
if any("in stock" in span.text.lower() for span in div.find_all("span")):
logging.info("Found availability")
notify()
break
else:
logging.error("Out of stock.")
raise Exception("Nothing available")
if __name__ == "__main__":
scrape()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment