api/endpoints/yt.py
2025-02-11 11:19:52 -05:00

39 lines
1.2 KiB
Python

#!/usr/bin/env python3.12
import importlib
from fastapi import FastAPI
from pydantic import BaseModel
from .constructors import ValidYTSearchRequest
class YT(FastAPI):
"""YT Endpoints"""
def __init__(self, app: FastAPI, util, constants, glob_state): # 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 = {
"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):
"""
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)
return {
'video_id': yt_video_id,
'extras': yts_res[0]
}