File size: 4,162 Bytes
0a7e81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51fe39a
0a7e81a
 
 
 
 
51fe39a
 
 
0a7e81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
Trends Module Router
API endpoints for Google Trends data
"""
import logging
from fastapi import APIRouter, HTTPException

from .schemas import (
    TrendingNowRequest,
    TrendingNowResponse,
    KeywordResearchRequest,
    KeywordResearchResponse,
    YouTubeTrendsRequest,
    TrendingTopic
)
from .services.trends_client import TrendsClient

logger = logging.getLogger(__name__)

router = APIRouter()

# Create client instance
trends_client = TrendsClient()


@router.post("/trending-now",
    response_model=TrendingNowResponse,
    summary="Get trending topics",
    description="Get currently trending searches for a country"
)
async def get_trending_now(request: TrendingNowRequest):
    """
    Get trending searches right now.
    
    - Returns top trending topics sorted by popularity
    - Supports different countries
    """
    try:
        results = trends_client.get_trending_now(
            country=request.country,
            limit=request.limit
        )
        
        return TrendingNowResponse(
            success=True,
            count=len(results),
            trends=[TrendingTopic(**r) for r in results]
        )
        
    except Exception as e:
        logger.error(f"Error getting trending now: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/keyword-research",
    response_model=KeywordResearchResponse,
    summary="Keyword research",
    description="Get related topics and queries for a keyword"
)
async def keyword_research(request: KeywordResearchRequest):
    """
    Complete keyword research - related topics and queries.
    
    - Related Topics: topics related to the keyword
    - Related Queries: search queries people also search
    - Sorted by search volume (highest first)
    """
    try:
        results = trends_client.keyword_research(
            keyword=request.keyword,
            region=request.region,
            timeframe=request.timeframe.value,
            category=request.category.value,
            search_type=request.search_type.value
        )
        
        return KeywordResearchResponse(
            success=True,
            **results
        )
        
    except Exception as e:
        logger.error(f"Error in keyword research: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/youtube-trends",
    response_model=KeywordResearchResponse,
    summary="YouTube trends",
    description="Get YouTube-specific trends for a keyword"
)
async def youtube_trends(request: YouTubeTrendsRequest):
    """
    Get YouTube-specific trends and related content.
    """
    try:
        results = trends_client.get_youtube_trends(
            keyword=request.keyword,
            region=request.region,
            timeframe=request.timeframe.value
        )
        
        return KeywordResearchResponse(
            success=True,
            **results
        )
        
    except Exception as e:
        logger.error(f"Error getting YouTube trends: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/categories",
    summary="List categories",
    description="Get list of available categories"
)
async def list_categories():
    """Get all available trend categories"""
    from .services.trends_client import TrendsClient
    return {
        "categories": list(TrendsClient.CATEGORIES.keys())
    }


@router.get("/countries",
    summary="List countries",
    description="Get list of supported countries"
)
async def list_countries():
    """Get commonly used country codes"""
    return {
        "countries": [
            {"code": "united_states", "name": "United States"},
            {"code": "united_kingdom", "name": "United Kingdom"},
            {"code": "india", "name": "India"},
            {"code": "bangladesh", "name": "Bangladesh"},
            {"code": "japan", "name": "Japan"},
            {"code": "germany", "name": "Germany"},
            {"code": "france", "name": "France"},
            {"code": "brazil", "name": "Brazil"},
            {"code": "canada", "name": "Canada"},
            {"code": "australia", "name": "Australia"},
        ]
    }