cleanup
This commit is contained in:
parent
ba849449ce
commit
b532238d80
@ -11,7 +11,7 @@ class LastFM:
|
||||
def __init__(self,
|
||||
noInit: Optional[bool] = False) -> None:
|
||||
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:
|
||||
"""Search LastFM for an artist"""
|
||||
@ -21,8 +21,18 @@ class LastFM:
|
||||
'err': 'No artist specified.',
|
||||
}
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("method", "artist.getInfo"),
|
||||
("artist", artist),
|
||||
("api_key", self.creds.get('key')),
|
||||
("autocorrect", "1"),
|
||||
("format", "json"),
|
||||
]
|
||||
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(f"{self.api_base_url}artist.getinfo&artist={artist}&api_key={self.creds.get('key')}&autocorrect=1&format=json",
|
||||
|
||||
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()
|
||||
data: dict = await request.json()
|
||||
@ -59,17 +69,29 @@ class LastFM:
|
||||
'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 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,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
request.raise_for_status()
|
||||
data: dict = await request.json()
|
||||
data = data.get('track', None)
|
||||
if not isinstance(data.get('artist'), dict):
|
||||
return None
|
||||
artist_mbid: int = data.get('artist', None).get('mbid')
|
||||
album: str = data.get('album', None).get('title')
|
||||
ret_obj: dict = {
|
||||
'artist_mbid': data.get('artist', None).get('mbid'),
|
||||
'album': data.get('album', None).get('title'),
|
||||
'artist_mbid': artist_mbid,
|
||||
'album': album,
|
||||
}
|
||||
return ret_obj
|
||||
except:
|
||||
@ -122,8 +144,17 @@ class LastFM:
|
||||
'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 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,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
request.raise_for_status()
|
||||
json_data: dict = await request.json()
|
||||
@ -131,7 +162,8 @@ class LastFM:
|
||||
ret_obj: list = [
|
||||
{
|
||||
'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
|
||||
except:
|
||||
@ -175,11 +207,16 @@ class LastFM:
|
||||
'err': 'Invalid/no artist_id specified.',
|
||||
}
|
||||
|
||||
req_url: str = f"{self.api_base_url}artists/{artist_id}?key={self.creds.get('key')}\
|
||||
&secret={self.creds.get('secret')}"
|
||||
req_url: str = f"{self.api_base_url}artists/{artist_id}"
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("key", self.creds.get('key')),
|
||||
("secret", self.creds.get('secret')),
|
||||
]
|
||||
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(req_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
request.raise_for_status()
|
||||
data: dict = await request.json()
|
||||
@ -253,8 +290,18 @@ class LastFM:
|
||||
|
||||
req_url: str = f"{self.api_base_url}album.getinfo&artist={artist}&album={album}\
|
||||
&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 await session.get(req_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
request.raise_for_status()
|
||||
json_data: dict = await request.json()
|
||||
|
Loading…
x
Reference in New Issue
Block a user