52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
|
#!/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
|
||
|
}
|