| import gradio as gr |
| import requests |
| import pandas as pd |
| from io import BytesIO |
|
|
| |
| def obtener_datos_y_transformar(dominio, idioma="es", pais="es"): |
| url = f"https://v7.authoritas.com/api/v3/visibility-explorer/ranking/keywords/{idioma}_{pais}?domains[]={dominio}&pageSize=5000" |
| headers = { |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36", |
| "Accept": "application/json", |
| "Connection": "keep-alive" |
| } |
|
|
| try: |
| response = requests.get(url, headers=headers) |
| if response.status_code == 200: |
| json_data = response.json() |
|
|
| |
| column_mapping = { |
| "Palabra_clave": "keyword", |
| "B煤squedas_mensuales": "searchVolume", |
| "CPC": "cpc", |
| "URL": "fullUrl", |
| "Visibilidad_1": "potentialVisibilityScoreInc", |
| "Visibilidad_2": "visibilityIndex", |
| "Visibilidad_3": "visibilityIndexShare" |
| } |
|
|
| |
| items = json_data.get("items", []) |
| if not items: |
| return None, "No se encontraron datos para el dominio proporcionado." |
|
|
| |
| data = [] |
| for item in items: |
| row = {key: item.get(value, 0) for key, value in column_mapping.items()} |
| data.append(row) |
|
|
| df = pd.DataFrame(data, columns=column_mapping.keys()) |
|
|
| |
| df["Palabra_clave"] = df["Palabra_clave"].apply( |
| lambda x: f"{x} (隆Clave larga!)" if isinstance(x, str) and len(x.split()) > 4 else x |
| ) |
|
|
| |
| output = BytesIO() |
| with pd.ExcelWriter(output, engine="xlsxwriter") as writer: |
| df.to_excel(writer, index=False, sheet_name="Datos") |
| sheet = writer.sheets["Datos"] |
| sheet.set_column(0, len(column_mapping) - 1, 20) |
|
|
| output.seek(0) |
| return output, "Archivo generado exitosamente. Desc谩rgalo abajo." |
| else: |
| return None, f"Error al consultar la API: HTTP {response.status_code}" |
| except requests.RequestException as e: |
| return None, f"Error en la conexi贸n: {str(e)}" |
|
|
| |
| def interfaz(): |
| with gr.Blocks() as app: |
| gr.Markdown("## Consulta de Keywords y Exportaci贸n a Excel") |
| dominio_input = gr.Textbox(label="Dominio (ejemplo.com)", placeholder="psicologiaymente.com") |
| idioma_input = gr.Textbox(label="Idioma (ej: es)", value="es") |
| pais_input = gr.Textbox(label="Pa铆s (ej: es)", value="es") |
| archivo_salida = gr.File(label="Archivo Excel", file_types=[".xlsx"]) |
| mensaje_salida = gr.Textbox(label="Estado", interactive=False) |
|
|
| btn = gr.Button("Obtener Datos y Transformar") |
| btn.click( |
| fn=obtener_datos_y_transformar, |
| inputs=[dominio_input, idioma_input, pais_input], |
| outputs=[archivo_salida, mensaje_salida] |
| ) |
| return app |
|
|
| |
| if __name__ == "__main__": |
| interfaz().launch() |
|
|