tx3bas commited on
Commit
c8fa91e
·
verified ·
1 Parent(s): 66788dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -172
app.py CHANGED
@@ -3,6 +3,12 @@ import requests
3
  import urllib.parse
4
  import re
5
  import xmltodict
 
 
 
 
 
 
6
 
7
  # Función para obtener sugerencias de DuckDuckGo
8
  def fetch_duckduckgo_suggestions(query, lang_code="es"):
@@ -12,7 +18,7 @@ def fetch_duckduckgo_suggestions(query, lang_code="es"):
12
  if response.status_code == 200:
13
  try:
14
  data = response.json()
15
- return [item['phrase'] for item in data]
16
  except ValueError:
17
  print("Error decodificando JSON de DuckDuckGo")
18
  return []
@@ -26,7 +32,7 @@ def fetch_google_suggestions(query, lang_code="es"):
26
  response = requests.get(url)
27
  if response.status_code == 200:
28
  try:
29
- return response.json()[1]
30
  except ValueError:
31
  print("Error decodificando JSON de Google")
32
  return []
@@ -38,16 +44,12 @@ def fetch_youtube_suggestions(query, lang_code="es"):
38
  encoded_query = urllib.parse.quote(query)
39
  url = f"http://suggestqueries.google.com/complete/search?client=youtube&hl={lang_code}&q={encoded_query}"
40
  response = requests.get(url)
41
-
42
  if response.status_code == 200:
43
  try:
44
- # Extraer las sugerencias del formato window.google.ac.h(["..."])
45
  match = re.search(r'window\.google\.ac\.h\(\["[^"]*",\[(.*?)\],', response.text)
46
  if match:
47
  suggestions_data = match.group(1)
48
- # Extraemos las sugerencias
49
- suggestions = re.findall(r'\["([^"]+)"', suggestions_data)
50
- return suggestions
51
  else:
52
  print("No se encontraron sugerencias en el formato esperado.")
53
  return []
@@ -60,27 +62,17 @@ def fetch_youtube_suggestions(query, lang_code="es"):
60
  # Función para obtener sugerencias de Bing
61
  def fetch_bing_suggestions(query, market="en-US"):
62
  url = "https://api.bing.com/qsml.aspx"
63
- params = {
64
- "Market": market,
65
- "query": query
66
- }
67
- headers = {
68
- "User-agent": "Mozilla/5.0"
69
- }
70
  response = requests.get(url, params=params, headers=headers)
71
-
72
  if response.status_code == 200:
73
  try:
74
  obj = xmltodict.parse(response.content)
75
- suggestList = []
76
- if 'SearchSuggestion' in obj and obj['SearchSuggestion']['Section']:
77
- suggestions = obj['SearchSuggestion']['Section']['Item']
78
- if isinstance(suggestions, list):
79
- for s in suggestions:
80
- suggestList.append(s['Text'])
81
- elif isinstance(suggestions, dict):
82
- suggestList.append(suggestions['Text'])
83
- return suggestList
84
  except Exception as e:
85
  print(f"Error procesando la respuesta de Bing: {e}")
86
  return []
@@ -90,67 +82,27 @@ def fetch_bing_suggestions(query, market="en-US"):
90
  # Función para obtener sugerencias de Amazon
91
  def fetch_amazon_suggestions(query, market_id="ATVPDKIKX0DER", alias="aps"):
92
  url = "https://completion.amazon.com/api/2017/suggestions"
93
- params = {
94
- "mid": market_id,
95
- "alias": alias,
96
- "prefix": query
97
- }
98
  response = requests.get(url, params=params)
99
-
100
  if response.status_code == 200:
101
  try:
102
  data = response.json()
103
- # Extraemos las sugerencias desde el JSON bajo la clave 'suggestions'
104
- return [item['value'] for item in data.get('suggestions', [])]
105
  except ValueError:
106
  print("Error decodificando JSON de Amazon")
107
  return []
108
  else:
109
  return []
110
 
