minor fixes, added result processing
This commit is contained in:
@ -5,12 +5,13 @@ from ast import literal_eval
|
||||
|
||||
import uvicorn
|
||||
from aio_pika import connect, Message, DeliveryMode
|
||||
from fastapi import FastAPI, Request, Form, HTTPException
|
||||
from fastapi import FastAPI, Request, Depends
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
from starlette.responses import JSONResponse, FileResponse, StreamingResponse
|
||||
from starlette.templating import Jinja2Templates
|
||||
|
||||
from src.core.redis_client import RedisClient
|
||||
from src.web.schemes.submit import SubmitIn
|
||||
|
||||
app = FastAPI(
|
||||
title="video_downloader", openapi_url=f"/api/v1/openapi.json"
|
||||
@ -27,25 +28,43 @@ app.add_middleware(
|
||||
)
|
||||
|
||||
|
||||
async def is_task_already_done_or_exist(redis: RedisClient, link: str):
|
||||
messages = await redis.get_task_done_queue()
|
||||
|
||||
tasks = [
|
||||
literal_eval(message.decode('utf-8')) for message in messages
|
||||
if literal_eval(message.decode('utf-8'))["link"] == link
|
||||
and literal_eval(message.decode('utf-8'))["status"] in ["done", "exist"]
|
||||
]
|
||||
|
||||
if len(tasks) > 0:
|
||||
task = tasks[0]
|
||||
await redis.del_task_from_task_done_queue(task)
|
||||
return task
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def index(request: Request):
|
||||
return templates.TemplateResponse("index.html", {"request": request})
|
||||
|
||||
|
||||
@app.post('/submit/')
|
||||
async def get_url_for_download_video(request: Request, link: str = Form(...)):
|
||||
connection = await connect("amqp://guest:guest@localhost/")
|
||||
async def get_url_for_download_video(request: Request, data: SubmitIn = Depends()):
|
||||
red = RedisClient()
|
||||
task_done = await is_task_already_done_or_exist(red, data.link)
|
||||
if task_done:
|
||||
link_to_download_video = str(request.base_url) + "get/?file_path=" + task_done["result"]
|
||||
return JSONResponse({"result": link_to_download_video})
|
||||
|
||||
async with connection:
|
||||
async with await connect("amqp://guest:guest@localhost/") as connection:
|
||||
# Creating a channel
|
||||
channel = await connection.channel()
|
||||
body = [
|
||||
|
||||
{
|
||||
"link": link,
|
||||
"parser": "base",
|
||||
"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
|
||||
"merge_output_format": "mp4",
|
||||
"link": data.link,
|
||||
"format": f"bestvideo[ext={data.format.value}]+bestaudio[ext={data.format.value}]/best[ext={data.format.value}]/best",
|
||||
"merge_output_format": data.merge_output_format.value,
|
||||
"outtmpl": f"downloads/%(extractor_key)s/%(id)s_%(width)sp.%(ext)s",
|
||||
}, ]
|
||||
# Sending the message
|
||||
@ -63,13 +82,19 @@ async def get_url_for_download_video(request: Request, link: str = Form(...)):
|
||||
)
|
||||
|
||||
print(f" [x] Sent '{link}'")
|
||||
red = RedisClient()
|
||||
|
||||
while True:
|
||||
try:
|
||||
mes = await red.get_task_done_queue()
|
||||
task = literal_eval(list(mes)[0].decode('utf-8'))
|
||||
if task["link"] == link["link"]:
|
||||
messages = await red.get_task_done_queue()
|
||||
tasks = [
|
||||
literal_eval(message.decode('utf-8')) for message in messages
|
||||
if literal_eval(message.decode('utf-8'))["link"] == link["link"]
|
||||
]
|
||||
error_tasks = [tasks.pop(tasks.index(error_task)) for error_task in tasks if error_task["status"] == "error"]
|
||||
if len(error_tasks) > 0:
|
||||
return JSONResponse({"result": f"STATUS: ERROR {error_tasks[-1]['result']}"})
|
||||
if len(tasks) > 0:
|
||||
task = tasks[0]
|
||||
await red.del_task_from_task_done_queue(task)
|
||||
break
|
||||
await asyncio.sleep(5)
|
||||
|
30
src/web/schemes/submit.py
Normal file
30
src/web/schemes/submit.py
Normal file
@ -0,0 +1,30 @@
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
|
||||
from fastapi import Form
|
||||
|
||||
|
||||
class FormatEnum(Enum):
|
||||
format_3gp = "3gp"
|
||||
format_aac = "aac"
|
||||
format_flv = "flv"
|
||||
format_m4a = "m4a"
|
||||
format_mp3 = "mp3"
|
||||
format_mp4 = "mp4"
|
||||
format_ogg = "ogg"
|
||||
|
||||
|
||||
class MergeOutputFormatEnum(Enum):
|
||||
format_avi = "avi"
|
||||
format_flv = "flv"
|
||||
format_mkv = "mkv"
|
||||
format_mov = "mov"
|
||||
format_mp4 = "mp4"
|
||||
format_webm = "webm"
|
||||
|
||||
@dataclass
|
||||
class SubmitIn:
|
||||
link: str = Form(...)
|
||||
format: FormatEnum = Form(...)
|
||||
merge_output_format: MergeOutputFormatEnum = Form(...)
|
||||
|
@ -71,7 +71,9 @@
|
||||
</style>
|
||||
<body>
|
||||
<form method="post" action="/submit" id="download">
|
||||
<input type="text" name="link">
|
||||
<input type="text" name="link" placeholder="link">
|
||||
<input type="text" name="format" placeholder="format">
|
||||
<input type="text" name="merge_output_format" placeholder="merge_output_format">
|
||||
<button type="submit" class="custom-btn btn-1"><span class="submit-spinner submit-spinner_hide"></span> Download</button>
|
||||
</form>
|
||||
<div class="col">
|
||||
|
Reference in New Issue
Block a user