From 9083f05c94f5e2e8a6a423fefb3ffaec7eccc484 Mon Sep 17 00:00:00 2001 From: Dantenerosas Date: Tue, 26 Sep 2023 01:07:44 +0300 Subject: [PATCH] minor fixes --- src/web/main.py | 12 +++++++----- src/web/schemes/submit.py | 5 ++++- src/web/templates/index.html | 11 +++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/web/main.py b/src/web/main.py index efb5288..b9223bd 100644 --- a/src/web/main.py +++ b/src/web/main.py @@ -11,7 +11,7 @@ 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 +from src.web.schemes.submit import SubmitIn, CheckIn app = FastAPI( title="video_downloader", openapi_url=f"/api/v1/openapi.json" @@ -59,6 +59,7 @@ queue_name -> { } ''' + async def is_task_already_done_or_exist(redis: RedisClient, link: str): messages = await redis.get_task_done_queue() temp = [json.loads(msg) for msg in messages] @@ -91,7 +92,7 @@ async def index(request: Request): return templates.TemplateResponse("index.html", {"request": request}) -@app.post('/submit/') +@app.post('/submit') 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) @@ -148,19 +149,20 @@ async def download_video(file_path): @app.post('/check/', response_class=FileResponse, status_code=200) -async def download_video(request: Request, link: str): +async def download_video(data: CheckIn, request: Request): try: red = RedisClient() + messages_task_done = await red.get_task_done_queue() messages_tasks = await red.get_tasks() tasks_done = [ literal_eval(message.decode('utf-8')) for message in messages_task_done - if literal_eval(message.decode('utf-8'))["link"] == link + if literal_eval(message.decode('utf-8'))["link"] == data.link ] tasks = [ literal_eval(message.decode('utf-8')) for message in messages_tasks - if literal_eval(message.decode('utf-8'))["link"] == link + if literal_eval(message.decode('utf-8'))["link"] == data.link ] error_tasks = [ diff --git a/src/web/schemes/submit.py b/src/web/schemes/submit.py index 4913165..832e987 100644 --- a/src/web/schemes/submit.py +++ b/src/web/schemes/submit.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from enum import Enum from fastapi import Form - +from pydantic import BaseModel ''' vext: Video Extension (mp4 > mov > webm > flv > other). If --prefer-free-formats is used, webm is preferred. @@ -44,3 +44,6 @@ class SubmitIn: audio_format: AudioFormatEnum = Form(default=AudioFormatEnum.format_m4a) merge_output_format: MergeOutputFormatEnum = Form(default=MergeOutputFormatEnum.format_mp4) + +class CheckIn(BaseModel): + link: str diff --git a/src/web/templates/index.html b/src/web/templates/index.html index c97aa9d..57142ee 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -93,8 +93,10 @@ const link = document.getElementById("link").value const xhr2 = new XMLHttpRequest(); // TODO: скорректировать ссылку, она должна быть относительной - xhr2.open('POST', 'http://'+document.location.host+'/check'); + xhr2.open('POST', 'http://'+document.location.host+'/check/'); xhr2.responseType = 'json'; + xhr2.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + xhr2.onload = function() { // TODO: добавить обработку исключений и всех возможных кодов в ответе if (xhr2.status !== 200) { @@ -112,9 +114,10 @@ document.forms.download.querySelector('.submit-spinner').classList.add('submit-spinner_hide'); }; }; - body = {}; - body.link = link; - xhr2.send(body) + var params = { + "link":link, + }; + setTimeout(function() { xhr2.send(JSON.stringify(params));}, 1000) }