@commands.command()
async def np(self,ctx):
async with aiohttp.ClientSession() as session:
params= {"api_key" : "censored",
"user" : "x",
"period" : "overall",
"limit" : 10,
"method":"user.getTopArtists",
"format":"json"}
async with session.get(url="http://ws.audioscrobbler.com/2.0", params=params) as response:
resp = await response.read()
print(resp)
I am making it so that it retrieves the top (1st) artist of a user, the reply is something really long which you can find here. How do I retrieve/fetch only the "rank" : 1
artist from all that mess?
You're requesting a JSON response
"format":"json"}
So this is what you get. To load it into a dictionary, use the json
library
import json
jsonData = json.loads(resp)
Now, you can get the dictionary for the first artist via
topArtist = jsonData["topartists"]["artist"][0]
And from there, you can retrieve all the info, like the url
topArtistUrl = topArtist["url"]
import json
@commands.command()
async def np(self,ctx):
async with aiohttp.ClientSession() as session:
params= {"api_key" : "censored",
"user" : "x",
"period" : "overall",
"limit" : 10,
"method":"user.getTopArtists",
"format":"json"}
async with session.get(url="http://ws.audioscrobbler.com/2.0", params=params) as response:
resp = await response.read()
jsonData = json.loads(resp)
topArtist = jsonData["topartists"]["artist"][0]
topArtistUrl = topArtist["url"]