diff --git a/lastfm_wrapper.py b/lastfm_wrapper.py new file mode 100644 index 0000000..7adc4f3 --- /dev/null +++ b/lastfm_wrapper.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python3.11 + +from aiohttp import ClientSession, ClientTimeout +import json +import regex +import traceback + +from constants import Constants +from typing import Union, Any + +class LastFM: + def __init__(self, noInit: Union[None, bool] = False): + self.creds = Constants().LFM_CREDS + self.api_base_url = "https://ws.audioscrobbler.com/2.0/?method=" + + + async def search_artist(self, artist=None): + try: + if artist is None: + return { + 'err': 'No artist specified.' + } + + async with ClientSession() as session: + async with session.get(f"{self.api_base_url}artist.getinfo&artist={artist}&api_key={self.creds.get('key')}&autocorrect=1&format=json", timeout=ClientTimeout(connect=3, sock_read=8)) as request: + assert request.status in [200, 204] + data = await request.json() + data = data.get('artist') + + print(f"Using data:\n{data}") + # return data.get('results') + retObj = { + 'id': data.get('mbid'), + 'touring': data.get('ontour'), + 'name': data.get('name'), + 'bio': data.get('bio').get('summary').strip().split("