This commit is contained in:
2025-02-15 21:09:33 -05:00
parent 60416c493f
commit 39d1ddaffa
22 changed files with 509 additions and 525 deletions

View File

@ -2,28 +2,30 @@
import importlib
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
from fastapi.responses import JSONResponse
from typing import Optional, Union
from .constructors import ValidYTSearchRequest
class YT(FastAPI):
"""YT Endpoints"""
def __init__(self, app: FastAPI, util, constants) -> None: # pylint: disable=super-init-not-called
self.app = app
"""
YT Endpoints
"""
def __init__(self, app: FastAPI, util,
constants) -> None: # pylint: disable=super-init-not-called
self.app: FastAPI = app
self.util = util
self.constants = constants
self.ytsearch = importlib.import_module("youtube_search_async").YoutubeSearch()
self.endpoints: dict = {
"yt/search": self.yt_video_search_handler,
#tbd
}
for endpoint, handler in self.endpoints.items():
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
include_in_schema=True)
async def yt_video_search_handler(self, data: ValidYTSearchRequest) -> dict:
async def yt_video_search_handler(self, data: ValidYTSearchRequest) -> JSONResponse:
"""
Search for YT Video by Title (closest match returned)
- **t**: Title to search
@ -32,13 +34,13 @@ class YT(FastAPI):
title: str = data.t
yts_res: Optional[list[dict]] = await self.ytsearch.search(title)
if not yts_res:
return {
return JSONResponse(status_code=404, content={
'err': True,
'errorText': 'No result.',
}
yt_video_id: str|bool = yts_res[0].get('id', False)
})
yt_video_id: Union[str, bool] = yts_res[0].get('id', False)
return {
return JSONResponse(content={
'video_id': yt_video_id,
'extras': yts_res[0]
}
'extras': yts_res[0],
})