mc-server-query.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # WARNING: Only on GNU/Linux you see the colors of this script,
  3. # because it is using the standard bash shell color formatting
  4. # import modules
  5. from mcipc.query import Client
  6. from os import name as os_name, system
  7. from time import sleep
  8. from sys import argv
  9. # define "replace_all" function
  10. def replace_all(text, dic):
  11. for i, j in dic.items():
  12. text = text.replace(i, j)
  13. return text
  14. # define sleep time
  15. if len(argv)<2:
  16. seconds, stop_cmd = 1, False
  17. elif argv[1]=="0":
  18. stop_cmd = True
  19. else:
  20. seconds, stop_cmd = int(argv[1]), False
  21. # Define colors for bash shell, WARNING: "§r" gets removed and font styles are not integrated
  22. minecraft_to_terminal_colors = { "§0": "\\e[30m", "§1": "\\e[34m", "§2":"\\e[32m", "§2":"\\e[36m", "§4":"\\e[31m",
  23. "§5":"\\e[35m", "§6":"\\e[33m", "§7":"\\e[37m", "§8":"\\e[90m","§9":"\\e[94m", "§a":"\\e[92m", "§b":"\\e[96m",
  24. "§c":"\\e[91m", "§d":"\\e[95m", "§e":"\\e[93m", "§f":"\\e[97m", "§r":""}
  25. # first cleanup if sleep timer given
  26. if not(stop_cmd):
  27. system("clear")
  28. while True:
  29. # try to get query
  30. try:
  31. with Client('127.0.0.1', 25565) as client:
  32. stats = client.full_stats
  33. except ConnectionRefusedError:
  34. system("echo -e \"\\e[31mWarning: \\e[97m'ConnectionRefusedError' [Errno 111] detected\\e[39m\"")
  35. # print motd
  36. motd="echo -e \""+replace_all(stats[2], minecraft_to_terminal_colors)+"\\e[39m\""
  37. system(motd)
  38. # print server type
  39. cache_text = str(stats[6])[str(stats[6]).find('\'')+1:]
  40. system("echo -e \"\n\\e[97mServer: \\e[92m"+stats[4]+" "+cache_text[:cache_text.find('\'')]+"\\e[39m\"")
  41. # list all players
  42. player_list = ""
  43. for i in stats[12]:
  44. player_list += i+", "
  45. player_list = player_list[:-2]
  46. system("echo -e \"\\e[97mPlayers ["+str(stats[8])+"/"+str(stats[9])+"]: \\e[92m"+player_list+"\\e[39m\"")
  47. # reload after timer or stop completely
  48. if stop_cmd:
  49. break
  50. else:
  51. sleep(seconds)
  52. system("clear")