This commit is contained in:
2025-02-14 16:07:24 -05:00
parent 00af36703a
commit 60416c493f
19 changed files with 204 additions and 308 deletions

View File

@@ -3,7 +3,7 @@
import traceback
import logging
from typing import Optional
from typing import Optional, Union
import regex
from aiohttp import ClientSession, ClientTimeout
from constants import Constants
@@ -30,13 +30,13 @@ class LastFM:
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
request.raise_for_status()
data: dict = await request.json()
data = data.get('artist')
data = data.get('artist', 'N/A')
ret_obj: dict = {
'id': data.get('mbid'),
'touring': data.get('ontour'),
'name': data.get('name'),
'bio': data.get('bio').get('summary').strip()\
'bio': data.get('bio', None).get('summary').strip()\
.split("<a href")[0],
}
return ret_obj
@@ -68,10 +68,10 @@ class LastFM:
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
request.raise_for_status()
data: dict = await request.json()
data = data.get('track')
data = data.get('track', None)
ret_obj: dict = {
'artist_mbid': data.get('artist').get('mbid'),
'album': data.get('album').get('title'),
'artist_mbid': data.get('artist', None).get('mbid'),
'album': data.get('album', None).get('title'),
}
return ret_obj
except:
@@ -97,8 +97,8 @@ class LastFM:
'err': 'No artist or album specified',
}
tracks: list|dict = await self.get_release(artist=artist, album=album)
tracks: list|dict = tracks.get('tracks')
tracks: dict = await self.get_release(artist=artist, album=album)
tracks = tracks.get('tracks', None)
ret_obj: dict = {
'tracks': tracks,
}
@@ -129,9 +129,9 @@ class LastFM:
async with await session.get(f"{self.api_base_url}artist.gettopalbums&artist={artist}&api_key={self.creds.get('key')}&autocorrect=1&format=json",
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
request.raise_for_status()
data: dict = await request.json()
data: str = data.get('topalbums').get('album')
ret_obj: dict = [
json_data: dict = await request.json()
data: dict = data.get('topalbums', None).get('album')
ret_obj: list = [
{
'title': item.get('name')
} for item in data if not(item.get('name').lower() == "(null)") and int(item.get('playcount')) >= 50
@@ -143,7 +143,7 @@ class LastFM:
'err': 'Failed',
}
async def get_artist_id(self, artist: Optional[str] = None) -> int|dict:
async def get_artist_id(self, artist: Optional[str] = None) -> int:
"""
Get Artist ID from LastFM
Args:
@@ -153,22 +153,16 @@ class LastFM:
"""
try:
if artist is None:
return {
'err': 'No artist specified.',
}
return -1
artist_search: dict = await self.search_artist(artist=artist)
if not artist_search:
logging.debug("[get_artist_id] Throwing no result error")
return {
'err': 'No results.',
}
return -1
artist_id: int = int(artist_search[0].get('id', 0))
return artist_id
except:
traceback.print_exc()
return {
'err': 'Failed',
}
return -1
async def get_artist_info_by_id(self, artist_id: Optional[int] = None) -> dict:
"""
@@ -219,12 +213,12 @@ class LastFM:
return {
'err': 'No artist specified.',
}
artist_id: int = await self.get_artist_id(artist=artist)
artist_id: Optional[int] = await self.get_artist_id(artist=artist)
if not artist_id:
return {
'err': 'Failed',
}
artist_info: dict = await self.get_artist_info_by_id(artist_id=artist_id)
artist_info: Optional[dict] = await self.get_artist_info_by_id(artist_id=artist_id)
if not artist_info:
return {
'err': 'Failed',
@@ -258,21 +252,21 @@ class LastFM:
async with await session.get(req_url,
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
request.raise_for_status()
data: dict = await request.json()
data: dict = data.get('album')
json_data: dict = await request.json()
data: dict = json_data.get('album', None)
ret_obj: dict = {
'id': data.get('mbid'),
'artists': data.get('artist'),
'tags': data.get('tags'),
'title': data.get('name'),
'summary': data.get('wiki').get('summary').split("<a href")[0]\
'summary': data.get('wiki', None).get('summary').split("<a href")[0]\
if "wiki" in data.keys()\
else "No summary available for this release.",
}
try:
track_key: list = data.get('tracks').get('track')
track_key: list = data.get('tracks', None).get('track')
except:
track_key: list = []
track_key = []
if isinstance(track_key, list):
ret_obj['tracks'] = [
{