|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from functools import lru_cache |
| 6 | +from hashlib import sha256 |
| 7 | +from pathlib import Path |
| 8 | +from time import time |
| 9 | + |
| 10 | +import requests |
| 11 | +try: |
| 12 | + from scrapling.fetchers import FetcherSession # type: ignore |
| 13 | + |
| 14 | + _SCRAPLING_AVAILABLE = True |
| 15 | +except Exception: # pragma: no cover |
| 16 | + FetcherSession = None # type: ignore |
| 17 | + _SCRAPLING_AVAILABLE = False |
| 18 | + |
| 19 | +URL_CHECK_HEADERS = { |
| 20 | + "User-Agent": "Mozilla/5.0 (compatible; RummerLab/1.0; +https://rummerlab.org)", |
| 21 | + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 22 | +} |
| 23 | + |
| 24 | +RUMMER_STRONG_MARKERS = ( |
| 25 | + "rummerlab", |
| 26 | + "physioshark", |
| 27 | + "physiologyfish", |
| 28 | +) |
| 29 | + |
| 30 | +RUMMER_NAME_MARKERS = ( |
| 31 | + "jodie rummer", |
| 32 | + "dr jodie rummer", |
| 33 | + "professor jodie rummer", |
| 34 | +) |
| 35 | + |
| 36 | +RUMMER_CONTEXT_MARKERS = ( |
| 37 | + "shark", |
| 38 | + "sharks", |
| 39 | + "fish", |
| 40 | + "marine", |
| 41 | + "jcu", |
| 42 | + "james cook university", |
| 43 | +) |
| 44 | + |
| 45 | +NEWS_HTML_CACHE_DIR = Path(os.environ.get("CACHE_DIR", "cache")) / "news_html" |
| 46 | +NEWS_HTML_CACHE_DIR.mkdir(parents=True, exist_ok=True) |
| 47 | + |
| 48 | +def _news_html_cache_max_age_seconds() -> int | None: |
| 49 | + """ |
| 50 | + Returns: |
| 51 | + - None: keep forever (no expiry) |
| 52 | + - int: max age in seconds |
| 53 | + """ |
| 54 | + raw = os.environ.get("NEWS_HTML_CACHE_EXPIRE_SECONDS", None) |
| 55 | + if raw is None: |
| 56 | + return 60 * 60 * 24 * 365 # 365 days default |
| 57 | + if raw.strip() == "": |
| 58 | + return None |
| 59 | + return int(raw) |
| 60 | + |
| 61 | + |
| 62 | +NEWS_HTML_CACHE_MAX_AGE_SECONDS = _news_html_cache_max_age_seconds() |
| 63 | + |
| 64 | + |
| 65 | +def _url_cache_key(url: str) -> str: |
| 66 | + return sha256(url.encode("utf-8")).hexdigest() |
| 67 | + |
| 68 | + |
| 69 | +def _cache_paths_for_url(url: str) -> tuple[Path, Path]: |
| 70 | + key = _url_cache_key(url) |
| 71 | + return (NEWS_HTML_CACHE_DIR / f"{key}.html", NEWS_HTML_CACHE_DIR / f"{key}.json") |
| 72 | + |
| 73 | + |
| 74 | +def _is_likely_blocked_or_captcha(text_lower: str) -> bool: |
| 75 | + """ |
| 76 | + Heuristics to avoid persisting "fake 200" pages (captcha / bot detection / blocked). |
| 77 | + Keep this broad; false positives are preferable to caching a captcha page. |
| 78 | + """ |
| 79 | + markers = ( |
| 80 | + "captcha", |
| 81 | + "recaptcha", |
| 82 | + "hcaptcha", |
| 83 | + "cloudflare", |
| 84 | + "ddos protection", |
| 85 | + "attention required", |
| 86 | + "verify you are human", |
| 87 | + "are you a robot", |
| 88 | + "access denied", |
| 89 | + "request blocked", |
| 90 | + "bot detection", |
| 91 | + "unusual traffic", |
| 92 | + "temporarily unavailable", |
| 93 | + "incapsula", |
| 94 | + "imperva", |
| 95 | + "akamai", |
| 96 | + "sucuri", |
| 97 | + ) |
| 98 | + return any(m in text_lower for m in markers) |
| 99 | + |
| 100 | + |
| 101 | +def _load_cached_html(url: str) -> str | None: |
| 102 | + html_path, meta_path = _cache_paths_for_url(url) |
| 103 | + if not html_path.exists() or not meta_path.exists(): |
| 104 | + return None |
| 105 | + try: |
| 106 | + meta = json.loads(meta_path.read_text(encoding="utf-8")) |
| 107 | + fetched_at = float(meta.get("fetched_at", 0)) |
| 108 | + if fetched_at <= 0: |
| 109 | + return None |
| 110 | + if NEWS_HTML_CACHE_MAX_AGE_SECONDS is not None: |
| 111 | + if time() - fetched_at > NEWS_HTML_CACHE_MAX_AGE_SECONDS: |
| 112 | + return None |
| 113 | + return html_path.read_text(encoding="utf-8", errors="ignore") |
| 114 | + except OSError: |
| 115 | + return None |
| 116 | + except (ValueError, TypeError): |
| 117 | + return None |
| 118 | + |
| 119 | + |
| 120 | +def _save_cached_html(url: str, html: str) -> None: |
| 121 | + html_path, meta_path = _cache_paths_for_url(url) |
| 122 | + try: |
| 123 | + html_path.write_text(html, encoding="utf-8", errors="ignore") |
| 124 | + meta_path.write_text( |
| 125 | + json.dumps({"url": url, "fetched_at": time()}, ensure_ascii=False), |
| 126 | + encoding="utf-8", |
| 127 | + ) |
| 128 | + except OSError: |
| 129 | + # Best-effort cache only |
| 130 | + return |
| 131 | + |
| 132 | + |
| 133 | +def _scrapling_fetch_html_prefix(url: str, *, timeout_s: int = 8) -> tuple[int, str, str] | None: |
| 134 | + """ |
| 135 | + Fetch a page using Scrapling's static engine (browser-like TLS + headers). |
| 136 | +
|
| 137 | + Returns (status_code, content_type_lower, text_lower) or None on any error. |
| 138 | + Only returns a bounded prefix of the page text (to keep it fast). |
| 139 | + """ |
| 140 | + if not _SCRAPLING_AVAILABLE or FetcherSession is None: |
| 141 | + return None |
| 142 | + try: |
| 143 | + # NOTE: we create a short-lived session per call to keep this simple and safe. |
| 144 | + # If this becomes a hotspot, we can switch to a longer-lived session/pool. |
| 145 | + with FetcherSession( |
| 146 | + impersonate="chrome", |
| 147 | + timeout=timeout_s, |
| 148 | + stealthy_headers=True, |
| 149 | + follow_redirects=True, |
| 150 | + retries=2, |
| 151 | + retry_delay=1, |
| 152 | + verify=True, |
| 153 | + ) as s: |
| 154 | + resp = s.get(url) |
| 155 | + status_code = int(getattr(resp, "status_code", 0) or 0) |
| 156 | + headers = getattr(resp, "headers", {}) or {} |
| 157 | + # curl-cffi headers are case-insensitive; treat like dict |
| 158 | + ctype = (headers.get("Content-Type") or headers.get("content-type") or "").lower() |
| 159 | + text = getattr(resp, "text", "") or "" |
| 160 | + if not isinstance(text, str): |
| 161 | + return None |
| 162 | + text_lower = text[:200_000].lower() |
| 163 | + return status_code, ctype, text_lower |
| 164 | + except Exception: |
| 165 | + return None |
| 166 | + |
| 167 | + |
| 168 | +@lru_cache(maxsize=4096) |
| 169 | +def url_is_definitely_404(url: str) -> bool: |
| 170 | + """ |
| 171 | + Return True only when we're confident the URL is a 404. |
| 172 | + We do HEAD with redirects and a short timeout, with GET fallback for sites |
| 173 | + that block/disable HEAD. |
| 174 | + """ |
| 175 | + try: |
| 176 | + resp = requests.head( |
| 177 | + url, |
| 178 | + allow_redirects=True, |
| 179 | + timeout=5, |
| 180 | + headers=URL_CHECK_HEADERS, |
| 181 | + ) |
| 182 | + if resp.status_code == 404: |
| 183 | + return True |
| 184 | + if resp.status_code in (405, 403): |
| 185 | + resp = requests.get( |
| 186 | + url, |
| 187 | + allow_redirects=True, |
| 188 | + timeout=5, |
| 189 | + headers=URL_CHECK_HEADERS, |
| 190 | + stream=True, |
| 191 | + ) |
| 192 | + return resp.status_code == 404 |
| 193 | + return False |
| 194 | + except requests.RequestException: |
| 195 | + # Only exclude on definitive 404. Any network hiccup -> keep the item. |
| 196 | + return False |
| 197 | + |
| 198 | + |
| 199 | +@lru_cache(maxsize=4096) |
| 200 | +def url_page_is_about_rummer(url: str) -> bool | None: |
| 201 | + """ |
| 202 | + Best-effort content check. |
| 203 | +
|
| 204 | + Returns: |
| 205 | + - True: confident the page mentions Dr Jodie Rummer / lab terms |
| 206 | + - False: confident it does NOT (based on fetched content) |
| 207 | + - None: unknown (blocked, paywall, non-HTML, network error) -> do not exclude |
| 208 | + """ |
| 209 | + cached_html = _load_cached_html(url) |
| 210 | + if cached_html: |
| 211 | + text = cached_html.lower() |
| 212 | + if any(m in text for m in RUMMER_STRONG_MARKERS): |
| 213 | + return True |
| 214 | + if any(m in text for m in RUMMER_NAME_MARKERS): |
| 215 | + return True |
| 216 | + if "rummer" in text and any(m in text for m in ("jodie", *(RUMMER_CONTEXT_MARKERS))): |
| 217 | + return True |
| 218 | + return False |
| 219 | + |
| 220 | + try: |
| 221 | + fetched = _scrapling_fetch_html_prefix(url, timeout_s=8) |
| 222 | + if not fetched: |
| 223 | + return None |
| 224 | + status_code, ctype, text = fetched |
| 225 | + if status_code == 404: |
| 226 | + # Let the 404 filter handle it; treat as unknown here. |
| 227 | + return None |
| 228 | + if ctype and ("text/html" not in ctype and "application/xhtml+xml" not in ctype): |
| 229 | + return None |
| 230 | + if not text.strip(): |
| 231 | + return None |
| 232 | + if _is_likely_blocked_or_captcha(text): |
| 233 | + return None |
| 234 | + |
| 235 | + if any(m in text for m in RUMMER_STRONG_MARKERS): |
| 236 | + _save_cached_html(url, text) |
| 237 | + return True |
| 238 | + if any(m in text for m in RUMMER_NAME_MARKERS): |
| 239 | + _save_cached_html(url, text) |
| 240 | + return True |
| 241 | + |
| 242 | + # We only accept generic "rummer" when there's some relevant context. |
| 243 | + if "rummer" in text and any(m in text for m in ("jodie", *(RUMMER_CONTEXT_MARKERS))): |
| 244 | + _save_cached_html(url, text) |
| 245 | + return True |
| 246 | + |
| 247 | + # If we successfully fetched HTML and found none of the markers, treat as not about. |
| 248 | + _save_cached_html(url, text) |
| 249 | + return False |
| 250 | + except requests.RequestException: |
| 251 | + return None |
| 252 | + |
| 253 | + |
| 254 | +def filter_media_items(items: list[dict]) -> list[dict]: |
| 255 | + """ |
| 256 | + Filter media items: |
| 257 | + - drop items with absolute URL that is definitively 404 |
| 258 | + - drop items with absolute URL that we confidently conclude are not about Rummer |
| 259 | + - keep on unknown (errors, blocked, non-HTML) to avoid false negatives |
| 260 | + - keep items without an absolute URL |
| 261 | + """ |
| 262 | + filtered: list[dict] = [] |
| 263 | + for item in items: |
| 264 | + url = (item.get("url") or "").strip() if isinstance(item, dict) else "" |
| 265 | + # Keep items without an absolute URL (e.g. curated/in-site items). |
| 266 | + if not url or not (url.startswith("http://") or url.startswith("https://")): |
| 267 | + filtered.append(item) |
| 268 | + continue |
| 269 | + if url_is_definitely_404(url): |
| 270 | + continue |
| 271 | + about = url_page_is_about_rummer(url) |
| 272 | + if about is False: |
| 273 | + continue |
| 274 | + filtered.append(item) |
| 275 | + return filtered |
| 276 | + |
| 277 | + |
| 278 | +def clear_caches() -> None: |
| 279 | + url_is_definitely_404.cache_clear() |
| 280 | + url_page_is_about_rummer.cache_clear() |
| 281 | + |
0 commit comments