initial commit
This commit is contained in:
34
src/api/api_v1/endpoints/main.py
Normal file
34
src/api/api_v1/endpoints/main.py
Normal file
@ -0,0 +1,34 @@
|
||||
from fastapi import APIRouter, Form, HTTPException
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import HTMLResponse
|
||||
from starlette.templating import Jinja2Templates
|
||||
|
||||
from src.core.ydl import VideoDownloader
|
||||
from src.exceptions.download_exceptions import SiteNotImplementedException
|
||||
|
||||
main_router = APIRouter()
|
||||
|
||||
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
|
||||
@main_router.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request):
|
||||
return templates.TemplateResponse("index.html", {"request": request})
|
||||
|
||||
|
||||
@main_router.post('/submit')
|
||||
def disable_cat(request: Request, link: str = Form(...)):
|
||||
downloader = VideoDownloader(link=link, ydl_opts={
|
||||
"forceurl": True,
|
||||
"username": "garick.badalov@yandex.ru",
|
||||
"password": "garik876.",
|
||||
})
|
||||
try:
|
||||
result = downloader.download()
|
||||
except SiteNotImplementedException as ex:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=ex.message
|
||||
)
|
||||
return templates.TemplateResponse("index.html", {"request": request, "result": result["url"]})
|
5
src/api/deps.py
Normal file
5
src/api/deps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from src.core.ydl import VideoDownloader
|
||||
|
||||
|
||||
def get_downloader(link: str, opts: dict):
|
||||
return VideoDownloader(link=link, ydl_opts=opts)
|
29
src/core/ydl.py
Normal file
29
src/core/ydl.py
Normal file
@ -0,0 +1,29 @@
|
||||
from __future__ import unicode_literals
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import youtube_dl
|
||||
|
||||
from src.exceptions.download_exceptions import SiteNotImplementedException
|
||||
|
||||
|
||||
class VideoDownloader:
|
||||
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"
|
||||
]
|
||||
|
||||
def download(self):
|
||||
domain = urlparse(self.link).netloc
|
||||
if domain not in self.SUPPORTING_WEBSITES:
|
||||
raise SiteNotImplementedException
|
||||
|
||||
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
|
7
src/exceptions/base_exception.py
Normal file
7
src/exceptions/base_exception.py
Normal file
@ -0,0 +1,7 @@
|
||||
class DefaultException(Exception):
|
||||
default_message = "Что то пошло не так"
|
||||
error_code = "ErrorCodeNotDefined"
|
||||
|
||||
def __init__(self, message=None):
|
||||
self.message = message if message else self.default_message
|
||||
self.error_code = self.error_code
|
6
src/exceptions/download_exceptions.py
Normal file
6
src/exceptions/download_exceptions.py
Normal file
@ -0,0 +1,6 @@
|
||||
from src.exceptions.base_exception import DefaultException
|
||||
|
||||
|
||||
class SiteNotImplementedException(DefaultException):
|
||||
default_message = "This site is not supported"
|
||||
error_code = "SiteNotImplemented"
|
17
src/main.py
Normal file
17
src/main.py
Normal file
@ -0,0 +1,17 @@
|
||||
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
|
||||
|
||||
app = FastAPI(
|
||||
title="PythonProject", openapi_url=f"/api/v1/openapi.json"
|
||||
)
|
||||
|
||||
app.include_router(
|
||||
main_router,
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run("src.main:app", host="localhost", log_level="info")
|
63
src/templates/index.html
Normal file
63
src/templates/index.html
Normal file
@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Video Downloading</title>
|
||||
</head>
|
||||
<style>
|
||||
|
||||
.custom-btn {
|
||||
width: 130px;
|
||||
height: 40px;
|
||||
color: #fff;
|
||||
border-radius: 5px;
|
||||
padding: 10px 25px;
|
||||
font-family: 'Lato', sans-serif;
|
||||
font-weight: 500;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
box-shadow:inset 2px 2px 2px 0px rgba(255,255,255,.5),
|
||||
7px 7px 20px 0px rgba(0,0,0,.1),
|
||||
4px 4px 5px 0px rgba(0,0,0,.1);
|
||||
outline: none;
|
||||
}
|
||||
.btn-1 {
|
||||
background: rgb(6,14,131);
|
||||
background: linear-gradient(0deg, rgba(6,14,131,1) 0%, rgba(12,25,180,1) 100%);
|
||||
border: none;
|
||||
}
|
||||
.btn-1:hover {
|
||||
background: rgb(0,3,255);
|
||||
background: linear-gradient(0deg, rgba(0,3,255,1) 0%, rgba(2,126,251,1) 100%);
|
||||
}
|
||||
input {
|
||||
width: 300px;
|
||||
font-size: 13px;
|
||||
padding: 6px 0 4px 10px;
|
||||
border: 1px solid #cecece;
|
||||
background: #F6F6f6;
|
||||
border-radius: 8px;
|
||||
}
|
||||
.col {
|
||||
background: #f0f0f0; /* Цвет фона */
|
||||
width: 1000px; /* Ширина блока */
|
||||
padding: 10px; /* Поля */
|
||||
font-size: 1.5em; /* Размер шрифта */
|
||||
word-wrap: break-word; /* Перенос слов */
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
<form method="post" action="/submit">
|
||||
<input type="text" name="link">
|
||||
<button type="submit" class="custom-btn btn-1">Download</button>
|
||||
</form>
|
||||
<div class="col">
|
||||
<p> Ссылка для скачивания:</p>
|
||||
<br>
|
||||
<a href={{result}}>{{result}}</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user