""" Fetches the Crypto Fear & Greed Index from alternative.me — completely free. """ import logging import requests from datetime import datetime, timezone from config import FEAR_GREED_URL logger = logging.getLogger(__name__) class FearGreedCollector: def fetch(self) -> list[dict]: """Returns last 7 days of fear & greed data.""" try: resp = requests.get(FEAR_GREED_URL, timeout=10) resp.raise_for_status() data = resp.json().get("data", []) return [ { "value": int(item["value"]), "label": item["value_classification"], "timestamp": datetime.fromtimestamp( int(item["timestamp"]), tz=timezone.utc ).isoformat(), } for item in data ] except Exception as exc: logger.warning(f"[FearGreed] Fetch failed: {exc}") return [] def current(self) -> dict: data = self.fetch() return data[0] if data else {"value": 50, "label": "Neutral", "timestamp": ""}