"""Minimal ONVIF client: the vendor-neutral way to ask a camera for its snapshot URL. Only what the PoC needs: GetProfiles + GetSnapshotUri, WS-UsernameToken digest auth.""" import base64, hashlib, os, re, urllib.request from datetime import datetime, timezone def _call(host, user, pw, path, body): nonce = os.urandom(16) created = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") digest = base64.b64encode(hashlib.sha1(nonce + created.encode() + pw.encode()).digest()).decode() env = f""" {user} {digest} {base64.b64encode(nonce).decode()} {created} {body}""" req = urllib.request.Request(f"http://{host}{path}", env.encode(), {"Content-Type": "application/soap+xml; charset=utf-8"}) return urllib.request.urlopen(req, timeout=10).read().decode() def profiles(host, user, pw): """[(token, width, height)] in the camera's order (main stream first, as a rule).""" x = _call(host, user, pw, "/onvif/media_service", "") out = [] for p in re.findall(r"", x, re.S): tok = re.search(r'token="([^"]+)"', p).group(1) w = re.search(r"(\d+)", p) h = re.search(r"(\d+)", p) out.append((tok, int(w.group(1)) if w else 0, int(h.group(1)) if h else 0)) return out def snapshot_uri(host, user, pw, token): x = _call(host, user, pw, "/onvif/media_service", f"{token}") return re.search(r"([^<]+)", x).group(1).replace("&", "&")