-2

I made a little script to get the UUID's of people who joined my minecraft server, then run them through the PlayerDB API through a post request to: https://playerdb.co/api/player/minecraft/* where the * is a player UUID, however it returns random 404 errors.

Error runs on the player_name_history line and highlights KeyError at 'player':

def getPlayerData(UUID_list):
    baseurl = "https://playerdb.co/api/player/minecraft/"
    player_names=[]
    icon_list = []

    for UUID in UUID_list:
        response = requests.post(baseurl+UUID)
        print("url posted:",baseurl+UUID)
        print(response.status_code)
        responseObject = response.json()

        with open('responseObject.json','w') as f:
            json.dump(responseObject, f)

        player_name_history = responseObject['data']['player']['meta']['name_history']

        for x in player_name_history:
            player_name = x['name'] #iterates through all names, last name will not be overrwritten
        
        player_names.append(player_name)

        icon_link = responseObject['data']['player']['avatar']

        icon_list.append(icon_link)

    return player_names, icon_list

        for x in player_name_history:
            player_name = x['name'] #iterates through all names, last name will not be overrwritten
        
        player_names.append(player_name)

        icon_link = responseObject['data']['player']['avatar']

        icon_list.append(icon_link)

    return player_names, icon_list

You can pass my UUID into the function as a list to test:

['bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310']

Example of it working:

url posted: https://playerdb.co/api/player/minecraft/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310
200
(['zonkedzolt'], ['https://crafthead.net/avatar/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310'])

Example of it not working:

url posted: https://playerdb.co/api/player/minecraft/bf22088f-3d5b-45ef-b7dd-8d5bd3cdc310
404
Traceback (most recent call last):
  File "g:\*\getPlayerData.py", line 74, in <module>
    print(getPlayerData(UUID_list))
  File "g:\*\getPlayerData.py", line 58, in getPlayerData
    player_name_history = responseObject['data']['player']['meta']['name_history']
KeyError: 'player'

json Dump: {"message": "", "code": "api.404", "data": {}, "success": false, "error": false}

The reason why i suspect it might be my code is because when i get the error, if i ctrl+left click the link on the "url posted:" line it brings up the correct result.

zoltan
  • 3
  • 4
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer (simpler to select and copy), and more people will see it - so more people can help you. – furas Aug 31 '22 at 16:18
  • 1
    this API doesn't look like official API. So it may scrape data unofficially from pages and these pages may block it – furas Aug 31 '22 at 16:39
  • another suggestion: always put code with correct indentations. Your code has wrong indentations so it is useless - we can't see if problem is indentation or something else. You could put code, select it and use `Ctrl+K` – furas Aug 31 '22 at 17:00
  • i think it should have the proper formatting now, thanks for the help! – zoltan Sep 01 '22 at 11:40
  • as I said: it looks like `unofficial` API and it can be blocked by `official` servers. You may have to check `if "player" in responseObject['data']:` and repeat request. – furas Sep 01 '22 at 12:02
  • found out that the API's problem was that it would cache UUID requests after their first time being searched, and had some problems retrieving them if you request the UUID again, I made a quick workaround and now it works :D. thanks for your help, ill be closing the question now. – zoltan Sep 01 '22 at 15:57

1 Answers1

0

If you are getting error messages from the API like this:

{"message": "", "code": "api.404", "data": {}, "success": false, "error": false}

try requesting each UUID seperately and once you have gotten a good response, try running it with your program. It seems that the API caches UUIDs that have been asked for the first time, but if you ask again too quickly it will return the error above.

Occasionally I still run into the error above, but a re-request sorts it out. You can make a while loop to keep requesting until you recieve a good response. Here is the while loop I made:

goodResponse = False

        while goodResponse == False: 
            response = requests.post(baseurl+UUID)
            print("url posted:",baseurl+UUID)
            print(response.status_code)
            responseObject = response.json()

            if "player" in responseObject['data']: #checks if UUID has recieved a good response, otherwise loops until it does.
                goodResponse = True #continue code here
zoltan
  • 3
  • 4
  • I would seriously consider using the official mojang api instead because its really easy to use. – zoltan Sep 03 '22 at 22:52