test dowloand

This commit is contained in:
2026-02-18 14:07:21 +05:00
parent 11780384e0
commit 74b32e869f
8 changed files with 202 additions and 11 deletions

34
api/route/installing.py Normal file
View File

@@ -0,0 +1,34 @@
# Импортируем необходимые модули
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
import yt_dlp
import tempfile
from fastapi.responses import FileResponse
import os
router = APIRouter(prefix="/yt-dlp", tags=["yt-dlp"])
class DownloadRequest(BaseModel):
url: str
format: str = "best"
@router.post("/download")
async def download_video(data: DownloadRequest):
"""
Скачать видео по ссылке через yt-dlp и вернуть файл пользователю.
"""
save_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data'))
os.makedirs(save_dir, exist_ok=True)
ydl_opts = {
'outtmpl': os.path.join(save_dir, '%(title)s.%(ext)s'),
'format': data.format,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(data.url, download=True)
filename = ydl.prepare_filename(info)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Ошибка загрузки: {e}")
if not os.path.exists(filename):
raise HTTPException(status_code=404, detail="Файл не найден после загрузки")
return FileResponse(filename, filename=os.path.basename(filename), media_type='application/octet-stream')