significant refactor/cleanup
This commit is contained in:
@ -3,9 +3,10 @@
|
||||
|
||||
import importlib
|
||||
import traceback
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI
|
||||
from .constructors import ValidArtistSearchRequest, ValidAlbumDetailRequest,\
|
||||
ValidTrackInfoRequest
|
||||
ValidTrackInfoRequest, LastFMException
|
||||
|
||||
class LastFM(FastAPI):
|
||||
"""Last.FM Endpoints"""
|
||||
@ -34,7 +35,7 @@ class LastFM(FastAPI):
|
||||
Get artist info
|
||||
- **a**: Artist to search
|
||||
"""
|
||||
artist = data.a.strip()
|
||||
artist: Optional[str] = data.a.strip()
|
||||
if not artist:
|
||||
return {
|
||||
'err': True,
|
||||
@ -53,24 +54,24 @@ class LastFM(FastAPI):
|
||||
'result': artist_result
|
||||
}
|
||||
|
||||
async def artist_album_handler(self, data: ValidArtistSearchRequest):
|
||||
async def artist_album_handler(self, data: ValidArtistSearchRequest) -> dict:
|
||||
"""
|
||||
Get artist's albums/releases
|
||||
- **a**: Artist to search
|
||||
"""
|
||||
artist = data.a.strip()
|
||||
artist: str = 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 = []
|
||||
album_result: dict|list[dict] = await self.lastfm.get_artist_albums(artist=artist)
|
||||
album_result_out: list = []
|
||||
seen_release_titles: list = []
|
||||
|
||||
for release in album_result:
|
||||
release_title = release.get('title')
|
||||
release_title: str = release.get('title')
|
||||
if release_title.lower() in seen_release_titles:
|
||||
continue
|
||||
seen_release_titles.append(release_title.lower())
|
||||
@ -81,14 +82,14 @@ class LastFM(FastAPI):
|
||||
'result': album_result_out
|
||||
}
|
||||
|
||||
async def release_detail_handler(self, data: ValidAlbumDetailRequest):
|
||||
async def release_detail_handler(self, data: ValidAlbumDetailRequest) -> dict:
|
||||
"""
|
||||
Get details of a particular release by an artist
|
||||
- **a**: Artist to search
|
||||
- **a2**: Release title to search (subject to change)
|
||||
- **release**: Release title to search
|
||||
"""
|
||||
artist = data.a.strip()
|
||||
release = data.a2.strip()
|
||||
artist: str = data.a.strip()
|
||||
release: str = data.release.strip()
|
||||
|
||||
if not artist or not release:
|
||||
return {
|
||||
@ -110,14 +111,14 @@ class LastFM(FastAPI):
|
||||
'result': ret_obj
|
||||
}
|
||||
|
||||
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest):
|
||||
async def release_tracklist_handler(self, data: ValidAlbumDetailRequest) -> dict:
|
||||
"""
|
||||
Get track list for a particular release by an artist
|
||||
- **a**: Artist to search
|
||||
- **a2**: Release title to search (subject to change)
|
||||
- **release**: Release title to search
|
||||
"""
|
||||
artist = data.a.strip()
|
||||
release = data.a2.strip()
|
||||
artist: str = data.a.strip()
|
||||
release: str = data.release.strip()
|
||||
|
||||
if not artist or not release:
|
||||
return {
|
||||
@ -125,7 +126,7 @@ class LastFM(FastAPI):
|
||||
'errorText': 'Invalid request'
|
||||
}
|
||||
|
||||
tracklist_result = await self.lastfm.get_album_tracklist(artist=artist, album=release)
|
||||
tracklist_result: dict = await self.lastfm.get_album_tracklist(artist=artist, album=release)
|
||||
return {
|
||||
'success': True,
|
||||
'id': tracklist_result.get('id'),
|
||||
@ -135,15 +136,15 @@ class LastFM(FastAPI):
|
||||
'tracks': tracklist_result.get('tracks')
|
||||
}
|
||||
|
||||
async def track_info_handler(self, data: ValidTrackInfoRequest):
|
||||
async def track_info_handler(self, data: ValidTrackInfoRequest) -> dict:
|
||||
"""
|
||||
Get track info from Last.FM given an artist/track
|
||||
- **a**: Artist to search
|
||||
- **t**: Track title to search
|
||||
"""
|
||||
try:
|
||||
artist = data.a
|
||||
track = data.t
|
||||
artist: str = data.a
|
||||
track: str = data.t
|
||||
|
||||
if not artist or not track:
|
||||
return {
|
||||
@ -151,8 +152,10 @@ class LastFM(FastAPI):
|
||||
'errorText': 'Invalid request'
|
||||
}
|
||||
|
||||
track_info_result = await self.lastfm.get_track_info(artist=artist, track=track)
|
||||
assert not "err" in track_info_result.keys()
|
||||
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 {
|
||||
'success': True,
|
||||
'result': track_info_result
|
||||
|
Reference in New Issue
Block a user