resolves #7
This commit is contained in:
parent
5ef9c960df
commit
447bcbc3fa
51
endpoints/lastfm.py
Normal file
51
endpoints/lastfm.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python3.12
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class ValidArtistSearchRequest(BaseModel):
|
||||||
|
"""
|
||||||
|
- **a**: artist name
|
||||||
|
"""
|
||||||
|
|
||||||
|
a: str
|
||||||
|
|
||||||
|
class LastFM(FastAPI):
|
||||||
|
"""Last.FM Endpoints"""
|
||||||
|
def __init__(self, app: FastAPI, util, constants): # pylint: disable=super-init-not-called
|
||||||
|
self.app = app
|
||||||
|
self.util = util
|
||||||
|
self.constants = constants
|
||||||
|
self.lastfm = importlib.import_module("lastfm_wrapper").LastFM()
|
||||||
|
|
||||||
|
self.endpoints = {
|
||||||
|
"get_artist_by_name": self.artist_by_name_handler,
|
||||||
|
#tbd
|
||||||
|
}
|
||||||
|
|
||||||
|
for endpoint, handler in self.endpoints.items():
|
||||||
|
app.add_api_route(f"/{endpoint}/", handler, methods=["POST"])
|
||||||
|
|
||||||
|
async def artist_by_name_handler(self, data: ValidArtistSearchRequest):
|
||||||
|
"""
|
||||||
|
Get artist info
|
||||||
|
"""
|
||||||
|
artist = data.a.strip()
|
||||||
|
if not artist:
|
||||||
|
return {
|
||||||
|
'err': True,
|
||||||
|
'errorText': 'No artist specified'
|
||||||
|
}
|
||||||
|
|
||||||
|
artist_result = await self.lastfm.search_artist(artist=artist)
|
||||||
|
if not artist_result or "err" in artist_result.keys():
|
||||||
|
return {
|
||||||
|
'err': True,
|
||||||
|
'errorText': 'Search failed (no results?)'
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
'success': True,
|
||||||
|
'result': artist_result
|
||||||
|
}
|
2
main.py
2
main.py
@ -54,6 +54,8 @@ Actionable Routes
|
|||||||
randmsg_endpoint = importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants)
|
randmsg_endpoint = importlib.import_module("endpoints.rand_msg").RandMsg(app, util, constants)
|
||||||
# Below also provides: /lyric_cache_list/ (in addition to /lyric_search/)
|
# Below also provides: /lyric_cache_list/ (in addition to /lyric_search/)
|
||||||
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
|
||||||
|
lastfm_endpoints = importlib.import_module("endpoints.lastfm").LastFM(app, util, constants)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user