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.
CodePudding user response:
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