From 71c860689afb5e6351c2040d462a8f2ec54fcfcb Mon Sep 17 00:00:00 2001 From: garickbadalov Date: Mon, 14 Aug 2023 17:47:15 +0300 Subject: [PATCH] added link --- src/api/api_v1/endpoints/main.py | 29 ++++++++++++++++++++++------- src/core/ydl.py | 23 ++++++++++++++++++----- src/main.py | 2 ++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/api/api_v1/endpoints/main.py b/src/api/api_v1/endpoints/main.py index 16c66d3..7e56268 100644 --- a/src/api/api_v1/endpoints/main.py +++ b/src/api/api_v1/endpoints/main.py @@ -1,6 +1,8 @@ +import os + from fastapi import APIRouter, Form, HTTPException from starlette.requests import Request -from starlette.responses import HTMLResponse +from starlette.responses import HTMLResponse, FileResponse from starlette.templating import Jinja2Templates from src.core.ydl import VideoDownloader @@ -18,17 +20,30 @@ async def index(request: Request): @main_router.post('/submit') -def disable_cat(request: Request, link: str = Form(...)): - downloader = VideoDownloader(link=link, ydl_opts={ +async def get_url_for_download_video(request: Request, link: str = Form(...)): + file_name = VideoDownloader.get_unique_video_filename() + ydl_opts = { "forceurl": True, - "username": "garick.badalov@yandex.ru", - "password": "garik876.", - }) + '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" except SiteNotImplementedException as ex: raise HTTPException( status_code=400, detail=ex.message ) - return templates.TemplateResponse("index.html", {"request": request, "result": result["url"]}) + return templates.TemplateResponse("index.html", {"request": request, "result": file_path}) + + +@main_router.get('/{file_path}', response_class=FileResponse) +async def download_video(request: Request, 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}' diff --git a/src/core/ydl.py b/src/core/ydl.py index e4b4cc4..ad63c19 100644 --- a/src/core/ydl.py +++ b/src/core/ydl.py @@ -1,4 +1,8 @@ from __future__ import unicode_literals + +import os +import uuid +from datetime import datetime from urllib.parse import urlparse import youtube_dl @@ -7,15 +11,25 @@ from src.exceptions.download_exceptions import SiteNotImplementedException class VideoDownloader: + SUPPORTING_WEBSITES = [ + "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") + BASE_YOUTUBE_DIR = os.path.join(BASE_DOWNLOAD_DIR, "Youtube") + def __init__(self, link: str, ydl_opts: dict = None, username: str = None, password: str = None): self.link = link self.ydl_opts = ydl_opts self.username = username self.password = password - self.SUPPORTING_WEBSITES = [ - "ok.ru", "vk.com", "www.youtube.com" - ] + @staticmethod + def get_unique_video_filename(): + prefix = f'vid_{datetime.now().strftime("%Y%m%d%H%M%S")}' + random_uuid4 = uuid.uuid4().hex[:8] + filename = f"{prefix}_{random_uuid4}" + return filename def download(self): domain = urlparse(self.link).netloc @@ -25,5 +39,4 @@ class VideoDownloader: with youtube_dl.YoutubeDL(self.ydl_opts if self.ydl_opts else {}) as ydl: ydl.download([self.link]) result = ydl.extract_info(self.link, download=False) - url = result['formats'][-1] - return url + return result diff --git a/src/main.py b/src/main.py index bb4e750..b10a797 100644 --- a/src/main.py +++ b/src/main.py @@ -13,5 +13,7 @@ app.include_router( main_router, ) +app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads") + if __name__ == '__main__': uvicorn.run("src.main:app", host="localhost", log_level="info")