This commit is contained in:
2025-02-15 21:09:33 -05:00
parent 60416c493f
commit 39d1ddaffa
22 changed files with 509 additions and 525 deletions

View File

@ -3,15 +3,17 @@
import importlib
import traceback
from typing import Optional
from typing import Optional, Union
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from .constructors import ValidArtistSearchRequest, ValidAlbumDetailRequest,\
ValidTrackInfoRequest, LastFMException
class LastFM(FastAPI):
"""Last.FM Endpoints"""
def __init__(self, app: FastAPI, util, constants) -> None: # pylint: disable=super-init-not-called
self.app = app
def __init__(self, app: FastAPI,
util, constants) -> None: # pylint: disable=super-init-not-called
self.app: FastAPI = app
self.util = util
self.constants = constants
self.lastfm = importlib.import_module("lastfm_wrapper").LastFM()
@ -29,43 +31,43 @@ class LastFM(FastAPI):
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
include_in_schema=True)
async def artist_by_name_handler(self, data: ValidArtistSearchRequest):
async def artist_by_name_handler(self, data: ValidArtistSearchRequest) -> JSONResponse:
"""
Get artist info
- **a**: Artist to search
"""
artist: Optional[str] = data.a.strip()
if not artist:
return {
return JSONResponse(content={
'err': True,
'errorText': 'No artist specified'
}
'errorText': 'No artist specified',
})
artist_result = await self.lastfm.search_artist(artist=artist)
if not artist_result or "err" in artist_result.keys():
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'Search failed (no results?)'
}
'errorText': 'Search failed (no results?)',
})
return {
return JSONResponse(content={
'success': True,
'result': artist_result
}
'result': artist_result,
})
async def artist_album_handler(self, data: ValidArtistSearchRequest) -> dict:
async def artist_album_handler(self, data: ValidArtistSearchRequest) -> JSONResponse:
"""
Get artist's albums/releases
- **a**: Artist to search
"""
artist: str = data.a.strip()
if not artist:
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'No artist specified'
}
'errorText': 'Invalid request: No artist specified',
})
album_result: dict|list[dict] = await self.lastfm.get_artist_albums(artist=artist)
album_result: Union[dict, list[dict]] = await self.lastfm.get_artist_albums(artist=artist)
album_result_out: list = []
seen_release_titles: list = []
@ -76,12 +78,12 @@ class LastFM(FastAPI):
seen_release_titles.append(release_title.lower())
album_result_out.append(release)
return {
return JSONResponse(content={
'success': True,
'result': album_result_out
}
})
async def release_detail_handler(self, data: ValidAlbumDetailRequest) -> dict:
async def release_detail_handler(self, data: ValidAlbumDetailRequest) -> JSONResponse:
"""
Get details of a particular release by an artist
- **a**: Artist to search
@ -91,10 +93,10 @@ class LastFM(FastAPI):
release: str = data.release.strip()
if not artist or not release:
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'Invalid request'
}
'errorText': 'Invalid request',
})
release_result = await self.lastfm.get_release(artist=artist, album=release)
ret_obj = {
@ -102,15 +104,15 @@ class LastFM(FastAPI):
'artists': release_result.get('artists'),
'title': release_result.get('title'),
'summary': release_result.get('summary'),
'tracks': release_result.get('tracks')
'tracks': release_result.get('tracks'),
}
return {
return JSONResponse(content={
'success': True,
'result': ret_obj
}
'result': ret_obj,
})
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest) -> dict:
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest) -> JSONResponse:
"""
Get track list for a particular release by an artist
- **a**: Artist to search
@ -120,22 +122,22 @@ class LastFM(FastAPI):
release: str = data.release.strip()
if not artist or not release:
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'Invalid request'
}
'errorText': 'Invalid request',
})
tracklist_result: dict = await self.lastfm.get_album_tracklist(artist=artist, album=release)
return {
return JSONResponse(content={
'success': True,
'id': tracklist_result.get('id'),
'artists': tracklist_result.get('artists'),
'title': tracklist_result.get('title'),
'summary': tracklist_result.get('summary'),
'tracks': tracklist_result.get('tracks')
}
'tracks': tracklist_result.get('tracks'),
})
async def track_info_handler(self, data: ValidTrackInfoRequest) -> dict:
async def track_info_handler(self, data: ValidTrackInfoRequest) -> JSONResponse:
"""
Get track info from Last.FM given an artist/track
- **a**: Artist to search
@ -146,22 +148,23 @@ class LastFM(FastAPI):
track: str = data.t
if not artist or not track:
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'Invalid request'
}
})
track_info_result: dict = await self.lastfm.get_track_info(artist=artist, track=track)
track_info_result: dict = await self.lastfm.get_track_info(artist=artist,
track=track)
if "err" in track_info_result:
raise LastFMException("Unknown error occurred: %s",
track_info_result.get('errorText', '??'))
return {
return JSONResponse(content={
'success': True,
'result': track_info_result
}
})
except:
traceback.print_exc()
return {
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'General error',
}
})