sushilideaclan01's picture
refactored the files
d4a4da7
"""Trending topics endpoints."""
from typing import Literal
from fastapi import APIRouter, Depends
from services.auth_dependency import get_current_user
from services.trend_monitor import trend_monitor
from services.current_occasions import get_current_occasions
router = APIRouter(tags=["trends"])
@router.get("/api/trends/{niche}")
async def get_trends(
niche: Literal["home_insurance", "glp1", "auto_insurance"],
username: str = Depends(get_current_user),
):
"""
Get current trending topics for a niche: date-based occasions (e.g. Valentine's Week)
plus niche-specific news. Topics are analyzed from the current date so they stay timely.
"""
try:
data = await trend_monitor.get_relevant_trends_for_niche(niche)
raw = data.get("relevant_trends") or []
# Map to frontend shape: title, description (from summary), category, url
trends = [
{
"title": t.get("title", ""),
"description": t.get("summary", t.get("description", "")),
"category": t.get("category", "General"),
"url": t.get("url"),
}
for t in raw
]
return {
"status": "ok",
"niche": niche,
"trends": trends,
"count": len(trends),
}
except Exception as e:
# Fallback to occasions only if news fails
occasions = await get_current_occasions()
trends = [
{
"title": o["title"],
"description": o["summary"],
"category": o.get("category", "Occasion"),
}
for o in occasions
]
return {
"status": "ok",
"niche": niche,
"trends": trends,
"count": len(trends),
"message": "Showing current occasions (news temporarily unavailable).",
}
@router.get("/api/trends/angles/{niche}")
async def get_trending_angles(
niche: Literal["home_insurance", "glp1", "auto_insurance"],
username: str = Depends(get_current_user),
):
"""
Get auto-generated angle suggestions based on current trends and occasions.
"""
try:
angles = await trend_monitor.get_trending_angles(niche)
return {
"status": "ok",
"niche": niche,
"trending_angles": angles,
"count": len(angles),
}
except Exception as e:
return {
"status": "ok",
"niche": niche,
"trending_angles": [],
"count": 0,
"message": "Angle suggestions temporarily unavailable.",
}