File size: 12,726 Bytes
0a7e81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e9e349
 
0a7e81a
 
 
 
 
 
 
 
0e9e349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7e81a
0e9e349
 
 
 
 
 
 
 
0a7e81a
 
0e9e349
0a7e81a
0e9e349
 
 
 
 
 
 
 
 
 
 
 
 
0a7e81a
 
 
0e9e349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
Trends Client - PyTrends Wrapper
Provides Google Trends and YouTube Trends data
"""
import logging
from typing import Optional, List, Dict
from pytrends.request import TrendReq
import pandas as pd

logger = logging.getLogger(__name__)


class TrendsClient:
    """
    Client for Google Trends data using pytrends.
    
    Features:
    - Trending Now (daily trends)
    - Related Topics
    - Related Queries
    - YouTube Trends
    """
    
    # Category codes
    CATEGORIES = {
        "all": 0,
        "arts_entertainment": 3,
        "autos_vehicles": 47,
        "beauty_fitness": 44,
        "books_literature": 22,
        "business_industrial": 12,
        "computers_electronics": 5,
        "finance": 7,
        "food_drink": 71,
        "games": 8,
        "health": 45,
        "hobbies_leisure": 65,
        "home_garden": 11,
        "internet_telecom": 13,
        "jobs_education": 958,
        "law_government": 19,
        "news": 16,
        "online_communities": 299,
        "people_society": 14,
        "pets_animals": 66,
        "real_estate": 29,
        "reference": 533,
        "science": 174,
        "shopping": 18,
        "sports": 20,
        "travel": 67,
    }
    
    # Timeframe options
    TIMEFRAMES = {
        "now_1h": "now 1-H",
        "now_4h": "now 4-H",
        "now_1d": "now 1-d",
        "now_7d": "now 7-d",
        "today_1m": "today 1-m",
        "today_3m": "today 3-m",
        "today_12m": "today 12-m",
        "today_5y": "today 5-y",
    }
    
    def __init__(self, hl: str = "en-US", tz: int = 360):
        """
        Initialize TrendsClient.
        
        Args:
            hl: Host language
            tz: Timezone offset
        """
        self.hl = hl
        self.tz = tz
        self.pytrends = TrendReq(hl=hl, tz=tz)
        
    def get_trending_now(
        self,
        country: str = "united_states",
        limit: int = 20
    ) -> List[Dict]:
        """
        Get currently trending searches using Google Trends RSS feed.
        More reliable than pytrends API for daily trends.
        
        Args:
            country: Country code (e.g., 'united_states', 'bangladesh', 'india')
            limit: Number of results (default 20)
            
        Returns:
            List of trending topics with rank
        """
        import requests
        import xml.etree.ElementTree as ET
        
        # Country code mapping for RSS
        country_codes = {
            "united_states": "US",
            "united_kingdom": "GB",
            "india": "IN",
            "bangladesh": "BD",
            "japan": "JP",
            "germany": "DE",
            "france": "FR",
            "brazil": "BR",
            "canada": "CA",
            "australia": "AU",
        }
        
        geo = country_codes.get(country, "US")
        
        try:
            # Google Trends RSS Feed URL
            rss_url = f"https://trends.google.com/trending/rss?geo={geo}"
            
            response = requests.get(rss_url, timeout=10)
            response.raise_for_status()
            
            # Parse XML
            root = ET.fromstring(response.content)
            
            results = []
            items = root.findall('.//item')
            
            for i, item in enumerate(items[:limit]):
                title = item.find('title')
                traffic = item.find('{https://trends.google.com/trending/rss}approx_traffic')
                
                if title is not None:
                    results.append({
                        "rank": i + 1,
                        "topic": title.text,
                        "country": country,
                        "traffic": traffic.text if traffic is not None else "N/A"
                    })
            
            logger.info(f"Got {len(results)} trending topics for {country} via RSS")
            return results
            
        except Exception as e:
            logger.error(f"Error getting trending searches via RSS: {e}")
            # Fallback to pytrends
            try:
                df = self.pytrends.trending_searches(pn=country)
                results = []
                for i, topic in enumerate(df[0].head(limit).tolist()):
                    results.append({
                        "rank": i + 1,
                        "topic": topic,
                        "country": country,
                        "traffic": "N/A"
                    })
                logger.info(f"Got {len(results)} trending topics via pytrends fallback")
                return results
            except Exception as e2:
                logger.error(f"Fallback also failed: {e2}")
                return []
    
    def get_realtime_trends(
        self,
        country: str = "US",
        category: str = "all",
        limit: int = 20
    ) -> List[Dict]:
        """
        Get realtime trending stories.
        
        Args:
            country: Country code (US, BD, IN, etc.)
            category: Category name
            limit: Number of results
            
        Returns:
            List of trending stories
        """
        try:
            cat_code = self.CATEGORIES.get(category, 0)
            
            df = self.pytrends.realtime_trending_searches(pn=country)
            
            results = []
            if not df.empty:
                for i, row in df.head(limit).iterrows():
                    results.append({
                        "rank": i + 1,
                        "title": row.get('title', ''),
                        "entity_names": row.get('entityNames', []),
                        "articles": row.get('articles', [])
                    })
            
            logger.info(f"Got {len(results)} realtime trends")
            return results
            
        except Exception as e:
            logger.error(f"Error getting realtime trends: {e}")
            return []
    
    def get_related_topics(
        self,
        keyword: str,
        region: str = "",
        timeframe: str = "today 12-m",
        category: int = 0,
        search_type: str = ""
    ) -> Dict:
        """
        Get related topics for a keyword.
        
        Args:
            keyword: Search keyword
            region: Region code (empty for worldwide)
            timeframe: Time range
            category: Category code
            search_type: 'youtube', 'news', 'images', 'froogle' or '' for web
            
        Returns:
            Dict with 'top' and 'rising' topics
        """
        try:
            self.pytrends.build_payload(
                kw_list=[keyword],
                cat=category,
                timeframe=timeframe,
                geo=region,
                gprop=search_type
            )
            
            data = self.pytrends.related_topics()
            
            result = {
                "keyword": keyword,
                "top": [],
                "rising": []
            }
            
            if keyword in data:
                topic_data = data[keyword]
                
                # Top topics
                if 'top' in topic_data and topic_data['top'] is not None:
                    top_df = topic_data['top']
                    for _, row in top_df.iterrows():
                        result["top"].append({
                            "topic": row.get('topic_title', ''),
                            "type": row.get('topic_type', ''),
                            "value": int(row.get('value', 0))
                        })
                
                # Rising topics
                if 'rising' in topic_data and topic_data['rising'] is not None:
                    rising_df = topic_data['rising']
                    for _, row in rising_df.iterrows():
                        result["rising"].append({
                            "topic": row.get('topic_title', ''),
                            "type": row.get('topic_type', ''),
                            "value": str(row.get('value', ''))
                        })
            
            # Sort by value
            result["top"] = sorted(result["top"], key=lambda x: x["value"], reverse=True)
            
            logger.info(f"Got {len(result['top'])} top and {len(result['rising'])} rising topics")
            return result
            
        except Exception as e:
            logger.error(f"Error getting related topics: {e}")
            return {"keyword": keyword, "top": [], "rising": [], "error": str(e)}
    
    def get_related_queries(
        self,
        keyword: str,
        region: str = "",
        timeframe: str = "today 12-m",
        category: int = 0,
        search_type: str = ""
    ) -> Dict:
        """
        Get related queries for a keyword.
        
        Args:
            keyword: Search keyword
            region: Region code (empty for worldwide)
            timeframe: Time range
            category: Category code
            search_type: 'youtube', 'news', 'images', 'froogle' or '' for web
            
        Returns:
            Dict with 'top' and 'rising' queries
        """
        try:
            self.pytrends.build_payload(
                kw_list=[keyword],
                cat=category,
                timeframe=timeframe,
                geo=region,
                gprop=search_type
            )
            
            data = self.pytrends.related_queries()
            
            result = {
                "keyword": keyword,
                "top": [],
                "rising": []
            }
            
            if keyword in data:
                query_data = data[keyword]
                
                # Top queries
                if 'top' in query_data and query_data['top'] is not None:
                    top_df = query_data['top']
                    for _, row in top_df.iterrows():
                        result["top"].append({
                            "query": row.get('query', ''),
                            "value": int(row.get('value', 0))
                        })
                
                # Rising queries
                if 'rising' in query_data and query_data['rising'] is not None:
                    rising_df = query_data['rising']
                    for _, row in rising_df.iterrows():
                        result["rising"].append({
                            "query": row.get('query', ''),
                            "value": str(row.get('value', ''))
                        })
            
            # Sort by value
            result["top"] = sorted(result["top"], key=lambda x: x["value"], reverse=True)
            
            logger.info(f"Got {len(result['top'])} top and {len(result['rising'])} rising queries")
            return result
            
        except Exception as e:
            logger.error(f"Error getting related queries: {e}")
            return {"keyword": keyword, "top": [], "rising": [], "error": str(e)}
    
    def keyword_research(
        self,
        keyword: str,
        region: str = "",
        timeframe: str = "today 12-m",
        category: str = "all",
        search_type: str = "web"
    ) -> Dict:
        """
        Complete keyword research - combines related topics and queries.
        
        Args:
            keyword: Search keyword
            region: Region code (empty for worldwide)
            timeframe: Time range key
            category: Category name
            search_type: 'web', 'youtube', 'news', 'images', 'shopping'
            
        Returns:
            Combined dict with topics and queries
        """
        # Convert params
        cat_code = self.CATEGORIES.get(category, 0)
        tf = self.TIMEFRAMES.get(timeframe, "today 12-m")
        gprop = "" if search_type == "web" else search_type
        
        # Get both
        topics = self.get_related_topics(keyword, region, tf, cat_code, gprop)
        queries = self.get_related_queries(keyword, region, tf, cat_code, gprop)
        
        return {
            "keyword": keyword,
            "region": region if region else "worldwide",
            "timeframe": timeframe,
            "category": category,
            "search_type": search_type,
            "related_topics": topics,
            "related_queries": queries
        }
    
    def get_youtube_trends(
        self,
        keyword: str,
        region: str = "",
        timeframe: str = "today 12-m"
    ) -> Dict:
        """
        Get YouTube-specific trends for a keyword.
        """
        return self.keyword_research(
            keyword=keyword,
            region=region,
            timeframe=timeframe,
            category="all",
            search_type="youtube"
        )