suggest / app.py
tx3bas's picture
Update app.py
d5d9419 verified
raw
history blame
10.8 kB
import gradio as gr
import requests
import urllib.parse
import re
import xmltodict
import inflect
# Función para obtener sugerencias de DuckDuckGo
def fetch_duckduckgo_suggestions(query, lang_code="es"):
encoded_query = urllib.parse.quote(query)
url = f"https://duckduckgo.com/ac/?q={encoded_query}&kl={lang_code}"
response = requests.get(url)
if response.status_code == 200:
try:
data = response.json()
return [item['phrase'] for item in data]
except ValueError:
print("Error decodificando JSON de DuckDuckGo")
return []
else:
return []
# Función para obtener sugerencias de Google
def fetch_google_suggestions(query, lang_code="es"):
encoded_query = urllib.parse.quote(query)
url = f"http://suggestqueries.google.com/complete/search?client=firefox&hl={lang_code}&q={encoded_query}"
response = requests.get(url)
if response.status_code == 200:
try:
return response.json()[1]
except ValueError:
print("Error decodificando JSON de Google")
return []
else:
return []
# Función para obtener sugerencias de YouTube
def fetch_youtube_suggestions(query, lang_code="es"):
encoded_query = urllib.parse.quote(query)
url = f"http://suggestqueries.google.com/complete/search?client=youtube&hl={lang_code}&q={encoded_query}"
response = requests.get(url)
if response.status_code == 200:
try:
match = re.search(r'window\.google\.ac\.h\(\["[^"]*",\[(.*?)\],', response.text)
if match:
suggestions_data = match.group(1)
suggestions = re.findall(r'\["([^"]+)"', suggestions_data)
return suggestions
else:
print("No se encontraron sugerencias en el formato esperado.")
return []
except Exception as e:
print(f"Error procesando la respuesta de YouTube: {e}")
return []
else:
return []
# Función para obtener sugerencias de Bing
def fetch_bing_suggestions(query, market="en-US"):
url = "https://api.bing.com/qsml.aspx"
params = {
"Market": market,
"query": query
}
headers = {
"User-agent": "Mozilla/5.0"
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
try:
obj = xmltodict.parse(response.content)
suggestList = []
if 'SearchSuggestion' in obj and obj['SearchSuggestion']['Section']:
suggestions = obj['SearchSuggestion']['Section']['Item']
if isinstance(suggestions, list):
for s in suggestions:
suggestList.append(s['Text'])
elif isinstance(suggestions, dict):
suggestList.append(suggestions['Text'])
return suggestList
except Exception as e:
print(f"Error procesando la respuesta de Bing: {e}")
return []
else:
return []
# Función para obtener sugerencias de Amazon
def fetch_amazon_suggestions(query, market_id="ATVPDKIKX0DER", alias="aps"):
url = "https://completion.amazon.com/api/2017/suggestions"
params = {
"mid": market_id,
"alias": alias,
"prefix": query
}
response = requests.get(url, params=params)
if response.status_code == 200:
try:
data = response.json()
return [item['value'] for item in data.get('suggestions', [])]
except ValueError:
print("Error decodificando JSON de Amazon")
return []
else:
return []
# Función para generar plurales y variaciones
def generate_plural_variations(keyword):
p = inflect.engine()
singular = keyword
plural = p.plural(keyword)
return [singular, plural]
# Función para expandir la palabra clave con stopwords y conjunciones
STOP_WORDS = ["de", "en", "por", "para", "con", "sin", "y", "o"]
def expand_keyword_with_stopwords(keyword):
expanded_keywords = [keyword]
for word in STOP_WORDS:
expanded_keywords.append(f"{keyword} {word}")
expanded_keywords.append(f"{word} {keyword}")
return expanded_keywords
# Función para expandir las palabras clave
def expand_keyword(keyword):
expanded_keywords = expand_keyword_with_stopwords(keyword)
expanded_keywords += generate_plural_variations(keyword)
for letter in 'abcdefghijklmnopqrstuvwxyz*_':
expanded_keywords.append(keyword + " " + letter)
expanded_keywords.append(letter + " " + keyword)
return expanded_keywords
# Función principal
def main(keyword):
expanded_keywords = expand_keyword(keyword)
all_suggestions = {}
google_suggestions_all = []
duckduckgo_suggestions_all = []
youtube_suggestions_all = []
bing_suggestions_all = []
amazon_suggestions_all = []
# Obtener sugerencias de cada fuente
for exp_keyword in expanded_keywords:
suggestions = fetch_duckduckgo_suggestions(exp_keyword)
duckduckgo_suggestions_all.extend(suggestions)
for suggestion in suggestions:
if suggestion in all_suggestions:
all_suggestions[suggestion] += 1
else:
all_suggestions[suggestion] = 1
suggestions = fetch_google_suggestions(exp_keyword)
google_suggestions_all.extend(suggestions)
for suggestion in suggestions:
if suggestion in all_suggestions:
all_suggestions[suggestion] += 1
else:
all_suggestions[suggestion] = 1
suggestions = fetch_youtube_suggestions(exp_keyword)
youtube_suggestions_all.extend(suggestions)
for suggestion in suggestions:
if suggestion in all_suggestions:
all_suggestions[suggestion] += 1
else:
all_suggestions[suggestion] = 1
suggestions = fetch_bing_suggestions(exp_keyword)
bing_suggestions_all.extend(suggestions)
for suggestion in suggestions:
if suggestion in all_suggestions:
all_suggestions[suggestion] += 1
else:
all_suggestions[suggestion] = 1
suggestions = fetch_amazon_suggestions(exp_keyword)
amazon_suggestions_all.extend(suggestions)
for suggestion in suggestions:
if suggestion in all_suggestions:
all_suggestions[suggestion] += 1
else:
all_suggestions[suggestion] = 1
# Filtrar las top 10 sugerencias de cada plataforma
google_top_10 = list(set(google_suggestions_all))[:10]
duckduckgo_top_10 = list(set(duckduckgo_suggestions_all))[:10]
youtube_top_10 = list(set(youtube_suggestions_all))[:10]
bing_top_10 = list(set(bing_suggestions_all))[:10]
amazon_top_10 = list(set(amazon_suggestions_all))[:10]
# Ordenar y filtrar las sugerencias combinadas
sorted_suggestions = sorted(all_suggestions.items(), key=lambda item: item[1], reverse=True)
combined_top_10_suggestions = [sug for sug, freq in sorted_suggestions if freq >= 2][:10]
suggestions_str = ", ".join(combined_top_10_suggestions)
# Crear la lista de todas las palabras clave con su número de repeticiones
all_suggestions_str = "<ul>"
for suggestion, freq in sorted_suggestions:
all_suggestions_str += f"<li>{suggestion} - {freq} repeticiones</li>"
all_suggestions_str += "</ul>"
# Crear el HTML de salida con un botón de copia
html_output = f"""
<div>
<b>Sugerencias combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon (Top 10 combinadas):</b> <span id='suggestions_text'>{suggestions_str}</span>
<button class="lg secondary svelte-cmf5ev" style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;"
onclick='navigator.clipboard.writeText(document.getElementById("suggestions_text").innerText).then(() => alert("Texto copiado al portapapeles"))'>&nbsp;✂&nbsp;</button>
</div>
<h4>Top 10 Sugerencias de Google:</h4>
<ul>
"""
for suggestion in google_top_10:
freq = all_suggestions[suggestion]
html_output += f"<li>{suggestion} ({freq})</li>"
html_output += "</ul>"
html_output += """
<h4>Top 10 Sugerencias de DuckDuckGo:</h4>
<ul>
"""
for suggestion in duckduckgo_top_10:
freq = all_suggestions[suggestion]
html_output += f"<li>{suggestion} ({freq})</li>"
html_output += "</ul>"
html_output += """
<h4>Top 10 Sugerencias de YouTube:</h4>
<ul>
"""
for suggestion in youtube_top_10:
freq = all_suggestions[suggestion]
html_output += f"<li>{suggestion} ({freq})</li>"
html_output += "</ul>"
html_output += """
<h4>Top 10 Sugerencias de Bing:</h4>
<ul>
"""
for suggestion in bing_top_10:
freq = all_suggestions[suggestion]
html_output += f"<li>{suggestion} ({freq})</li>"
html_output += "</ul>"
html_output += """
<h4>Top 10 Sugerencias de Amazon:</h4>
<ul>
"""
for suggestion in amazon_top_10:
freq = all_suggestions[suggestion]
html_output += f"<li>{suggestion} ({freq})</li>"
html_output += "</ul>"
# Agregar la lista completa de todas las palabras clave
html_output += """
<h4>Lista completa de palabras clave con su número de repeticiones:</h4>
"""
html_output += all_suggestions_str
return html_output
# Interfaz de Gradio
iface = gr.Interface(
fn=main,
inputs="text",
outputs="html",
title="<div style='margin:0 auto;text-align:center'><div style='margin:0 auto;text-align:center'><img style='width:100px;display: inline-table;margin-bottom:-10px' src='https://artxeweb.com/media/files/search.jpg'><p>Sugerencias Combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon</p></div>",
description="<p style='margin-bottom:10px;text-align:center;background: #ffffff; padding: 8px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb;'>Ingrese una palabra clave para obtener sugerencias de búsqueda relacionadas de Google, DuckDuckGo, YouTube, Bing y Amazon. Se mostrarán las 10 primeras sugerencias combinadas y también las 10 principales de cada plataforma por separado.</p>",
article="<div style='margin-top:10px'><p style='text-align: center !important; background: #ffffff; padding: 5px 30px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb; width: fit-content; margin: auto;'>Desarrollada por <a style='text-decoration: none !important; color: #e12a31 !important;' href='https://artxeweb.com'>© Artxe Web</a></p></div>"
)
iface.launch()