111
- # Lista de stopwords, artículos, términos de comparativas, preposiciones, pronombres y conjunciones comunes en español
112
- stopwords = [
113
- # Artículos
114
- "el", "la", "los", "las", "un", "una", "unos", "unas",
115
-
116
- # Conjunciones y preposiciones
117
- "y", "o", "de", "por", "para", "con", "sin", "a", "en",
118
- "sobre", "desde", "hacia", "hasta", "entre", "contra",
119
- "bajo", "según", "durante", "ante", "tras", "mediante",
120
-
121
- # Términos comunes en comparativas y compras
122
- "vs", "review", "opiniones", "opinion", "caracteristicas",
123
- "amazon", "comparativa", "precio", "comprar", "mejor",
124
- "especificaciones", "barato", "oferta", "descuento",
125
- "envio", "gratis", "analisis", "producto", "gama",
126
- "guía", "pros", "contras", "calidad", "rendimiento",
127
- "valoracion", "reseña", "nuevo", "usado", "rebaja",
128
- "disponible", "más", "menos", "mejor", "peor", "versus",
129
- "tamaño", "peso", "marca", "modelo", "compatible", "accesorios",
130
-
131
- # Otros términos relevantes
132
- "novedades",
133
- "actualización", "diferencias", "similitudes", "competencia",
134
- "comparar", "top", "ranking", "valor", "rendimiento",
135
- "durabilidad", "material", "acabado", "resistente"
136
- ]
137
-
138
- # Función para expandir la palabra clave añadiendo stopwords, artículos y conjunciones
139
- def expand_keyword(keyword):
140
- expanded_keywords = [keyword]
141
- for letter in 'abcdefghijklmnopqrstuvwxyz*_':
142
- expanded_keywords.append(keyword + " " + letter)
143
- expanded_keywords.append(letter + " " + keyword)
144
- # Añadir combinaciones con stopwords, artículos y conjunciones
145
- for stopword in stopwords:
146
- expanded_keywords.append(f"{stopword} {keyword}")
147
- expanded_keywords.append(f"{keyword} {stopword}")
148
- return expanded_keywords
149
 
150
  # Función principal actualizada
151
  def main(keyword):
152
  expanded_keywords = expand_keyword(keyword)
153
- all_suggestions = {}
154
  google_suggestions_all = []
155
  duckduckgo_suggestions_all = []
156
  youtube_suggestions_all = []
@@ -160,128 +112,58 @@ def main(keyword):
160
  # Obtener sugerencias de DuckDuckGo
161
  for exp_keyword in expanded_keywords:
162
  suggestions = fetch_duckduckgo_suggestions(exp_keyword)
163
- duckduckgo_suggestions_all.extend(suggestions) # Agregar todas las sugerencias
164
- for suggestion in suggestions:
165
- if suggestion in all_suggestions:
166
- all_suggestions[suggestion] += 1
167
- else:
168
- all_suggestions[suggestion] = 1
169
 
170
  # Obtener sugerencias de Google
171
  for exp_keyword in expanded_keywords:
172
  suggestions = fetch_google_suggestions(exp_keyword)
173
- google_suggestions_all.extend(suggestions) # Agregar todas las sugerencias
174
- for suggestion in suggestions:
175
- if suggestion in all_suggestions:
176
- all_suggestions[suggestion] += 1
177
- else:
178
- all_suggestions[suggestion] = 1
179
 
180
  # Obtener sugerencias de YouTube
181
  for exp_keyword in expanded_keywords:
182
  suggestions = fetch_youtube_suggestions(exp_keyword)
183
- youtube_suggestions_all.extend(suggestions) # Agregar todas las sugerencias
184
- for suggestion in suggestions:
185
- if suggestion in all_suggestions:
186
- all_suggestions[suggestion] += 1
187
- else:
188
- all_suggestions[suggestion] = 1
189
 
190
  # Obtener sugerencias de Bing
191
  for exp_keyword in expanded_keywords:
192
  suggestions = fetch_bing_suggestions(exp_keyword)
193
  bing_suggestions_all.extend(suggestions)
194
- for suggestion in suggestions:
195
- if suggestion in all_suggestions:
196
- all_suggestions[suggestion] += 1
197
- else:
198
- all_suggestions[suggestion] = 1
199
 
