tx3bas commited on
Commit
3c9b5a0
·
verified ·
1 Parent(s): 40ee9ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -31,16 +31,21 @@ def buscar_google(query, hl='es', num_results=10):
31
  return serp_data
32
 
33
  def identificar_urls_comunes(serps):
34
- # Extraer todas las URLs de todas las búsquedas
35
  all_urls = [url for serp in serps for url in serp]
36
-
37
- # Identificar URLs que aparecen en más de una búsqueda
38
  urls_comunes = {url for url in all_urls if all_urls.count(url) > 1}
39
-
40
  return urls_comunes
41
 
 
 
 
 
 
 
 
 
 
 
42
  def generar_html_con_colores(serp_results, urls_comunes):
43
- # Asignar un color único a cada URL común
44
  url_color_map = {url: color_palette[i % len(color_palette)] for i, url in enumerate(urls_comunes)}
45
 
46
  html_table = "<style>td, th {border: 1px solid #ddd; text-align: left; padding: 8px;} tr:nth-child(even) {background-color: #f2f2f2;} th {padding-top: 11px; padding-bottom: 11px; background-color: #4CAF50; color: white;}</style>"
@@ -59,11 +64,7 @@ def generar_html_con_colores(serp_results, urls_comunes):
59
  if i < len(serp_results[keyword]):
60
  entry = serp_results[keyword][i]
61
  url = entry["URL"]
62
- if url in urls_comunes:
63
- color = url_color_map[url]
64
- display_url = f"<a href='{url}' target='_blank' style='color:{color};'>{url}</a>"
65
- else:
66
- display_url = f"<a href='{url}' target='_blank'>{url}</a>"
67
  html_table += f"<td>{display_url}</td>"
68
  else:
69
  html_table += "<td></td>"
@@ -75,20 +76,25 @@ def analyze_keywords(keywords):
75
  keywords_list = [keyword.strip() for keyword in keywords.split(',')]
76
  serp_results = {keyword: buscar_google(keyword) for keyword in keywords_list}
77
 
78
- # Extraer solo las URLs de cada búsqueda para identificar comunes
79
  urls_por_keyword = [[entry["URL"] for entry in serp] for serp in serp_results.values()]
80
  urls_comunes = identificar_urls_comunes(urls_por_keyword)
81
 
82
  html_table = generar_html_con_colores(serp_results, urls_comunes)
83
 
84
- return html_table
 
 
 
 
 
 
85
 
86
  iface = gr.Interface(
87
  fn=analyze_keywords,
88
  inputs="text",
89
- outputs="html",
90
  title="Comparador de Keywords en Google",
91
- description="Introduce las keywords separadas por comas para encontrar coincidencias en los resultados de búsqueda de Google. Las URLs comunes entre las búsquedas se resaltarán en colores únicos."
92
  )
93
 
94
- iface.launch()
 
31
  return serp_data
32
 
33
  def identificar_urls_comunes(serps):
 
34
  all_urls = [url for serp in serps for url in serp]
 
 
35
  urls_comunes = {url for url in all_urls if all_urls.count(url) > 1}
 
36
  return urls_comunes
37
 
38
+ def calcular_porcentajes_similaridad(serp_results):
39
+ urls_por_keyword = [set(entry["URL"] for entry in serp) for serp in serp_results.values()]
40
+ porcentajes = {}
41
+ for combo in combinations(serp_results.keys(), 2):
42
+ intersection = len(urls_por_keyword[list(serp_results.keys()).index(combo[0])] & urls_por_keyword[list(serp_results.keys()).index(combo[1])])
43
+ union = len(urls_por_keyword[list(serp_results.keys()).index(combo[0])] | urls_por_keyword[list(serp_results.keys()).index(combo[1])])
44
+ porcentaje = (intersection / union) * 100 if union != 0 else 0
45
+ porcentajes[combo] = porcentaje
46
+ return porcentajes
47
+
48
  def generar_html_con_colores(serp_results, urls_comunes):
 
49
  url_color_map = {url: color_palette[i % len(color_palette)] for i, url in enumerate(urls_comunes)}
50
 
51
  html_table = "<style>td, th {border: 1px solid #ddd; text-align: left; padding: 8px;} tr:nth-child(even) {background-color: #f2f2f2;} th {padding-top: 11px; padding-bottom: 11px; background-color: #4CAF50; color: white;}</style>"
 
64
  if i < len(serp_results[keyword]):
65
  entry = serp_results[keyword][i]
66
  url = entry["URL"]
67
+ display_url = f"<a href='{url}' target='_blank' style='color:{url_color_map[url] if url in urls_comunes else '#000'};'>{url}</a>"
 
 
 
 
68
  html_table += f"<td>{display_url}</td>"
69
  else:
70
  html_table += "<td></td>"
 
76
  keywords_list = [keyword.strip() for keyword in keywords.split(',')]
77
  serp_results = {keyword: buscar_google(keyword) for keyword in keywords_list}
78
 
 
79
  urls_por_keyword = [[entry["URL"] for entry in serp] for serp in serp_results.values()]
80
  urls_comunes = identificar_urls_comunes(urls_por_keyword)
81
 
82
  html_table = generar_html_con_colores(serp_results, urls_comunes)
83
 
84
+ porcentajes_similaridad = calcular_porcentajes_similaridad(serp_results)
85
+ similaridad_html = "<div><strong>Porcentajes de Similaridad:</strong><br>"
86
+ for combo, porcentaje in porcentajes_similaridad.items():
87
+ similaridad_html += f"{combo[0]} & {combo[1]}: {porcentaje:.2f}%<br>"
88
+ similaridad_html += "</div>"
89
+
90
+ return html_table, similaridad_html
91
 
92
  iface = gr.Interface(
93
  fn=analyze_keywords,
94
  inputs="text",
95
+ outputs=["html", "html"],
96
  title="Comparador de Keywords en Google",
97
+ description="Introduce las keywords separadas por comas para encontrar coincidencias en los resultados de búsqueda de Google. Las URLs comunes entre las búsquedas se resaltarán en colores únicos y se mostrará el porcentaje de similaridad."
98
  )
99
 
100
+ iface.launch()