-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyword_research.py
More file actions
301 lines (241 loc) · 10.4 KB
/
keyword_research.py
File metadata and controls
301 lines (241 loc) · 10.4 KB
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
#!/usr/bin/env python3
"""
Keyword research integration for BlogAgents
Supports Google Ads API for keyword data and Google Trends for trend analysis
"""
# Suppress gRPC warnings from Google Ads
import os
os.environ['GRPC_VERBOSITY'] = 'ERROR'
os.environ['GLOG_minloglevel'] = '2'
import streamlit as st
from typing import List, Dict, Optional
from pytrends.request import TrendReq
import time
class KeywordResearcher:
"""Manages keyword research using Google Ads API and Google Trends"""
def __init__(self, google_ads_config: Optional[Dict] = None):
"""
Initialize keyword researcher
Args:
google_ads_config: Optional dict with Google Ads API credentials
{
'developer_token': str,
'client_id': str,
'client_secret': str,
'refresh_token': str,
'customer_id': str
}
"""
self.google_ads_config = google_ads_config
self.google_ads_client = None
# Initialize Google Trends (requires network access)
try:
self.pytrends = TrendReq(hl='en-US', tz=360)
except Exception:
self.pytrends = None
# Initialize Google Ads client if config provided
if google_ads_config:
self._initialize_google_ads()
def _initialize_google_ads(self):
"""Initialize Google Ads API client with service account"""
try:
from google.ads.googleads.client import GoogleAdsClient
import json
import tempfile
import os
# Parse service account JSON
service_account_info = json.loads(self.google_ads_config['service_account_json'])
# Google Ads API requires a file path, so create a temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(service_account_info, f)
temp_file_path = f.name
try:
# Create config dict for Google Ads using service account file
config = {
'developer_token': self.google_ads_config['developer_token'],
'use_proto_plus': True,
'json_key_file_path': temp_file_path,
'login_customer_id': self.google_ads_config['customer_id']
}
# Initialize client
self.google_ads_client = GoogleAdsClient.load_from_dict(config)
finally:
# Clean up temp file
try:
os.unlink(temp_file_path)
except:
pass
except Exception as e:
st.warning(f"⚠️ Could not initialize Google Ads API: {str(e)}")
self.google_ads_client = None
def get_keyword_ideas(self, seed_keywords: List[str], location: str = "US") -> List[Dict]:
"""
Get keyword ideas and search volumes from Google Ads API
Args:
seed_keywords: List of seed keywords to expand
location: Geographic location for keyword data
Returns:
List of dicts with keyword data
"""
if not self.google_ads_client:
return []
try:
from google.ads.googleads.client import GoogleAdsClient
keyword_plan_idea_service = self.google_ads_client.get_service(
"KeywordPlanIdeaService"
)
request = self.google_ads_client.get_type("GenerateKeywordIdeasRequest")
request.customer_id = self.google_ads_config['customer_id']
# Set location (US = 2840)
request.geo_target_constants.append(
keyword_plan_idea_service.geographic_target_constant_path("2840")
)
# Set language (English = 1000)
request.language = keyword_plan_idea_service.language_constant_path("1000")
# Add seed keywords
request.keyword_seed.keywords.extend(seed_keywords)
# Make request
response = keyword_plan_idea_service.generate_keyword_ideas(request=request)
keyword_ideas = []
for idea in response:
keyword_ideas.append({
'keyword': idea.text,
'avg_monthly_searches': idea.keyword_idea_metrics.avg_monthly_searches,
'competition': idea.keyword_idea_metrics.competition.name,
'competition_index': idea.keyword_idea_metrics.competition_index,
'low_top_of_page_bid_micros': idea.keyword_idea_metrics.low_top_of_page_bid_micros,
'high_top_of_page_bid_micros': idea.keyword_idea_metrics.high_top_of_page_bid_micros
})
return keyword_ideas[:50] # Limit to top 50
except Exception as e:
st.warning(f"⚠️ Google Ads API error: {str(e)}")
return []
def get_trend_data(self, keywords: List[str]) -> Dict[str, int]:
"""
Get Google Trends interest scores for keywords
Args:
keywords: List of keywords to check (max 5)
Returns:
Dict mapping keyword to trend score (0-100)
"""
if not self.pytrends:
return {kw: 0 for kw in keywords}
try:
# Google Trends allows max 5 keywords at once
keywords_chunk = keywords[:5]
# Build payload
self.pytrends.build_payload(
keywords_chunk,
cat=0,
timeframe='today 3-m',
geo='US',
gprop=''
)
# Get interest over time
interest_df = self.pytrends.interest_over_time()
if interest_df.empty:
return {kw: 0 for kw in keywords_chunk}
# Calculate average interest for each keyword
trend_scores = {}
for keyword in keywords_chunk:
if keyword in interest_df.columns:
trend_scores[keyword] = int(interest_df[keyword].mean())
else:
trend_scores[keyword] = 0
# Add delay to avoid rate limiting
time.sleep(2)
return trend_scores
except Exception as e:
# Silently fail on rate limits to avoid spam
# Rate limited - wait before next request
time.sleep(5)
return {kw: 0 for kw in keywords}
def get_related_queries(self, keyword: str) -> List[str]:
"""
Get related search queries from Google Trends
Args:
keyword: Seed keyword
Returns:
List of related query strings
"""
if not self.pytrends:
return []
try:
# Add delay before request to avoid rate limiting
time.sleep(2)
self.pytrends.build_payload([keyword], timeframe='today 3-m', geo='US')
related = self.pytrends.related_queries()
if keyword not in related or related[keyword]['top'] is None:
return []
# Get top 10 related queries
top_queries = related[keyword]['top']
if not top_queries.empty:
# Add delay after successful request
time.sleep(1)
return top_queries['query'].head(10).tolist()
return []
except Exception as e:
# Rate limited - wait longer before next request
time.sleep(5)
return []
def enrich_topics_with_keyword_data(self, topics: List[Dict]) -> List[Dict]:
"""
Enrich topic suggestions with keyword research data
Args:
topics: List of topic dicts with 'title' and 'keywords' fields
Returns:
Topics enriched with search volume, competition, and trend data
"""
for topic in topics:
keywords = topic.get('keywords', [])
if not keywords:
# Extract keywords from title
keywords = [word.lower() for word in topic['title'].split() if len(word) > 3][:3]
topic['keywords'] = keywords
# Get trend data (always available)
trend_scores = self.get_trend_data(keywords)
topic['trend_score'] = max(trend_scores.values()) if trend_scores else 0
topic['trend_status'] = self._get_trend_status(topic['trend_score'])
# Get keyword data from Google Ads if available
if self.google_ads_client:
keyword_ideas = self.get_keyword_ideas(keywords)
if keyword_ideas:
# Use the best keyword data
best_keyword = max(keyword_ideas, key=lambda x: x['avg_monthly_searches'])
topic['search_volume'] = best_keyword['avg_monthly_searches']
topic['competition'] = best_keyword['competition']
topic['competition_index'] = best_keyword['competition_index']
else:
topic['search_volume'] = 'N/A'
topic['competition'] = 'N/A'
else:
# Without Google Ads, provide trend-based estimate
# Trend score (0-100) can indicate relative interest
trend_score = topic.get('trend_score', 0)
if trend_score >= 75:
topic['search_volume'] = 'High (trend-based)'
elif trend_score >= 50:
topic['search_volume'] = 'Medium (trend-based)'
elif trend_score >= 25:
topic['search_volume'] = 'Low (trend-based)'
else:
topic['search_volume'] = 'Minimal (trend-based)'
topic['competition'] = 'Enable Google Ads for data'
return topics
def _get_trend_status(self, score: int) -> str:
"""Get trend status emoji based on score"""
if score >= 75:
return "🔥 Hot"
elif score >= 50:
return "📈 Rising"
elif score >= 25:
return "➡️ Steady"
else:
return "📉 Low"
def create_keyword_researcher(google_ads_config: Optional[Dict] = None) -> Optional[KeywordResearcher]:
"""Factory function to create KeywordResearcher with error handling"""
try:
return KeywordResearcher(google_ads_config)
except Exception as e:
st.error(f"Failed to create keyword researcher: {str(e)}")
return None