200
  # Obtener sugerencias de Amazon
201
  for exp_keyword in expanded_keywords:
202
  suggestions = fetch_amazon_suggestions(exp_keyword)
203
  amazon_suggestions_all.extend(suggestions)
204
- for suggestion in suggestions:
205
- if suggestion in all_suggestions:
206
- all_suggestions[suggestion] += 1
207
- else:
208
- all_suggestions[suggestion] = 1
209
-
210
- # Filtrar las top 3 de cada plataforma con su número de repeticiones
211
- google_top_3 = list(set(google_suggestions_all))[:3]
212
- duckduckgo_top_3 = list(set(duckduckgo_suggestions_all))[:3]
213
- youtube_top_3 = list(set(youtube_suggestions_all))[:3]
214
- bing_top_3 = list(set(bing_suggestions_all))[:3]
215
- amazon_top_3 = list(set(amazon_suggestions_all))[:3]
216
-
217
- # Ordenar y filtrar las sugerencias más frecuentes combinadas
218
- sorted_suggestions = sorted(all_suggestions.items(), key=lambda item: item[1], reverse=True)
219
- combined_top_10_suggestions = [sug for sug, freq in sorted_suggestions if freq >= 2][:10]
220
- suggestions_str = ", ".join(combined_top_10_suggestions)
221
-
222
- # Crear la lista de todas las palabras clave con su número de repeticiones
223
- all_suggestions_str = "<ul>"
224
- for suggestion, freq in sorted_suggestions:
225
- all_suggestions_str += f"<li>{suggestion} - {freq} repeticiones</li>"
226
- all_suggestions_str += "</ul>"
227
-
228
- # Crear el HTML de salida con un botón de copia, pero mostrando solo el Top 3
229
- html_output = f"""
230
- <div>
231
- <b>Sugerencias combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon (Top 10 combinadas):</b> <span id='suggestions_text'>{suggestions_str}</span>
232
- <button class="lg secondary svelte-cmf5ev" style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;"
233
- onclick='navigator.clipboard.writeText(document.getElementById("suggestions_text").innerText).then(() => alert("Texto copiado al portapapeles"))'>&nbsp;✂&nbsp;</button>
234
- </div>
235
- <h4>Top 3 Sugerencias de Google:</h4>
236
- <ul>
237
- """
238
- for suggestion in google_top_3:
239
- freq = all_suggestions[suggestion]
240
  html_output += f"<li>{suggestion} ({freq})</li>"
241
- html_output += "</ul>"
242
-
243
- html_output += """
244
- <h4>Top 3 Sugerencias de DuckDuckGo:</h4>
245
- <ul>
246
- """
247
- for suggestion in duckduckgo_top_3:
248
- freq = all_suggestions[suggestion]
249
  html_output += f"<li>{suggestion} ({freq})</li>"
250
- html_output += "</ul>"
251
-
252
- html_output += """
253
- <h4>Top 3 Sugerencias de YouTube:</h4>
254
- <ul>
255
- """
256
- for suggestion in youtube_top_3:
257
- freq = all_suggestions[suggestion]
258
  html_output += f"<li>{suggestion} ({freq})</li>"
259
- html_output += "</ul>"
260
-
261
- html_output += """
262
- <h4>Top 3 Sugerencias de Bing:</h4>
263
- <ul>
264
- """
265
- for suggestion in bing_top_3:
266
- freq = all_suggestions[suggestion]
267
  html_output += f"<li>{suggestion} ({freq})</li>"
268
- html_output += "</ul>"
269
-
270
- html_output += """
271
- <h4>Top 3 Sugerencias de Amazon:</h4>
272
- <ul>
273
- """
274
- for suggestion in amazon_top_3:
275
- freq = all_suggestions[suggestion]
276
  html_output += f"<li>{suggestion} ({freq})</li>"
277
  html_output += "</ul>"
278
 
279
- # Agregar la lista completa de todas las palabras clave
280
- html_output += """
281
- <h4>Lista completa de palabras clave con su número de repeticiones:</h4>
282
- """
283
- html_output += all_suggestions_str
284
-
285
  return html_output
