get players list from this, it's empty at the moment. python code…
- User
get players list from this, it's empty at the moment. python code
{"online":true,"host":"147.135.125.35","port":25565,"ip_address":"147.135.125.35","eula_blocked":false,"retrieved_at":1698232437295,"expires_at":1698232497295,"version":{"name_raw":"Paper 1.20.1","name_clean":"Paper 1.20.1","name_html":"\u003cspan\u003e\u003cspan style="color: #ffffff;"\u003ePaper 1.20.1\u003c/span\u003e\u003c/span\u003e","protocol":763},"players":{"online":0,"max":100,"list":[]},"motd":{"raw":"§\u0000AmV10z","clean":"AmV10z","html":"\u003cspan\u003e\u003cspan style="color: #ffffff;"\u003eAmV10z\u003c/span\u003e\u003c/span\u003e"},"icon":null,"mods":[],"software":"Paper on 1.20.1-R0.1-SNAPSHOT","plugins":[{"name":"squaremap","version":"1.1.16"},{"name":"StreamMC","version":"1.0"},{"name":"voicechat","version":"2.4.24"},{"name":"squaremarker","version":"1.0.4"},{"name":"BlueMap","version":"3.16"},{"name":"LuckPerms","version":"5.4.98"}],"srv_record":null}
request is from https://api.mcstatus.io/v2/status/java/147.135.125.35
- Assistant
To get the players list from the given JSON response, you can use the following Python code:
pythonimport json response = '''{"online":true,"host":"147.135.125.35","port":25565,"ip_address":"147.135.125.35","eula_blocked":false,"retrieved_at":1698232437295,"expires_at":1698232497295,"version":{"name_raw":"Paper 1.20.1","name_clean":"Paper 1.20.1","name_html":"\u003cspan\u003e\u003cspan style=\"color: #ffffff;\"\u003ePaper 1.20.1\u003c/span\u003e\u003c/span\u003e","protocol":763},"players":{"online":0,"max":100,"list":[]},"motd":{"raw":"§\u0000AmV10z","clean":"AmV10z","html":"\u003cspan\u003e\u003cspan style=\"color: #ffffff;\"\u003eAmV10z\u003c/span\u003e\u003c/span\u003e"},"icon":null,"mods":[],"software":"Paper on 1.20.1-R0.1-SNAPSHOT","plugins":[{"name":"squaremap","version":"1.1.16"},{"name":"StreamMC","version":"1.0"},{"name":"voicechat","version":"2.4.24"},{"name":"squaremarker","version":"1.0.4"},{"name":"BlueMap","version":"3.16"},{"name":"LuckPerms","version":"5.4.98"}],"srv_record":null}''' json_data = json.loads(response) players_list = json_data['players']['list'] print(players_list) - User
replace response with get request to https://api.mcstatus.io/v2/status/java/147.135.125.35
- Assistant
To replace the
responsevariable with a GET request to the provided URL, you can use therequestslibrary in Python. First, ensure that you have therequestslibrary installed by running the commandpip install requests. Then, you can use the following code:pythonimport json import requests url = "https://api.mcstatus.io/v2/status/java/147.135.125.35" response = requests.get(url) json_data = json.loads(response.text) players_list = json_data['players']['list'] print(players_list)This code sends a GET request to the specified URL, retrieves the JSON response, and extracts the players list from it. Finally, it prints the players list.
- User
now make use of expires_at unix timestamp so we only retrieve new data
- Assistant
To make use of the
expires_atUnix timestamp and only retrieve new data if the existing data has expired, you can modify the code as follows:pythonimport json import requests import time url = "https://api.mcstatus.io/v2/status/java/147.135.125.35" response = requests.get(url) json_data = json.loads(response.text) retrieved_at = json_data['retrieved_at'] / 1000 # Convert milliseconds to seconds expires_at = json_data['expires_at'] / 1000 current_time = time.time() # Check if the data has expired if current_time < expires_at: players_list = json_data['players']['list'] print(players_list) else: print("Data has expired. Please retrieve new data.")In this code, the
expires_attimestamp is converted from milliseconds to seconds by dividing it by 1000. The current time is obtained using . If the current time is less than the timestamp, it means that the data is still valid, and you can retrieve the players list. Otherwise, it indicates that the data has expired, and you can take appropriate action, such as fetching new data. - User
add sleep of 30 seconds
- Assistant
To add a pause of 30 seconds before sending the request again to retrieve new data, you can use the
timemodule'ssleep()function. Here is the updated code:pythonimport json import requests import time url = "https://api.mcstatus.io/v2/status/java/147.135.125.35" response = requests.get(url) json_data = json.loads(response.text) retrieved_at = json_data['retrieved_at'] / 1000 # Convert milliseconds to seconds expires_at = json_data['expires_at'] / 1000 current_time = time.time() # Check if the data has expired if current_time < expires_at: players_list = json_data['players']['list'] print(players_list) else: print("Data has expired. Retrieving new data in 30 seconds...") time.sleep(30) # Pause execution for 30 seconds # Send request again to retrieve new data response = requests.get(url) json_data = json.loads(response.text) players_list = json_data['players']['list'] print(players_list)