Actualizar app/main.py

Añadido mensaje de descargando mientras descargas lista de reproducción
This commit is contained in:
2025-07-15 21:26:34 +00:00
parent 53d1bdb33a
commit 2edfc6bb1d

View File

@@ -13,11 +13,12 @@ os.makedirs(BASE_DOWNLOADS_DIR, exist_ok=True)
def index(): def index():
if request.method == "POST": if request.method == "POST":
url = request.form.get("url") url = request.form.get("url")
mode = request.form.get("mode") # 'audio' o 'video' mode = request.form.get("mode") # 'audio' o 'video'
download_type = request.form.get("type") # 'single' o 'playlist' download_type = request.form.get("type") # 'single' o 'playlist'
if not url: if not url:
return render_template("index.html", error="URL obligatoria") # En caso de error, siempre renderiza la plantilla para que el JS pueda leer el error.
return render_template("index.html", error="URL obligatoria"), 400
# Modo single o playlist # Modo single o playlist
@@ -41,7 +42,7 @@ def index():
files = os.listdir(folder_path) files = os.listdir(folder_path)
if not files: if not files:
return render_template("index.html", error="No se pudo descargar el archivo.") return render_template("index.html", error="No se pudo descargar el archivo."), 500
downloaded_file_path = os.path.join(folder_path, files[0]) downloaded_file_path = os.path.join(folder_path, files[0])
@@ -56,10 +57,17 @@ def index():
print(f"Error al borrar archivos: {e}") print(f"Error al borrar archivos: {e}")
return response return response
# Si es exitoso, solo envía el archivo. No render_template aquí.
return send_file(downloaded_file_path, as_attachment=True, download_name=files[0]) return send_file(downloaded_file_path, as_attachment=True, download_name=files[0])
except subprocess.CalledProcessError: except subprocess.CalledProcessError as e:
return render_template("index.html", error="Error al descargar el vídeo.") # Captura la salida de error de yt-dlp para un mensaje más específico si es posible
error_message = f"Error al descargar el vídeo: {e}"
if e.stderr:
error_message += f" - {e.stderr.decode().strip()}"
return render_template("index.html", error=error_message), 500
except Exception as e:
return render_template("index.html", error=f"Error inesperado: {e}"), 500
elif download_type == "playlist": elif download_type == "playlist":
folder_id = str(uuid.uuid4()) folder_id = str(uuid.uuid4())
@@ -77,8 +85,13 @@ def index():
) )
playlist_title = result.stdout.strip().splitlines()[0] playlist_title = result.stdout.strip().splitlines()[0]
playlist_title_clean = "".join(c for c in playlist_title if c.isalnum() or c in " _-").strip().replace(" ", "_") playlist_title_clean = "".join(c for c in playlist_title if c.isalnum() or c in " _-").strip().replace(" ", "_")
except subprocess.CalledProcessError: except subprocess.CalledProcessError as e:
return render_template("index.html", error="Error al obtener el título de la playlist.") error_message = f"Error al obtener el título de la playlist: {e}"
if e.stderr:
error_message += f" - {e.stderr.decode().strip()}"
return render_template("index.html", error=error_message), 500
except Exception as e:
return render_template("index.html", error=f"Error inesperado al obtener título de playlist: {e}"), 500
output_path = os.path.join(folder_path, "%(title)s.%(ext)s") output_path = os.path.join(folder_path, "%(title)s.%(ext)s")
cmd = ["yt-dlp", "-o", output_path] cmd = ["yt-dlp", "-o", output_path]
@@ -102,16 +115,21 @@ def index():
if os.path.exists(zip_path): if os.path.exists(zip_path):
os.remove(zip_path) os.remove(zip_path)
except Exception as e: except Exception as e:
print(f"Error al borrar archivos: {e}") print(f"Error al borrar archivos de la playlist: {e}")
return response return response
# Si es exitoso, solo envía el archivo. No render_template aquí.
return send_file(zip_path, as_attachment=True, download_name=zip_filename, mimetype='application/zip') return send_file(zip_path, as_attachment=True, download_name=zip_filename, mimetype='application/zip')
except subprocess.CalledProcessError: except subprocess.CalledProcessError as e:
return render_template("index.html", error="Error al descargar la lista.") error_message = f"Error al descargar la lista: {e}"
if e.stderr:
error_message += f" - {e.stderr.decode().strip()}"
return render_template("index.html", error=error_message), 500
except Exception as e:
return render_template("index.html", error=f"Error inesperado al descargar playlist: {e}"), 500
else: else:
return render_template("index.html", error="Tipo de descarga no válido.") return render_template("index.html", error="Tipo de descarga no válido."), 400
return render_template("index.html") return render_template("index.html")