"""
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())