# app/utils/fetcher.py

import requests
import trafilatura


def fetch_article_text(url: str) -> str:

    if not url:
        return ""

    headers = {
        "User-Agent": "Mozilla/5.0"
    }

    try:
        response = requests.get(url, headers=headers, timeout=10)

        if response.status_code != 200:
            return ""

        html = response.text

        text = trafilatura.extract(
            html,
            include_comments=False,
            include_tables=False,
            include_links=False,
            favor_precision=True,
        )

        return (text or "").strip()

    except Exception:
        return ""