286
 
287
  # Interfaz de Gradio
@@ -289,9 +171,9 @@ iface = gr.Interface(
289
  fn=main,
290
  inputs="text",
291
  outputs="html",
292
- 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>",
293
- 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 3 primeras sugerencias combinadas y también las 3 principales de cada plataforma por separado.</p>",
294
- 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>"
295
  )
296
 
297
- iface.launch()
 
3
  import urllib.parse
4
  import re
5
  import xmltodict
6
+ from collections import Counter
7
+ import unicodedata
8
+
9
+ # Función para normalizar palabras clave (elimina tildes y convierte a minúsculas)
10
+ def normalize_keyword(keyword):
11
+ return ''.join(c for c in unicodedata.normalize('NFD', keyword.lower()) if unicodedata.category(c) != 'Mn')
12
 
13
  # Función para obtener sugerencias de DuckDuckGo
14
  def fetch_duckduckgo_suggestions(query, lang_code="es"):
 
18
  if response.status_code == 200:
19
  try:
20
  data = response.json()
21
+ return [normalize_keyword(item['phrase']) for item in data]
22
  except ValueError:
23
  print("Error decodificando JSON de DuckDuckGo")
24
  return []
 
32
  response = requests.get(url)
33
  if response.status_code == 200:
34
  try:
35
+ return [normalize_keyword(suggestion) for suggestion in response.json()[1]]
36
  except ValueError:
37
  print("Error decodificando JSON de Google")
38
  return []
 
44
  encoded_query = urllib.parse.quote(query)
45
  url = f"http://suggestqueries.google.com/complete/search?client=youtube&hl={lang_code}&q={encoded_query}"
46
  response = requests.get(url)
 
47
  if response.status_code == 200:
48
  try:
 
49
  match = re.search(r'window\.google\.ac\.h\(\["[^"]*",\[(.*?)\],', response.text)
50
  if match:
51
  suggestions_data = match.group(1)
52
+ return [normalize_keyword(suggestion) for suggestion in re.findall(r'\["([^"]+)"', suggestions_data)]
 
 
53
  else:
54
  print("No se encontraron sugerencias en el formato esperado.")
55
  return []
 
62
  # Función para obtener sugerencias de Bing
63
  def fetch_bing_suggestions(query, market="en-US"):
64
  url = "https://api.bing.com/qsml.aspx"
65
+ params = {"Market": market, "query": query}
66
+ headers = {"User-agent": "Mozilla/5.0"}
 
 
 
 
 
67
  response = requests.get(url, params=params, headers=headers)
 
68
  if response.status_code == 200:
69
  try:
70
  obj = xmltodict.parse(response.content)
71
+ suggestions = obj['SearchSuggestion']['Section']['Item']
72
+ if isinstance(suggestions, list):
73
+ return [normalize_keyword(s['Text']) for s in suggestions]
74
+ elif isinstance(suggestions, dict):
75
+ return [normalize_keyword(suggestions['Text'])]
 
 
 
 
76
  except Exception as e:
77
  print(f"Error procesando la respuesta de Bing: {e}")
78
  return []
 
82
  # Función para obtener sugerencias de Amazon
83
  def fetch_amazon_suggestions(query, market_id="ATVPDKIKX0DER", alias="aps"):
84
  url = "https://completion.amazon.com/api/2017/suggestions"
85
+ params = {"mid": market_id, "alias": alias, "prefix": query}
 
 
 
 
86
  response = requests.get(url, params=params)
 
87
  if response.status_code == 200:
88
  try:
89
  data = response.json()
90
+ return [normalize_keyword(item['value']) for item in data.get('suggestions', [])]
 
91
  except ValueError:
92
  print("Error decodificando JSON de Amazon")
93
  return []
94
  else:
95
  return []
96
 
97
+ # Función para contar y obtener las top N sugerencias más repetidas
98
+ def get_top_suggestions(suggestions, top_n=3):
99
+ suggestion_counter = Counter(suggestions)
100
+ return suggestion_counter.most_common(top_n)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  # Función principal actualizada
103
  def main(keyword):
