03-sep: [comando] /descargar ver progreso de descargas + listar_descargas en nas_dl

This commit is contained in:
juanjo
2026-09-03 23:38:23 +02:00
parent 9e067f4e4d
commit 07f4f89a22
2 changed files with 109 additions and 36 deletions

View File

@@ -139,6 +139,70 @@ def add_torrent(uri, session=None):
return False
def human_size(n):
"""Formatea bytes a una unidad legible."""
n = float(n or 0)
for u in ("B", "KB", "MB", "GB", "TB"):
if n < 1024:
return f"{n:.1f}{u}"
n /= 1024
return f"{n:.1f}PB"
def listar_descargas(hablar_solo_activas=True, session=None):
"""Devuelve (lineas_texto, activas). Lista las tareas de Download Station
con estado, porcentaje de progreso, tamaño descargado y velocidad.
`progress` se calcula con downloaded_pieces/total_pieces (fiable en NAS)."""
if not session:
session = login()
if not session:
return ["❌ No se pudo autenticar con Download Station."], []
try:
r = _api("/webapi/DownloadStation/task.cgi", {
"api": "SYNO.DownloadStation.Task",
"version": "3",
"method": "list",
"additional": "detail,transfer",
}, session=session)
if not r.get("success"):
return [f"❌ Error al listar: {r.get('error')}"], []
except Exception as e:
return [f"❌ Error al consultar el NAS: {e}"], []
tareas = r.get("data", {}).get("tasks", []) or []
lineas, activas, resto = [], [], []
for t in tareas:
titulo = t.get("title") or ""
estado = t.get("status") or "?"
detail = (t.get("additional") or {}).get("detail") or {}
transfer = (t.get("additional") or {}).get("transfer") or {}
total_pieces = detail.get("total_pieces") or 0
done_pieces = transfer.get("downloaded_pieces") or 0
pct = (done_pieces * 100.0 / total_pieces) if total_pieces else 0
size = transfer.get("size_downloaded") or 0
speed = transfer.get("speed_download") or 0
if estado == "downloading":
activas.append(f"▶️ <b>{pct:.1f}%</b> {human_size(size)} ⬇️{speed // 1024} KB/s\n"
f" {titulo}")
else:
resto.append((estado, pct, size, titulo))
if activas:
lineas.append("📥 <b>DESCARGANDO</b>")
lineas.extend(activas)
lineas.append("")
if not hablar_solo_activas:
lineas.append("📁 Otras tareas:")
for estado, pct, size, titulo in resto:
icono = {"finished": "✅", "error": "❌", "paused": "⏸️",
"seeding": "🌱"}.get(estado, "•")
if estado == "finished":
lineas.append(f"{icono} {titulo}")
else:
lineas.append(f"{icono} {estado} ({pct:.0f}%) — {titulo}")
if not activas:
lineas.append("No hay torrents descargándose ahora mismo.")
return lineas, activas
if __name__ == "__main__":
import sys
if len(sys.argv) < 2: