caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

79 lines
2.8 KiB
Python

"""Injectable Reddit HTTP transport (network only in ``default_fetch``).
The fetcher is a ``Callable[[str], str]`` seam so the whole story flow is testable
without touching the network — tests inject a fake that returns canned Reddit JSON.
``normalize_thread_url`` enforces the boundary (reddit host + http(s), ``.json``
suffix); ``default_fetch`` is a guarded ``httpx`` GET copied from the ingest
transport but with a stories-specific User-Agent and a byte cap.
"""
from __future__ import annotations
from typing import Callable
from urllib.parse import urlparse
Fetcher = Callable[[str], str]
_MAX_FETCH_BYTES = 2_000_000
_USER_AGENT = "ReelForgeBot/1.0 (+stories)"
class RedditFetchError(Exception):
"""Fetch/normalization failure carrying an HTTP-ish status (mirrors IngestError)."""
def __init__(self, status_code: int, message: str) -> None:
super().__init__(message)
self.status_code = status_code
self.message = message
def _host(url: str) -> str:
return (urlparse(url).netloc or "").lower().split(":")[0]
def normalize_thread_url(url: str) -> str:
"""Validate a Reddit thread URL and return its ``.json`` API form.
Rejects non-http(s) schemes and non-reddit hosts with a 400
:class:`RedditFetchError`; strips any query/fragment and appends ``.json`` so
the fetch hits Reddit's public JSON listing endpoint.
"""
raw = (url or "").strip()
lowered = raw.lower()
if not (lowered.startswith("http://") or lowered.startswith("https://")):
raise RedditFetchError(400, "a valid http(s) reddit URL is required")
parsed = urlparse(raw)
host = _host(raw)
if not (host == "reddit.com" or host.endswith(".reddit.com")):
raise RedditFetchError(400, "only reddit.com thread URLs are supported")
path = (parsed.path or "").rstrip("/")
if not path:
raise RedditFetchError(400, "the reddit URL is missing a thread path")
base = f"{parsed.scheme}://{parsed.netloc}{path}"
if base.endswith(".json"):
return base
return f"{base}.json"
def default_fetch(url: str) -> str:
"""Guarded ``httpx`` GET returning the raw JSON text (raises on failure)."""
try:
import httpx
except Exception as exc: # pragma: no cover - only without httpx
raise RedditFetchError(503, "reddit fetching is unavailable") from exc
try:
resp = httpx.get(
url,
timeout=15,
follow_redirects=True,
headers={"User-Agent": _USER_AGENT},
)
except Exception as exc:
raise RedditFetchError(502, f"could not fetch the reddit thread: {exc}") from exc
if resp.status_code >= 400:
raise RedditFetchError(502, f"the reddit thread returned {resp.status_code}")
return resp.text[:_MAX_FETCH_BYTES]