All checks were successful
continuous-integration/drone/push Build is passing
- Pin curl_cffi==0.7.4 to avoid BoringSSL bug in 0.12-0.14 - Patch curl_cffi Session to use safari TLS fingerprint instead of chrome, which triggers SSL_ERROR_SYSCALL on some networks - Register FRED API key with OpenBB credentials at startup - Fix macro overview to return latest data instead of oldest, and extract values by FRED series ID key - Replace Finnhub upgrades endpoint (premium-only) with yfinance upgrades_downgrades which includes price target changes - Remove redundant curl_cffi upgrade from Dockerfile
104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
"""Routes for sentiment, insider trades, and analyst data (Finnhub + Alpha Vantage)."""
|
|
|
|
import asyncio
|
|
|
|
from fastapi import APIRouter, Path, Query
|
|
|
|
from models import ApiResponse
|
|
from route_utils import safe, validate_symbol
|
|
import alphavantage_service
|
|
import finnhub_service
|
|
import openbb_service
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(prefix="/api/v1")
|
|
|
|
|
|
# --- Sentiment & News ---
|
|
|
|
|
|
@router.get("/stock/{symbol}/sentiment", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_sentiment(symbol: str = Path(..., min_length=1, max_length=20)):
|
|
"""Get aggregated sentiment: Alpha Vantage news sentiment + Finnhub analyst data."""
|
|
symbol = validate_symbol(symbol)
|
|
finnhub_data, av_data = await asyncio.gather(
|
|
finnhub_service.get_sentiment_summary(symbol),
|
|
alphavantage_service.get_news_sentiment(symbol, limit=20),
|
|
return_exceptions=True,
|
|
)
|
|
if isinstance(finnhub_data, BaseException):
|
|
logger.exception("Finnhub error", exc_info=finnhub_data)
|
|
finnhub_data = {}
|
|
if isinstance(av_data, BaseException):
|
|
logger.exception("Alpha Vantage error", exc_info=av_data)
|
|
av_data = {}
|
|
|
|
data = {**finnhub_data, "alpha_vantage_sentiment": av_data}
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/news-sentiment", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_news_sentiment(
|
|
symbol: str = Path(..., min_length=1, max_length=20),
|
|
limit: int = Query(default=30, ge=1, le=200),
|
|
):
|
|
"""Get news articles with per-ticker sentiment scores (Alpha Vantage)."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await alphavantage_service.get_news_sentiment(symbol, limit=limit)
|
|
return ApiResponse(data=data)
|
|
|
|
|
|
@router.get("/stock/{symbol}/insider-trades", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_insider_trades(symbol: str = Path(..., min_length=1, max_length=20)):
|
|
"""Get insider transactions (CEO/CFO buys and sells)."""
|
|
symbol = validate_symbol(symbol)
|
|
raw = await finnhub_service.get_insider_transactions(symbol)
|
|
trades = [
|
|
{
|
|
"name": t.get("name"),
|
|
"shares": t.get("share"),
|
|
"change": t.get("change"),
|
|
"transaction_date": t.get("transactionDate"),
|
|
"transaction_code": t.get("transactionCode"),
|
|
"transaction_price": t.get("transactionPrice"),
|
|
"filing_date": t.get("filingDate"),
|
|
}
|
|
for t in raw[:20]
|
|
]
|
|
return ApiResponse(data=trades)
|
|
|
|
|
|
@router.get("/stock/{symbol}/recommendations", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_recommendations(symbol: str = Path(..., min_length=1, max_length=20)):
|
|
"""Get analyst recommendation trends (monthly buy/hold/sell counts)."""
|
|
symbol = validate_symbol(symbol)
|
|
raw = await finnhub_service.get_recommendation_trends(symbol)
|
|
recs = [
|
|
{
|
|
"period": r.get("period"),
|
|
"strong_buy": r.get("strongBuy"),
|
|
"buy": r.get("buy"),
|
|
"hold": r.get("hold"),
|
|
"sell": r.get("sell"),
|
|
"strong_sell": r.get("strongSell"),
|
|
}
|
|
for r in raw[:12]
|
|
]
|
|
return ApiResponse(data=recs)
|
|
|
|
|
|
@router.get("/stock/{symbol}/upgrades", response_model=ApiResponse)
|
|
@safe
|
|
async def stock_upgrades(symbol: str = Path(..., min_length=1, max_length=20)):
|
|
"""Get recent analyst upgrades and downgrades (via yfinance)."""
|
|
symbol = validate_symbol(symbol)
|
|
data = await openbb_service.get_upgrades_downgrades(symbol)
|
|
return ApiResponse(data=data)
|