File size: 2,669 Bytes
d4a4da7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""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.",
        }