104
  expanded_keywords = expand_keyword(keyword)
105
+ all_suggestions = []
106
  google_suggestions_all = []
107
  duckduckgo_suggestions_all = []
108
  youtube_suggestions_all = []
 
112
  # Obtener sugerencias de DuckDuckGo
113
  for exp_keyword in expanded_keywords:
114
  suggestions = fetch_duckduckgo_suggestions(exp_keyword)
115
+ duckduckgo_suggestions_all.extend(suggestions)
116
+ all_suggestions.extend(suggestions)
 
 
 
 
117
 
118
  # Obtener sugerencias de Google
119
  for exp_keyword in expanded_keywords:
120
  suggestions = fetch_google_suggestions(exp_keyword)
121
+ google_suggestions_all.extend(suggestions)
122
+ all_suggestions.extend(suggestions)
 
 
 
 
123
 
124
  # Obtener sugerencias de YouTube
125
  for exp_keyword in expanded_keywords:
126
  suggestions = fetch_youtube_suggestions(exp_keyword)
127
+ youtube_suggestions_all.extend(suggestions)
128
+ all_suggestions.extend(suggestions)
 
 
 
 
129
 
130
  # Obtener sugerencias de Bing
131
  for exp_keyword in expanded_keywords:
132
  suggestions = fetch_bing_suggestions(exp_keyword)
133
  bing_suggestions_all.extend(suggestions)
134
+ all_suggestions.extend(suggestions)
 
 
 
 
135
 
136
  # Obtener sugerencias de Amazon
137
  for exp_keyword in expanded_keywords:
138
  suggestions = fetch_amazon_suggestions(exp_keyword)
139
  amazon_suggestions_all.extend(suggestions)
140
+ all_suggestions.extend(suggestions)
141
+
142
+ # Obtener las top 3 sugerencias de cada plataforma
143
+ google_top_3 = get_top_suggestions(google_suggestions_all, top_n=3)
144
+ duckduckgo_top_3 = get_top_suggestions(duckduckgo_suggestions_all, top_n=3)
145
+ youtube_top_3 = get_top_suggestions(youtube_suggestions_all, top_n=3)
146
+ bing_top_3 = get_top_suggestions(bing_suggestions_all, top_n=3)
147
+ amazon_top_3 = get_top_suggestions(amazon_suggestions_all, top_n=3)
148
+
149
+ # Mostrar los resultados
150
+ html_output = "<h4>Top 3 Sugerencias de Google:</h4><ul>"
151
+ for suggestion, freq in google_top_3:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  html_output += f"<li>{suggestion} ({freq})</li>"
153
+ html_output += "</ul><h4>Top 3 Sugerencias de DuckDuckGo:</h4><ul>"
154
+ for suggestion, freq in duckduckgo_top_3:
 
 
 
 
 
 
155
  html_output += f"<li>{suggestion} ({freq})</li>"
156
+ html_output += "</ul><h4>Top 3 Sugerencias de YouTube:</h4><ul>"
157
+ for suggestion, freq in youtube_top_3:
 
 
 
 
 
 
158
  html_output += f"<li>{suggestion} ({freq})</li>"
159
+ html_output += "</ul><h4>Top 3 Sugerencias de Bing:</h4><ul>"
160
+ for suggestion, freq in bing_top_3:
 
 
 
 
 
 
161
  html_output += f"<li>{suggestion} ({freq})</li>"
162
+ html_output += "</ul><h4>Top 3 Sugerencias de Amazon:</h4><ul>"
163
+ for suggestion, freq in amazon_top_3:
 
 
 
 
 
 
164
  html_output += f"<li>{suggestion} ({freq})</li>"
165
  html_output += "</ul>"
166
 
 
 
 
 
 
 
167
  return html_output
168
 
169
  # Interfaz de Gradio
 
171
  fn=main,
172
  inputs="text",
173
  outputs="html",
174
+ title="Sugerencias Combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon",
175
+ description="Ingrese una palabra clave para obtener sugerencias de búsqueda relacionadas.",
176
+ article="<p>Desarrollada por © Artxe Web</p>"
177
  )
178
 
179
+ iface.launch()