-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbomber.py
123 lines (103 loc) · 5.06 KB
/
bomber.py
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
import requests
import httpx
import xml.etree.ElementTree as ET
import prompts
import llm
LLM_MODEL = "gpt-3.5-turbo-1106"
async def get_keyword_data(input_keyword, input_country,api_key):
# Get results
keyword_data = await get_suggestion_keywords_google_optimized(
input_keyword, input_country
)
ai_report = await suggestions_ai_analysis(keyword_data,api_key)
# Preparing the result
result = {
"success": True,
"message": "Success! Keywords Generated",
"result": {
"ai_report": ai_report,
"keyword_data": keyword_data,
},
}
# Print the result
print(result)
return result
async def suggestions_ai_analysis(keyword_data: str, api_key):
max_retries = 5
for _ in range(max_retries):
try:
# Get Json Structure
prompt = prompts.suggestion_keywords_analysis.format(
KEYWORD_DATA=keyword_data
)
return await llm.generate_response(prompt, LLM_MODEL, api_key)
except Exception as e:
print(
f"Failed to generate analysis for suggestion keywords. Exception type: {type(e).__name__}, Message: {str(e)}"
)
return ""
async def get_suggestion_keywords_google_optimized(query, countryCode):
# Define categorization keywords for all categories
categories = {
"Questions": ["who", "what", "where", "when", "why", "how", "are"],
"Prepositions": ["can", "with", "for"],
"Alphabit": list("abcdefghijklmnopqrstuvwxyz"),
"Comparisons": ["vs", "versus", "or"],
"Intent-Based": ["buy", "review", "price", "best", "top", "how to", "why to"],
"Time-Related": ["when", "schedule", "deadline", "today", "now", "latest"],
"Audience-Specific": ["for beginners", "for small businesses", "for students", "for professionals"],
"Problem-Solving": ["solution", "issue", "error", "troubleshoot", "fix"],
"Feature-Specific": ["with video", "with images", "analytics", "tools", "with example"],
"Opinions/Reviews": ["review", "opinion", "rating", "feedback", "testimonial"],
"Cost-Related": ["price", "cost", "budget", "cheap", "expensive", "value"],
"Trend-Based": ["trends", "new", "upcoming"]
}
categorized_suggestions = {key: {} for key in categories.keys()}
for category in categories:
for keyword in categories[category]:
try:
if category in ["Questions", "Prepositions", "Intent-Based", "Time-Related",
"Audience-Specific", "Problem-Solving", "Opinions/Reviews", "Cost-Related", "Trend-Based"]:
modified_query = f"{keyword} {query}"
elif category in ["Alphabit", "Feature-Specific", "Industry-Specific"]:
modified_query = f"{query} {keyword}"
else:
# Default pattern if not specified above
modified_query = f"{keyword} {query}"
category_suggestions = await get_suggestions_for_query_async(modified_query,countryCode)
categorized_suggestions[category][keyword] = category_suggestions
except Exception as e:
print (f"Error in get_suggestion_keywords_google_optimized, {e}")
return categorized_suggestions
async def get_suggestions_for_query_async(query,country):
async with httpx.AsyncClient() as client:
try:
response = await client.get(f"http://google.com/complete/search?output=toolbar&gl={country}&q={query}")
suggestions = []
if response.status_code == 200:
root = ET.fromstring(response.content)
for complete_suggestion in root.findall('CompleteSuggestion'):
suggestion_element = complete_suggestion.find('suggestion')
if suggestion_element is not None:
data = suggestion_element.get('data').lower()
suggestions.append(data)
except Exception as e:
#nothing
error = e
return suggestions
def get_suggestions_for_query(query):
response = requests.get(f"http://google.com/complete/search?output=toolbar&q={query}")
suggestions = []
try:
if response.status_code == 200:
root = ET.fromstring(response.content)
for complete_suggestion in root.findall('CompleteSuggestion'):
suggestion_element = complete_suggestion.find('suggestion')
if suggestion_element is not None:
data = suggestion_element.get('data').lower()
suggestions.append(data)
except Exception as e:
print(
f"keyword_research: get_suggestions_for_query. Exception type: {type(e).__name__}, Message: {str(e)}"
)
return suggestions