PlayerDB API Post Requests bring 404

2024/11/19 10:26:47

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 overrwrittenplayer_names.append(player_name)icon_link = responseObject['data']['player']['avatar']icon_list.append(icon_link)return player_names, icon_listfor x in player_name_history:player_name = x['name'] #iterates through all names, last name will not be overrwrittenplayer_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 getPlayerDataplayer_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.

Answer

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 = Falsewhile 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
https://en.xdnf.cn/q/119961.html

Related Q&A

How to make changes using Configparser in .ini file persistent

How to modify the .ini file? My ini file looks like this. And i want the format section ini to be changed like this[Space to be replaced with a tab followed by $] Format="[%TimeStamp%] $(%Thre…

How to structure template libraries in a Django project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Not able to click button with Selenium (Python)

Im trying to click a button using Selenium, but everytime I get the message that it cant find the element. This happens even when I put a time.sleep() in front of it. time.sleep(5)#Click on downloaddow…

how to aproximate shapes height and width for image detection using opencv and python

i was following a tutorial about shapes detection using opencv ,numpy and python ,and it was this function i know the reason from it but i do not know how to modify it so i can use it as i want the to…

Using str.replace in a for loop

I am working on an assignment that is asking me to change the below code so that line 4 uses str.isalnum and lines 5-7 become uses only one line using str.replace.s = p55w-r@d result = for c in s:if(c…

extract all vertical slices from numpy array

I want to extract a complete slice from a 3D numpy array using ndeumerate or something similar. arr = np.random.rand(4, 3, 3)I want to extract all possible arr[:, x, y] where x, y range from 0 to 2

Output an OrderedDict to CSV

I read a CSV file and use the usaddress library to parse an address field. How do I write the resulting OrderedDicts to another CSV file?import usaddress import csvwith open(output.csv) as csvfile:re…

min() arg is an empty sequence

I created a function that consumes a list of values and produces the average. However it only works when I use integer values. I get the following error when I make the values into floats:min() arg is …

Removing last words in each row in pandas dataframe

A dataframe contains a column named full_name and the rows look like this: full_name Peter Eli Smith Vanessa Mary Ellen Raul Gonzales Kristine S Lee How do I remove the last words and add an additi…

Object of type function has no len() in python

I have been searching for a solution for this error for a while but the solutions that have helped others have not been much help for me.Here is the code that Ive wrote.def main():while True:userInput(…