rmi-backend/app/_archive/legacy_2026_07/news_intelligence.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

165 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""
RMI News Intelligence v3 - Industry Best
=========================================
AI-powered news pipeline: categorization, sentiment, trending, briefing.
Uses MiniMax ($20/mo flat) + Ollama Cloud.
"""
import json
import logging
import os
import urllib.request
from collections import Counter
from datetime import UTC, datetime
logger = logging.getLogger("rmi.news_v3")
OLLAMA_KEY = os.getenv("OLLAMA_API_KEY", os.getenv("DEEPSEEK_API_KEY", ""))
OLLAMA_URL = "https://ollama.com/v1/chat/completions"
# Simple in-memory trending tracker
_trending_topics = Counter()
_breaking_alerts = []
def analyze_article(title: str, content: str = "") -> dict:
"""Full AI analysis of a news article."""
text = f"{title} {content[:300]}"
# Category (fast, cached)
from app.ai_pipeline_v3 import classify_news
category = classify_news(title, content)
# Sentiment via MiniMax (batched - 1 call per article is fine at flat rate)
sentiment = "neutral"
try:
k = os.getenv("OLLAMA_API_KEY", "")
if not k:
with open("/app/.env") as f:
for line in f:
if line.startswith("OLLAMA_API_KEY"):
k = line.strip().split("=", 1)[1]
break
if not k:
return {"category": category, "sentiment": "neutral", "is_breaking": False}
body = json.dumps(
{
"model": "deepseek-v4-flash",
"messages": [
{
"role": "system",
"content": "Classify sentiment: BULLISH BEARISH NEUTRAL. Reply one word only.",
},
{"role": "user", "content": text[:400]},
],
"max_tokens": 10,
"temperature": 0.1,
}
).encode()
req = urllib.request.Request(
OLLAMA_URL,
data=body,
headers={"Authorization": f"Bearer {k}", "Content-Type": "application/json"},
)
resp = urllib.request.urlopen(req, timeout=8)
sentiment = json.loads(resp.read())["choices"][0]["message"]["content"].strip().upper()
if "BULL" in sentiment:
sentiment = "bullish"
elif "BEAR" in sentiment:
sentiment = "bearish"
else:
sentiment = "neutral"
except Exception as e:
logger.warning(f"Sentiment failed: {e}")
# Track trending topics
for word in title.lower().split():
if len(word) > 4 and word not in (
"after",
"before",
"while",
"could",
"would",
"should",
"their",
"there",
"these",
"those",
"about",
"which",
):
_trending_topics[word] += 1
# Detect breaking news
is_breaking = category == "SCAM" or any(
w in text.lower() for w in ["hacked", "exploited", "drained", "rug pulled", "emergency"]
)
return {
"category": category,
"sentiment": sentiment,
"is_breaking": is_breaking,
"analyzed_at": datetime.now(UTC).isoformat(),
}
def get_trending(limit: int = 10) -> list:
"""Get trending topics from recent article analysis."""
return [{"topic": word, "count": count} for word, count in _trending_topics.most_common(limit)]
def get_breaking() -> list:
"""Get breaking news alerts."""
return _breaking_alerts[-10:]
def daily_briefing() -> str:
"""Generate an AI-powered daily news briefing using Ollama Cloud."""
trending = get_trending(5)
topics = ", ".join(f"{t['topic']}({t['count']})" for t in trending)
k = OLLAMA_KEY
body = json.dumps(
{
"model": "deepseek-v4-flash",
"messages": [
{
"role": "system",
"content": "Write a 3-sentence daily crypto news briefing. Mention trending topics. Professional tone. Under 100 words.",
},
{"role": "user", "content": f"Trending topics: {topics}"},
],
"max_tokens": 150,
"temperature": 0.5,
}
).encode()
try:
req = urllib.request.Request(
OLLAMA_URL,
data=body,
headers={"Authorization": f"Bearer {k}", "Content-Type": "application/json"},
)
resp = urllib.request.urlopen(req, timeout=15)
return json.loads(resp.read())["choices"][0]["message"]["content"].strip()
except Exception:
return f"Daily briefing: {topics} are trending in crypto news today."
def news_search(query: str, articles: list, limit: int = 10) -> list:
"""Smart search across articles with relevance ranking."""
results = []
q_lower = query.lower()
for a in articles:
score = 0
if q_lower in a.get("title", "").lower():
score += 10
if q_lower in a.get("content", "").lower():
score += 5
if q_lower in a.get("category", "").lower():
score += 3
if score > 0:
a["relevance"] = score
results.append(a)
return sorted(results, key=lambda x: x.get("relevance", 0), reverse=True)[:limit]