resolves #16
This commit is contained in:
parent
a603366427
commit
a4155142fc
46
endpoints/yt.py
Normal file
46
endpoints/yt.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3.12
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class ValidYTSearchRequest(BaseModel):
|
||||||
|
"""
|
||||||
|
- **t**: title to search
|
||||||
|
"""
|
||||||
|
|
||||||
|
t: str = "rick astley - never gonna give you up"
|
||||||
|
|
||||||
|
|
||||||
|
class YT(FastAPI):
|
||||||
|
"""YT Endpoints"""
|
||||||
|
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||||
|
self.app = app
|
||||||
|
self.util = util
|
||||||
|
self.constants = constants
|
||||||
|
self.ytsearch = importlib.import_module("youtube_search_async").YoutubeSearch()
|
||||||
|
|
||||||
|
self.endpoints = {
|
||||||
|
"yt_video_search": self.yt_video_search_handler,
|
||||||
|
#tbd
|
||||||
|
}
|
||||||
|
|
||||||
|
for endpoint, handler in self.endpoints.items():
|
||||||
|
app.add_api_route(f"/{endpoint}/", handler, methods=["POST"])
|
||||||
|
|
||||||
|
async def yt_video_search_handler(self, data: ValidYTSearchRequest):
|
||||||
|
"""
|
||||||
|
/yt_video_search/
|
||||||
|
Search for YT Video by Title (closest match returned)
|
||||||
|
"""
|
||||||
|
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
2
main.py
2
main.py
@ -56,6 +56,8 @@ randmsg_endpoint = importlib.import_module("endpoints.rand_msg").RandMsg(app, ut
|
|||||||
lyric_search_endpoint = importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants)
|
lyric_search_endpoint = importlib.import_module("endpoints.lyric_search").LyricSearch(app, util, constants)
|
||||||
# Below provides numerous LastFM-fed endpoints
|
# Below provides numerous LastFM-fed endpoints
|
||||||
lastfm_endpoints = importlib.import_module("endpoints.lastfm").LastFM(app, util, constants)
|
lastfm_endpoints = importlib.import_module("endpoints.lastfm").LastFM(app, util, constants)
|
||||||
|
# Below: YT endpoint(s)
|
||||||
|
yt_endpoints = importlib.import_module("endpoints.yt").YT(app, util, constants)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user