Created
November 29, 2021 11:04
-
-
Save muffix/8556c9a9e915b0d5e3f25aedd60c8947 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
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
""" | |
Quick-and-dirty scraper that checks for the availability of a product in the Unifi store | |
Checks the link in the TARGET_URL variable every 30 seconds and calls the webhook in WEBHOOK_URL if the product | |
is available. | |
Requirements: | |
beautifulsoup4==4.10.0 | |
requests==2.26.0 | |
requests_html==0.10.0 | |
tenacity==8.0.1 | |
""" | |
import logging | |
import requests | |
from bs4 import BeautifulSoup | |
from requests_html import HTMLResponse, HTMLSession | |
from tenacity import retry, wait_fixed | |
TARGET_URL = "https://eu.store.ui.com/collections/early-access/products/dream-router-ea" | |
WEBHOOK_URL = ( | |
"https://maker.ifttt.com/trigger/udr_available/with/key/<YOUR_KEY_HERE>" | |
) | |
logging.basicConfig( | |
format="%(asctime)s %(levelname)-8s %(message)s", | |
level=logging.INFO, | |
datefmt="%Y-%m-%d %H:%M:%S", | |
) | |
def notify(): | |
requests.get(WEBHOOK_URL) | |
@retry(wait=wait_fixed(30)) | |
def scrape(session: HTMLSession): | |
resp = session.get(TARGET_URL) # type: HTMLResponse | |
resp.html.render() | |
soup = BeautifulSoup(resp.html.html, "html.parser") | |
for button in soup.find(id="bundleApp").find_all("button"): | |
if any("add to cart" in span.text.lower() for span in button.find_all("span")): | |
logging.info("Found availability") | |
notify() | |
break | |
else: | |
logging.error("Out of stock.") | |
raise Exception("Nothing available") | |
if __name__ == "__main__": | |
scrape(HTMLSession()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment