significant refactor/cleanup
This commit is contained in:
@ -3,18 +3,19 @@
|
||||
import importlib
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from .constructors import ValidYTSearchRequest
|
||||
|
||||
class YT(FastAPI):
|
||||
"""YT Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
|
||||
def __init__(self, app: FastAPI, util, constants, glob_state) -> None: # pylint: disable=super-init-not-called
|
||||
self.app = app
|
||||
self.util = util
|
||||
self.constants = constants
|
||||
self.glob_state = glob_state
|
||||
self.ytsearch = importlib.import_module("youtube_search_async").YoutubeSearch()
|
||||
|
||||
self.endpoints = {
|
||||
self.endpoints: dict = {
|
||||
"yt/search": self.yt_video_search_handler,
|
||||
#tbd
|
||||
}
|
||||
@ -23,15 +24,20 @@ class YT(FastAPI):
|
||||
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
|
||||
include_in_schema=True)
|
||||
|
||||
async def yt_video_search_handler(self, data: ValidYTSearchRequest):
|
||||
async def yt_video_search_handler(self, data: ValidYTSearchRequest) -> dict:
|
||||
"""
|
||||
Search for YT Video by Title (closest match returned)
|
||||
- **t**: Title to search
|
||||
"""
|
||||
|
||||
title = data.t
|
||||
yts_res = await self.ytsearch.search(title)
|
||||
yt_video_id = yts_res[0].get('id', False)
|
||||
title: str = data.t
|
||||
yts_res: Optional[list[dict]] = await self.ytsearch.search(title)
|
||||
if not yts_res:
|
||||
return {
|
||||
'err': True,
|
||||
'errorText': 'No result.',
|
||||
}
|
||||
yt_video_id: str|bool = yts_res[0].get('id', False)
|
||||
|
||||
return {
|
||||
'video_id': yt_video_id,
|
||||
|
Reference in New Issue
Block a user