1

I'm currently making a discord bot that will display osu! stats. This is my code:

@bot.command()
async def osu(osu_name : str):
    """Adds two numbers together."""
    await bot.say("Fetching data")
    url = 'https://osu.ppy.sh/api/get_user?k={ my api key }&u=' + osu_name
    # Do the HTTP get request
    response = requests.get(url, verify=True) #Verify is check SSL certificate
    # Decode the JSON response into a dictionary and use the data
    await bot.say(response.json())

The osu! api gives this in its json:

[{
    "user_id"      : "1",
    "username"     : "User name",
    "count300"     : "1337",      // Total amount for all ranked and approved beatmaps played
    "count100"     : "123",       // Total amount for all ranked and approved beatmaps played
    "count50"      : "69",        // Total amount for all ranked and approved beatmaps played
    "playcount"    : "42",        // Only counts ranked and approved beatmaps
    "ranked_score" : "666666",    // Counts the best individual score on each ranked and approved beatmaps
    "total_score"  : "999999998", // Counts every score on ranked and approved beatmaps
    "pp_rank"      : "2442",
    "level"        : "50.5050",
    "pp_raw"       : "3113",
    "accuracy"     : "98.1234",
    "count_rank_ss": "54",
    "count_rank_s" : "81",        // Counts for SS/S/A ranks on maps
    "count_rank_a" : "862",    
    "country"      : "DE",        // Uses the ISO3166-1 alpha-2 country code naming. See this for more information: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2/wiki/ISO_3166-1_alpha-2)
    "pp_country_rank":"1337",     // The user's rank in the country.
    "events"       : [{           // Contains events for this user
        "display_html"  : "<img src='\/images\/A_small.png'\/>...",
        "beatmap_id"    : "222342",
        "beatmapset_id" : "54851",
        "date"      : "2013-07-07 22:34:04",
        "epicfactor"    : "1"      // How "epic" this event is (between 1 and 32)
    }, { ... }, ...]
}]

My code it displays it like this in discord

discord screenshot

I would like to only show a few of the things from the json, such as the "pp_raw", "level", "accuracy", etc.

2
  • Okay, so only print part of the JSON. You can access it the same way you would access any other dictionary. Commented Jul 6, 2016 at 19:09
  • That is not quite accurate. Commented Jul 6, 2016 at 19:11

2 Answers 2

1

The data = response.json() returns a list of dictionary objects. So you have to specify the index first before accessing the value. Suppose you want to access the pp_raw in the first object, it will look like:

print data[0]['pp_raw']
Sign up to request clarification or add additional context in comments.

Comments

1

You can use python's builtin json module to convert the json response into a python dictionary - just import json and then do something like this to access individual elements of the response: data = json.loads(response.json())[0]. Then you can access individual elements like data['pp_raw'] and display them as you see fit.

3 Comments

could you give a snippet for the whole thing i just tried it and it didn't work
it did not display the data when i added data = json.loads(response.json()) await bot.say(data['pp_raw'])
Sorry, I missed that the response was a list. editing now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.