| import pandas as pd |
| from typing import List, Dict, Any |
|
|
| def format_results_for_dataframe(results: List[Dict[str, Any]]) -> pd.DataFrame: |
| if not results: |
| return pd.DataFrame(columns=["Título", "Autores", "Año", "DOI", "Fuente", "PDF URL"]) |
| rows = [] |
| for r in results: |
| autores = r.get("authors", []) |
| if isinstance(autores, list): |
| autores = ", ".join(autores) |
| rows.append({ |
| "Título": r.get("title") or "N/A", |
| "Autores": autores or "N/A", |
| "Año": r.get("year", "N/A"), |
| "DOI": r.get("doi", ""), |
| "Fuente": r.get("source", "N/A"), |
| "PDF URL": r.get("pdfUrl", ""), |
| }) |
| return pd.DataFrame(rows) |
|
|
| def format_error(error: Exception) -> str: |
| name = type(error).__name__ |
| msg = str(error) |
| if "Connect" in name or "connect" in msg: |
| return f"**⚠️ Sin conexión:** {msg}" |
| if "Timeout" in name or "timeout" in msg: |
| return f"**⏱️ Timeout:** {msg}" |
| return f"**❌ Error ({name}):** {msg}" |
|
|
| def truncate_text(text: str, max_length: int = 500) -> str: |
| if not text: |
| return "" |
| text = str(text) |
| return text[:max_length] + "..." if len(text) > max_length else text |
|
|