rmi-backend/app/_archive/legacy_2026_07/sentiment.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
PHASE 2.3 (AUDIT-2026-Q3.md):

Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
  - app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
    POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
    Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
    with the v1 /api/v1/scanner/ stub.
  - app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
    library module consumed by unified_scanner_router via get_wallet_scanner()).
  - app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
    /api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).

Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
  - 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
  - 63 flat app/*.py (domain modules never imported by live code).
  - 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
    tests/unit/test_bridge_health.py which directly imports it; reach graph
    considered tests/ only as transitive reach — to be patched in next cycle).

Forced-LIVE (NOT archived per user directive):
  - app/ai_pipeline_v3.py  (3 importers in audit window, importers themselves DEAD)
  - app/splade_bm25.py       (LIVE via app.rag_service)
  - app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
  - app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)

Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
  - imports = 348 app.* modules
  - reached = 194 files reachable from roots
  - archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
  - Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)

pyproject.toml updates:
  - setuptools.packages.find: added exclude for app._archive*
  - ruff.extend-exclude: added "app/_archive/"
  - mypy.exclude: added "app/_archive/"

Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).

Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.

Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
2026-07-06 20:52:31 +02:00

129 lines
3.7 KiB
Python

"""Sentiment pipeline - X/Twitter + Reddit crypto mentions → NLP scoring."""
import os
import re
from datetime import UTC, datetime
import httpx
from fastapi import APIRouter
router = APIRouter(prefix="/api/v1/sentiment", tags=["sentiment"])
SEARXNG = os.getenv("SEARXNG_URL", "http://localhost:8088")
POSITIVE_WORDS = {
"bullish",
"moon",
"pump",
"gem",
"buy",
"long",
"green",
"ATH",
"breakout",
"accumulation",
"undervalued",
"partnership",
"listed",
"launch",
"mainnet",
}
NEGATIVE_WORDS = {
"bearish",
"dump",
"rug",
"scam",
"sell",
"short",
"red",
"crash",
"hack",
"exploit",
"FUD",
"dead",
"delist",
"bankrupt",
"SEC",
}
def _score_text(text: str) -> dict:
words = set(re.findall(r"\b\w+\b", text.lower()))
pos = len(words & POSITIVE_WORDS)
neg = len(words & NEGATIVE_WORDS)
total = pos + neg
if total == 0:
return {"sentiment": "neutral", "score": 0.5, "positive": 0, "negative": 0, "total_mentions": 0}
score = pos / total
sentiment = "bullish" if score > 0.6 else "bearish" if score < 0.4 else "neutral"
return {"sentiment": sentiment, "score": round(score, 2), "positive": pos, "negative": neg, "total_mentions": total}
async def _search_mentions(symbol: str, source: str = "twitter") -> list[str]:
"""Search for crypto mentions via SearXNG."""
texts = []
try:
async with httpx.AsyncClient(timeout=10) as c:
r = await c.get(
f"{SEARXNG}/search",
params={"q": f"${symbol} crypto {source}", "format": "json", "categories": "social media"},
)
if r.status_code == 200:
results = r.json().get("results", [])
texts = [item.get("content", "") or item.get("title", "") for item in results[:20]]
except Exception:
pass
return texts
@router.get("/token/{symbol}")
async def token_sentiment(symbol: str):
"""Get sentiment for a token across social media."""
texts = await _search_mentions(symbol)
if not texts:
return {"symbol": symbol, "sentiment": "unknown", "note": "No mentions found"}
all_text = " ".join(texts)
sentiment = _score_text(all_text)
sentiment["symbol"] = symbol
sentiment["timestamp"] = datetime.now(UTC).isoformat()
sentiment["sources_scanned"] = len(texts)
# Emoji representation
emoji = "🟢" if sentiment["sentiment"] == "bullish" else "🔴" if sentiment["sentiment"] == "bearish" else ""
sentiment["emoji"] = emoji
return sentiment
@router.get("/market")
async def market_sentiment():
"""Overall crypto market sentiment."""
tickers = ["BTC", "ETH", "SOL"]
results = {}
for ticker in tickers:
texts = await _search_mentions(ticker)
results[ticker] = _score_text(" ".join(texts)) if texts else {"sentiment": "unknown"}
scores = [r["score"] for r in results.values() if r.get("score")]
avg_score = sum(scores) / len(scores) if scores else 0.5
overall = "bullish" if avg_score > 0.55 else "bearish" if avg_score < 0.45 else "neutral"
return {
"overall": overall,
"average_score": round(avg_score, 2),
"breakdown": results,
"emoji": "🟢" if overall == "bullish" else "🔴" if overall == "bearish" else "",
}
@router.get("/trending-signals/{symbol}")
async def sentiment_signal(symbol: str):
"""Quick sentiment signal for trading: BULLISH / BEARISH / NEUTRAL."""
result = await token_sentiment(symbol)
return {
"symbol": symbol,
"signal": result["sentiment"].upper(),
"emoji": result.get("emoji", ""),
"score": result.get("score", 0.5),
}