"""Filings / Regulatory API adapter - fetches SEC-style submissions.""" import hashlib import logging from datetime import datetime from typing import Any, Dict import httpx from .base import AdapterResult, BaseAdapter logger = logging.getLogger("filings_adapter") class FilingsAdapter(BaseAdapter): """Concrete adapter for SEC EDGAR or similar filings API.""" def __init__(self, base_url: str = "https://efts.sec.gov", user_agent: str = "StonksOracle/1.0"): self.base_url = base_url self.user_agent = user_agent def source_type(self) -> str: return "filings_api" async def fetch(self, ticker: str, config: Dict[str, Any]) -> AdapterResult: _cik = config.get("cik", "") endpoint = config.get("endpoint", f"/LATEST/search-index?q=%22{ticker}%22&dateRange=custom&startdt=2026-01-01&forms=8-K,10-Q,10-K") url = f"{self.base_url}{endpoint}" headers = {"User-Agent": self.user_agent} async with httpx.AsyncClient(timeout=30) as client: try: resp = await client.get(url, headers=headers) resp.raise_for_status() raw = resp.content data = resp.json() content_hash = hashlib.sha256(raw).hexdigest() hits = data.get("hits", {}).get("hits", []) return AdapterResult( source_type="filings_api", ticker=ticker, items=hits, raw_payload=raw, content_hash=content_hash, fetched_at=datetime.utcnow(), ) except Exception as e: logger.error(f"Filings fetch failed for {ticker}: {e}") return AdapterResult( source_type="filings_api", ticker=ticker, items=[], raw_payload=b"", content_hash="", fetched_at=datetime.utcnow(), error=str(e), )