File size: 1,245 Bytes
68fb5e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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