Files
opalfiles/.local/bin/webwatcher.py
2025-11-03 16:34:43 -08:00

41 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import os, re, json, subprocess
from urllib.request import urlopen, Request
URL = "https://shop.nwnprod.com" # page to watch
STATE = os.path.expanduser("~/.local/state/webwatch_beherit.json")
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) " "Gecko/20100101 Firefox/128.0"
# --- fetch html ---
req = Request(URL, headers={"User-Agent": USER_AGENT})
html = urlopen(req, timeout=15).read().decode("utf-8", "ignore")
# --- find product IDs and titles that contain 'Beherit' ---
pattern = re.compile(
r"product_id=(\d+)[^>]*>([^<]*Beherit[^<]*)</a>", re.IGNORECASE | re.DOTALL
)
products = {
pid: re.sub(r"\s+", " ", title).strip() for pid, title in pattern.findall(html)
}
# --- load previous seen IDs ---
seen = set()
if os.path.exists(STATE):
with open(STATE) as f:
seen = set(json.load(f))
# --- notify for new Beherit items ---
new = [(pid, title) for pid, title in products.items() if pid not in seen]
for pid, title in new:
subprocess.run(
["notify-send", "-u", "critical", "-t", "0", "Beherit Alert", title],
check=False,
)
print(f"New Beherit item: {title}")
# --- update state file ---
if new:
with open(STATE, "w") as f:
json.dump(sorted(products.keys()), f)