scan.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. """Stdlib fetch helper for simple url_pattern brokers (osint-style).
  2. For JS-rendered or anti-bot pages the agent should use the `web_extract` or
  3. `browser_exec` tools (and the `scrapling` skill for stealth/Cloudflare).
  4. This helper only covers plain static pages and is intentionally network-light so
  5. it can be mocked in tests.
  6. """
  7. from __future__ import annotations
  8. import urllib.error
  9. import urllib.request
  10. USER_AGENT = "Mozilla/5.0 (compatible; unbroker/1.0; data opt-out)"
  11. def fetch(url: str, timeout: int = 20) -> tuple[int, str]:
  12. req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
  13. try:
  14. with urllib.request.urlopen(req, timeout=timeout) as resp:
  15. charset = resp.headers.get_content_charset() or "utf-8"
  16. return getattr(resp, "status", 200), resp.read().decode(charset, errors="replace")
  17. except urllib.error.HTTPError as exc:
  18. return exc.code, ""
  19. except (urllib.error.URLError, TimeoutError, ValueError):
  20. return 0, ""
  21. def looks_listed(html: str, match_signal: str | None) -> bool:
  22. """Naive confirmation heuristic for static pages: does the match signal appear?"""
  23. if not html or not match_signal:
  24. return False
  25. return match_signal.lower() in html.lower()