api/endpoints/lastfm.py

176 lines
6.4 KiB
Python
Raw Normal View History

2024-08-11 17:28:43 -04:00
#!/usr/bin/env python3.12
2025-01-15 20:17:49 -05:00
# pylint: disable=bare-except
2024-08-11 17:28:43 -04:00
import importlib
2025-01-15 20:17:49 -05:00
import traceback
2025-02-15 21:09:33 -05:00
from typing import Optional, Union
2024-08-11 17:28:43 -04:00
from fastapi import FastAPI
2025-02-15 21:09:33 -05:00
from fastapi.responses import JSONResponse
2025-02-11 11:19:52 -05:00
from .constructors import ValidArtistSearchRequest, ValidAlbumDetailRequest,\
2025-02-11 20:01:07 -05:00
ValidTrackInfoRequest, LastFMException
2024-08-13 19:39:54 -04:00
2024-08-11 17:28:43 -04:00
class LastFM(FastAPI):
"""Last.FM Endpoints"""
2025-02-15 21:09:33 -05:00
def __init__(self, app: FastAPI,
util, constants) -> None: # pylint: disable=super-init-not-called
self.app: FastAPI = app
2024-08-11 17:28:43 -04:00
self.util = util
self.constants = constants
self.lastfm = importlib.import_module("lastfm_wrapper").LastFM()
2025-02-14 16:07:24 -05:00
self.endpoints: dict = {
2025-02-05 20:23:06 -05:00
"lastfm/get_artist_by_name": self.artist_by_name_handler,
"lastfm/get_artist_albums": self.artist_album_handler,
"lastfm/get_release": self.release_detail_handler,
"lastfm/get_release_tracklist": self.release_tracklist_handler,
"lastfm/get_track_info": self.track_info_handler,
2024-08-11 17:28:43 -04:00
#tbd
}
for endpoint, handler in self.endpoints.items():
2025-01-29 15:48:47 -05:00
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
2025-02-05 20:23:06 -05:00
include_in_schema=True)
2024-08-11 17:28:43 -04:00
2025-02-15 21:09:33 -05:00
async def artist_by_name_handler(self, data: ValidArtistSearchRequest) -> JSONResponse:
2024-08-11 17:28:43 -04:00
"""
Get artist info
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
2024-08-11 17:28:43 -04:00
"""
2025-02-11 20:01:07 -05:00
artist: Optional[str] = data.a.strip()
2024-08-11 17:28:43 -04:00
if not artist:
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2024-08-11 17:28:43 -04:00
'err': True,
2025-02-15 21:09:33 -05:00
'errorText': 'No artist specified',
})
2024-08-11 17:28:43 -04:00
artist_result = await self.lastfm.search_artist(artist=artist)
if not artist_result or "err" in artist_result.keys():
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2024-08-11 17:28:43 -04:00
'err': True,
2025-02-15 21:09:33 -05:00
'errorText': 'Search failed (no results?)',
})
2024-08-11 17:28:43 -04:00
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2024-08-11 17:28:43 -04:00
'success': True,
2025-02-15 21:09:33 -05:00
'result': artist_result,
})
2024-08-11 17:33:33 -04:00
2025-02-15 21:09:33 -05:00
async def artist_album_handler(self, data: ValidArtistSearchRequest) -> JSONResponse:
2024-08-11 17:33:33 -04:00
"""
Get artist's albums/releases
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
2024-08-11 17:33:33 -04:00
"""
2025-02-11 20:01:07 -05:00
artist: str = data.a.strip()
2024-08-11 17:33:33 -04:00
if not artist:
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2024-08-11 17:33:33 -04:00
'err': True,
2025-02-15 21:09:33 -05:00
'errorText': 'Invalid request: No artist specified',
})
2024-08-11 17:33:33 -04:00
2025-02-15 21:09:33 -05:00
album_result: Union[dict, list[dict]] = await self.lastfm.get_artist_albums(artist=artist)
2025-02-16 06:36:31 -05:00
if isinstance(album_result, dict):
return JSONResponse(status_code=500, content={
'err': True,
'errorText': 'General failure.',
})
2025-02-11 20:01:07 -05:00
album_result_out: list = []
seen_release_titles: list = []
2024-08-11 17:33:33 -04:00
for release in album_result:
2025-02-14 16:07:24 -05:00
release_title: str = release.get('title', 'Unknown')
2024-08-11 17:33:33 -04:00
if release_title.lower() in seen_release_titles:
continue
seen_release_titles.append(release_title.lower())
album_result_out.append(release)
2024-08-11 17:40:10 -04:00
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2024-08-11 17:33:33 -04:00
'success': True,
'result': album_result_out
2025-02-15 21:09:33 -05:00
})
2024-08-11 17:40:10 -04:00
2025-02-15 21:09:33 -05:00
async def release_detail_handler(self, data: ValidAlbumDetailRequest) -> JSONResponse:
2024-08-11 17:40:10 -04:00
"""
Get details of a particular release by an artist
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
2025-02-11 20:01:07 -05:00
- **release**: Release title to search
2024-08-11 17:40:10 -04:00
"""
2025-02-11 20:01:07 -05:00
artist: str = data.a.strip()
release: str = data.release.strip()
2024-08-11 17:40:10 -04:00
if not artist or not release:
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2024-08-11 17:40:10 -04:00
'err': True,
2025-02-15 21:09:33 -05:00
'errorText': 'Invalid request',
})
2024-08-11 17:40:10 -04:00
release_result = await self.lastfm.get_release(artist=artist, album=release)
ret_obj = {
'id': release_result.get('id'),
'artists': release_result.get('artists'),
'title': release_result.get('title'),
'summary': release_result.get('summary'),
2025-02-15 21:09:33 -05:00
'tracks': release_result.get('tracks'),
2024-08-11 17:40:10 -04:00
}
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2024-08-11 17:40:10 -04:00
'success': True,
2025-02-15 21:09:33 -05:00
'result': ret_obj,
})
2024-08-14 07:43:55 -04:00
2025-02-15 21:09:33 -05:00
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest) -> JSONResponse:
2024-08-14 07:43:55 -04:00
"""
Get track list for a particular release by an artist
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
2025-02-11 20:01:07 -05:00
- **release**: Release title to search
2024-08-14 07:43:55 -04:00
"""
2025-02-11 20:01:07 -05:00
artist: str = data.a.strip()
release: str = data.release.strip()
2024-08-14 07:43:55 -04:00
if not artist or not release:
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2024-08-14 07:43:55 -04:00
'err': True,
2025-02-15 21:09:33 -05:00
'errorText': 'Invalid request',
})
2024-08-14 07:43:55 -04:00
2025-02-11 20:01:07 -05:00
tracklist_result: dict = await self.lastfm.get_album_tracklist(artist=artist, album=release)
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2024-08-14 07:43:55 -04:00
'success': True,
'id': tracklist_result.get('id'),
'artists': tracklist_result.get('artists'),
'title': tracklist_result.get('title'),
'summary': tracklist_result.get('summary'),
2025-02-15 21:09:33 -05:00
'tracks': tracklist_result.get('tracks'),
})
2024-08-14 08:06:02 -04:00
2025-02-15 21:09:33 -05:00
async def track_info_handler(self, data: ValidTrackInfoRequest) -> JSONResponse:
2024-08-14 08:06:02 -04:00
"""
Get track info from Last.FM given an artist/track
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
- **t**: Track title to search
2024-08-14 08:06:02 -04:00
"""
2025-01-15 20:17:49 -05:00
try:
2025-02-11 20:01:07 -05:00
artist: str = data.a
track: str = data.t
2025-01-15 20:17:49 -05:00
if not artist or not track:
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2025-01-15 20:17:49 -05:00
'err': True,
'errorText': 'Invalid request'
2025-02-15 21:09:33 -05:00
})
2025-01-15 20:17:49 -05:00
2025-02-15 21:09:33 -05:00
track_info_result: dict = await self.lastfm.get_track_info(artist=artist,
track=track)
2025-02-11 20:01:07 -05:00
if "err" in track_info_result:
raise LastFMException("Unknown error occurred: %s",
track_info_result.get('errorText', '??'))
2025-02-15 21:09:33 -05:00
return JSONResponse(content={
2025-01-15 20:17:49 -05:00
'success': True,
'result': track_info_result
2025-02-15 21:09:33 -05:00
})
2025-01-15 20:17:49 -05:00
except:
traceback.print_exc()
2025-02-15 21:09:33 -05:00
return JSONResponse(status_code=500, content={
2024-08-14 08:06:02 -04:00
'err': True,
2025-01-15 20:17:49 -05:00
'errorText': 'General error',
2025-02-15 21:09:33 -05:00
})