import json import math import os import requests import time from keepalive import…
- User
import json import math import os import requests import time from keep_alive import keep_alive
keep_alive()
url = ("https://api.mcstatus.io/v2/status/java/" + os.environ['SERVER_IP'])
while True: try: response = requests.get(url) json_data = json.loads(response.text)
expires_at = json_data['expires_at'] / 1000 current_time = time.time() print("Current time:", current_time, " Expires at:", expires_at) players_list = json_data['players']['list'] players_online = json_data['players']['online'] players_list_names = [player["name_clean"] for player in players_list] print("Players online:", players_online) if players_online > 0: print(players_list_names) # Read the players list from the file with open("players.json", "r") as file: file_data = file.read() file_list_names = eval(file_data)Compare the lists
common_players = set(players_list_names) & set(file_list_names) new_players = set(players_list_names) - set(file_list_names) removed_players = set(file_list_names) - set(players_list_names) if len(common_players) > 0: print("Common Players:", common_players) if len(new_players) > 0: print("New Players:", new_players) if len(removed_players) > 0: print("Removed Players:", removed_players) if len(removed_players) or len(new_players): list_leaving = f"- {', '.join(removed_players)}" list_joining = f"+ {', '.join(new_players)}" web_content = f"<t:{math.trunc(expires_at)}> Online: {players_online}" web_content += "```diff" if len(removed_players) > 0: web_content += f"\n{list_leaving}" if len(new_players) > 0: web_content += f"\n{list_joining}" web_content += "```" data = { 'content': web_content } requests.post(os.environ['WEBHOOK_URL'], json=data) with open("players.json", "w") as file: json.dump(players_list_names, file) time.sleep(expires_at - current_time)except Exception as e: print ("Error:", e) time.sleep(60)
- Assistant
The code provided is a Python script that utilizes the
mcstatusAPI to retrieve information about a Minecraft server. The script continuously polls the server for information and compares it to a previous list of players stored in a file. If there are any changes in the players online (new players joining or players leaving), the script sends a notification to a specified webhook URL. - User
make so it writes online status into a file and reads from it
- Assistant