api/endpoints/lastfm.py

161 lines
4.7 KiB
Python
Raw Normal View History

2024-08-11 17:28:43 -04:00
#!/usr/bin/env python3.12
import importlib
from fastapi import FastAPI
from pydantic import BaseModel
class ValidArtistSearchRequest(BaseModel):
"""
- **a**: artist name
"""
a: str
2024-08-13 19:39:54 -04:00
class Config:
schema_extra = {
"example": {
"a": "eminem"
}
}
2024-08-11 17:40:10 -04:00
class ValidAlbumDetailRequest(BaseModel):
"""
- **a**: artist name
- **a2**: album/release name (as sourced from here/LastFM)
"""
a: str
a2: str
2024-08-13 19:39:54 -04:00
class Config:
schema_extra = {
"example": {
"a": "eminem",
"a2": "houdini"
}
}
2024-08-11 17:28:43 -04:00
class LastFM(FastAPI):
"""Last.FM Endpoints"""
2024-08-13 19:21:48 -04:00
def __init__(self, app: FastAPI, util, constants, glob_state): # pylint: disable=super-init-not-called
2024-08-11 17:28:43 -04:00
self.app = app
self.util = util
self.constants = constants
2024-08-13 19:21:48 -04:00
self.glob_state = glob_state
2024-08-11 17:28:43 -04:00
self.lastfm = importlib.import_module("lastfm_wrapper").LastFM()
self.endpoints = {
"get_artist_by_name": self.artist_by_name_handler,
2024-08-11 17:33:33 -04:00
"get_artist_albums": self.artist_album_handler,
2024-08-11 17:40:10 -04:00
"get_release": self.release_detail_handler,
2024-08-14 07:43:55 -04:00
"get_album_tracklist": self.release_tracklist_handler,
2024-08-11 17:28:43 -04:00
#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):
"""
2024-08-11 17:40:10 -04:00
/get_artist_by_name/
2024-08-11 17:28:43 -04:00
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
}
2024-08-11 17:33:33 -04:00
async def artist_album_handler(self, data: ValidArtistSearchRequest):
"""
2024-08-11 17:40:10 -04:00
/get_artist_albums/
2024-08-11 17:33:33 -04:00
Get artist's albums/releases
"""
artist = data.a.strip()
if not artist:
return {
'err': True,
'errorText': 'No artist specified'
}
album_result = await self.lastfm.get_artist_albums(artist=artist)
album_result_out = []
seen_release_titles = []
for release in album_result:
release_title = release.get('title')
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
2024-08-11 17:33:33 -04:00
return {
'success': True,
'result': album_result_out
}
2024-08-11 17:40:10 -04:00
async def release_detail_handler(self, data: ValidAlbumDetailRequest):
"""
/get_release/
Get details of a particular release by an artist
"""
artist = data.a.strip()
release = data.a2.strip()
if not artist or not release:
return {
'err': True,
'errorText': 'Invalid request'
}
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'),
'tracks': release_result.get('tracks')
}
return {
'success': True,
'result': ret_obj
}
2024-08-14 07:43:55 -04:00
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest):
"""
/get_release_tracklist/
Get track list for a particular release by an artist
"""
artist = data.a.strip()
release = data.a2.strip()
if not artist or not release:
return {
'err': True,
'errorText': 'Invalid request'
}
tracklist_result = await self.lastfm.get_album_tracklist(artist=artist, album=release)
return {
'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')
}