"""
newspaper4k-based content extractor.

Uses newspaper4k's article parser which combines structural analysis
with NLP heuristics. Particularly strong on news front pages and
multi-answer forum threads where it extracts more content than
trafilatura. Run in parallel with the primary extractor and the
longer result wins.
"""

from typing import Optional

from loguru import logger

from .base import BaseExtractor


class NewspaperExtractor(BaseExtractor):
    """Extract content using newspaper4k's article parser."""

    def extract(self, html: str, url: str = "") -> Optional[str]:
        if not html or not html.strip():
            return None

        try:
            from newspaper import Article, Config
        except ImportError:
            logger.debug(
                "newspaper4k not installed — skipping newspaper extraction"
            )
            return None

        try:
            # Disable image fetching to prevent SSRF: parse() calls
            # fetch_images() by default, which makes outbound requests
            # for every <img src="..."> in the HTML — attacker-controlled
            # content could point these at internal services.
            cfg = Config()
            cfg.fetch_images = False
            article = Article(url or "https://example.com", config=cfg)
            article.download(input_html=html)
            article.parse()
            text = article.text
            return text if text and text.strip() else None
        except Exception:
            logger.debug("newspaper4k extraction failed", exc_info=True)
            return None
