cleanup
This commit is contained in:
parent
ba849449ce
commit
b532238d80
@ -11,7 +11,7 @@ class LastFM:
|
|||||||
def __init__(self,
|
def __init__(self,
|
||||||
noInit: Optional[bool] = False) -> None:
|
noInit: Optional[bool] = False) -> None:
|
||||||
self.creds = Constants().LFM_CREDS
|
self.creds = Constants().LFM_CREDS
|
||||||
self.api_base_url: str = "https://ws.audioscrobbler.com/2.0/?method="
|
self.api_base_url: str = "https://ws.audioscrobbler.com/2.0"
|
||||||
|
|
||||||
async def search_artist(self, artist: Optional[str] = None) -> dict:
|
async def search_artist(self, artist: Optional[str] = None) -> dict:
|
||||||
"""Search LastFM for an artist"""
|
"""Search LastFM for an artist"""
|
||||||
@ -21,9 +21,19 @@ class LastFM:
|
|||||||
'err': 'No artist specified.',
|
'err': 'No artist specified.',
|
||||||
}
|
}
|
||||||
|
|
||||||
async with ClientSession() as session:
|
request_params: list[tuple] = [
|
||||||
async with await session.get(f"{self.api_base_url}artist.getinfo&artist={artist}&api_key={self.creds.get('key')}&autocorrect=1&format=json",
|
("method", "artist.getInfo"),
|
||||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
("artist", artist),
|
||||||
|
("api_key", self.creds.get('key')),
|
||||||
|
("autocorrect", "1"),
|
||||||
|
("format", "json"),
|
||||||
|
]
|
||||||
|
|
||||||
|
async with ClientSession() as session:
|
||||||
|
|
||||||
|
async with await session.get(self.api_base_url,
|
||||||
|
params=request_params,
|
||||||
|
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
data: dict = await request.json()
|
data: dict = await request.json()
|
||||||
data = data.get('artist', 'N/A')
|
data = data.get('artist', 'N/A')
|
||||||
@ -58,18 +68,30 @@ class LastFM:
|
|||||||
return {
|
return {
|
||||||
'err': 'Invalid/No artist or track specified',
|
'err': 'Invalid/No artist or track specified',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request_params: list[tuple] = [
|
||||||
|
("method", "track.getInfo"),
|
||||||
|
("api_key", self.creds.get('key')),
|
||||||
|
("autocorrect", "1"),
|
||||||
|
("artist", artist),
|
||||||
|
("track", track),
|
||||||
|
("format", "json"),
|
||||||
|
]
|
||||||
|
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with await session.get(f"{self.api_base_url}track.getInfo&api_key={self.creds.get('key')}&autocorrect=1&artist={artist}&track={track}&format=json",
|
async with await session.get(self.api_base_url,
|
||||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
params=request_params,
|
||||||
|
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
data: dict = await request.json()
|
data: dict = await request.json()
|
||||||
data = data.get('track', None)
|
data = data.get('track', None)
|
||||||
if not isinstance(data.get('artist'), dict):
|
if not isinstance(data.get('artist'), dict):
|
||||||
return None
|
return None
|
||||||
|
artist_mbid: int = data.get('artist', None).get('mbid')
|
||||||
|
album: str = data.get('album', None).get('title')
|
||||||
ret_obj: dict = {
|
ret_obj: dict = {
|
||||||
'artist_mbid': data.get('artist', None).get('mbid'),
|
'artist_mbid': artist_mbid,
|
||||||
'album': data.get('album', None).get('title'),
|
'album': album,
|
||||||
}
|
}
|
||||||
return ret_obj
|
return ret_obj
|
||||||
except:
|
except:
|
||||||
@ -122,16 +144,26 @@ class LastFM:
|
|||||||
'err': 'No artist specified.',
|
'err': 'No artist specified.',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request_params: list[tuple] = [
|
||||||
|
("method", "artist.gettopalbums"),
|
||||||
|
("artist", artist),
|
||||||
|
("api_key", self.creds.get('key')),
|
||||||
|
("autocorrect", "1"),
|
||||||
|
("format", "json"),
|
||||||
|
]
|
||||||
|
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with await session.get(f"{self.api_base_url}artist.gettopalbums&artist={artist}&api_key={self.creds.get('key')}&autocorrect=1&format=json",
|
async with await session.get(self.api_base_url,
|
||||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
params=request_params,
|
||||||
|
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
json_data: dict = await request.json()
|
json_data: dict = await request.json()
|
||||||
data: dict = json_data.get('topalbums', None).get('album')
|
data: dict = json_data.get('topalbums', None).get('album')
|
||||||
ret_obj: list = [
|
ret_obj: list = [
|
||||||
{
|
{
|
||||||
'title': item.get('name')
|
'title': item.get('name')
|
||||||
} for item in data if not(item.get('name').lower() == "(null)") and int(item.get('playcount')) >= 50
|
} for item in data if not(item.get('name').lower() == "(null)")\
|
||||||
|
and int(item.get('playcount')) >= 50
|
||||||
]
|
]
|
||||||
return ret_obj
|
return ret_obj
|
||||||
except:
|
except:
|
||||||
@ -175,12 +207,17 @@ class LastFM:
|
|||||||
'err': 'Invalid/no artist_id specified.',
|
'err': 'Invalid/no artist_id specified.',
|
||||||
}
|
}
|
||||||
|
|
||||||
req_url: str = f"{self.api_base_url}artists/{artist_id}?key={self.creds.get('key')}\
|
req_url: str = f"{self.api_base_url}artists/{artist_id}"
|
||||||
&secret={self.creds.get('secret')}"
|
|
||||||
|
request_params: list[tuple] = [
|
||||||
|
("key", self.creds.get('key')),
|
||||||
|
("secret", self.creds.get('secret')),
|
||||||
|
]
|
||||||
|
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with await session.get(req_url,
|
async with await session.get(req_url,
|
||||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
params=request_params,
|
||||||
|
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
data: dict = await request.json()
|
data: dict = await request.json()
|
||||||
if not data.get('profile'):
|
if not data.get('profile'):
|
||||||
@ -253,9 +290,19 @@ class LastFM:
|
|||||||
|
|
||||||
req_url: str = f"{self.api_base_url}album.getinfo&artist={artist}&album={album}\
|
req_url: str = f"{self.api_base_url}album.getinfo&artist={artist}&album={album}\
|
||||||
&api_key={self.creds.get('key')}&autocorrect=1&format=json"
|
&api_key={self.creds.get('key')}&autocorrect=1&format=json"
|
||||||
|
|
||||||
|
request_params: list[tuple] = [
|
||||||
|
("method", "album.getinfo"),
|
||||||
|
("artist", artist),
|
||||||
|
("album", album),
|
||||||
|
("api_key", self.creds.get('key')),
|
||||||
|
("autocorrect", "1"),
|
||||||
|
("format", "json"),
|
||||||
|
]
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
async with await session.get(req_url,
|
async with await session.get(req_url,
|
||||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
params=request_params,
|
||||||
|
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
json_data: dict = await request.json()
|
json_data: dict = await request.json()
|
||||||
data: dict = json_data.get('album', None)
|
data: dict = json_data.get('album', None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user