From 38e29ecfa047d68dc612d3d0a43d8f9d18e9a874 Mon Sep 17 00:00:00 2001 From: nikili0n Date: Tue, 15 Aug 2023 01:38:45 +0300 Subject: [PATCH] added loader, added video format check, fix video download --- src/api/api_v1/endpoints/main.py | 16 ++++---- src/core/ydl.py | 2 +- src/main.py | 3 +- src/templates/index.html | 64 +++++++++++++++++++++++++++++--- 4 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/api/api_v1/endpoints/main.py b/src/api/api_v1/endpoints/main.py index 7e56268..593b7fe 100644 --- a/src/api/api_v1/endpoints/main.py +++ b/src/api/api_v1/endpoints/main.py @@ -2,6 +2,7 @@ import os from fastapi import APIRouter, Form, HTTPException from starlette.requests import Request +from fastapi.responses import JSONResponse from starlette.responses import HTMLResponse, FileResponse from starlette.templating import Jinja2Templates @@ -25,25 +26,22 @@ async def get_url_for_download_video(request: Request, link: str = Form(...)): ydl_opts = { "forceurl": True, 'outtmpl': f'downloads/%(extractor_key)s/{file_name}.%(ext)s' - # "username": "garick.badalov@yandex.ru", - # "password": "garik876.", } - downloader = VideoDownloader(link=link, ydl_opts=ydl_opts) try: result = downloader.download() - file_path = str(request.base_url) + f"{file_name}.mp4" + link_to_download_video = str(request.base_url) + f"{file_name}.{result['formats'][-1]['ext']}" except SiteNotImplementedException as ex: raise HTTPException( status_code=400, detail=ex.message ) - return templates.TemplateResponse("index.html", {"request": request, "result": file_path}) + return JSONResponse({"result": link_to_download_video}) @main_router.get('/{file_path}', response_class=FileResponse) -async def download_video(request: Request, file_path): +async def download_video(file_path): base = os.path.dirname(os.path.dirname(os.path.abspath(file_path))) - BASE_DOWNLOAD_DIR = os.path.join(base, "src", "downloads") - BASE_YOUTUBE_DIR = os.path.join(BASE_DOWNLOAD_DIR, "Youtube") - return BASE_YOUTUBE_DIR + f'/{file_path}' + base_download_dir = os.path.join(base, "src", "downloads") + youtube_dir = os.path.join(base_download_dir, "Youtube") + return FileResponse(youtube_dir + f'/{file_path}', media_type="multipart/form-data") diff --git a/src/core/ydl.py b/src/core/ydl.py index ad63c19..776c402 100644 --- a/src/core/ydl.py +++ b/src/core/ydl.py @@ -12,7 +12,7 @@ from src.exceptions.download_exceptions import SiteNotImplementedException class VideoDownloader: SUPPORTING_WEBSITES = [ - "ok.ru", "vk.com", "www.youtube.com" + "ok.ru", "vk.com", "www.youtube.com", ] BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DOWNLOAD_DIR = os.path.join(BASE_DIR, "downloads") diff --git a/src/main.py b/src/main.py index b10a797..92260fd 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,8 @@ import uvicorn from fastapi import FastAPI from fastapi.staticfiles import StaticFiles -from starlette.templating import Jinja2Templates -from src.api.api_v1.endpoints.main import main_router +from api.api_v1.endpoints.main import main_router app = FastAPI( title="PythonProject", openapi_url=f"/api/v1/openapi.json" diff --git a/src/templates/index.html b/src/templates/index.html index 0fe347a..7984935 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -5,10 +5,9 @@ Video Downloading -
+ - +

Ссылка для скачивания:


- {{result}} + +
+
+
+ \ No newline at end of file