api/endpoints/lastfm.py

213 lines
6.3 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
2024-08-11 17:28:43 -04:00
from fastapi import FastAPI
from pydantic import BaseModel
class ValidArtistSearchRequest(BaseModel):
"""
- **a**: artist name
"""
a: str
2025-01-11 20:59:10 -05:00
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem"
}
}
2024-08-13 19:39:54 -04:00
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
2025-01-11 20:59:10 -05:00
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"a2": "houdini"
}
2024-08-13 19:39:54 -04:00
}
2024-08-14 08:06:02 -04:00
class ValidTrackInfoRequest(BaseModel):
"""
- **a**: artist name
- **t**: track
"""
a: str
t: str
2025-01-11 20:59:10 -05:00
class Config: # pylint: disable=missing-class-docstring
schema_extra = {
"example": {
"a": "eminem",
"t": "rap god"
}
2024-08-14 08:06:02 -04:00
}
2024-08-13 19:39:54 -04:00
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 = {
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
async def artist_by_name_handler(self, data: ValidArtistSearchRequest):
"""
Get artist info
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
2024-08-11 17:28:43 -04:00
"""
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):
"""
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
"""
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 details of a particular release by an artist
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
- **a2**: Release title to search (subject to change)
2024-08-11 17:40:10 -04:00
"""
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 track list for a particular release by an artist
2025-02-05 20:23:06 -05:00
- **a**: Artist to search
- **a2**: Release title to search (subject to change)
2024-08-14 07:43:55 -04:00
"""
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')
2024-08-14 08:06:02 -04:00
}
async def track_info_handler(self, data: ValidTrackInfoRequest):
"""
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:
artist = data.a
track = data.t
if not artist or not track:
return {
'err': True,
'errorText': 'Invalid request'
}
track_info_result = await self.lastfm.get_track_info(artist=artist, track=track)
assert not "err" in track_info_result.keys()
return {
'success': True,
'result': track_info_result
}
except:
traceback.print_exc()
2025-01-11 20:59:10 -05:00
return {
2024-08-14 08:06:02 -04:00
'err': True,
2025-01-15 20:17:49 -05:00
'errorText': 'General error',
}