mc-server-query.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # WARNING: This program is only made compatible for GNU/Linux,
  3. # because is is using the standard bash shell color formatting
  4. # import modules
  5. from mcipc.query import Client
  6. import os.system
  7. import time
  8. import sys
  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(sys.argv)<2:
  16. seconds, stop_cmd = 1, False
  17. elif sys.argv[1]=="0":
  18. stop_cmd = True
  19. else:
  20. seconds, stop_cmd = int(sys.argv[1]), False
  21. # first cleanup if sleep timer given
  22. if not(stop_cmd):
  23. os.system("clear")
  24. while True:
  25. # get query
  26. with Client('127.0.0.1', 25565) as client:
  27. stats = client.full_stats
  28. # print motd
  29. minecraft_to_terminal_colors = { "§0": "\\e[30m", "§1": "\\e[34m", "§2":"\\e[32m", "§2":"\\e[36m", "§4":"\\e[31m", "§5":"\\e[35m", "§6":"\\e[33m", "§7":"\\e[37m", "§8":"\\e[90m","§9":"\\e[94m", "§a":"\\e[92m", "§b":"\\e[96m", "§c":"\\e[91m", "§d":"\\e[95m", "§e":"\\e[93m", "§f":"\\e[97m", "§r":""} # WARNING: "§r" gets removed
  30. motd="echo -e \""+replace_all(stats[2], minecraft_to_terminal_colors)+"\\e[39m\""
  31. os.system(motd)
  32. # print server type
  33. cache_text = str(stats[6])[str(stats[6]).find('\'')+1:]
  34. os.system("echo -e \"\n\\e[97mServer: \\e[39m"+stats[4]+" "+cache_text[:cache_text.find('\'')]+"\"")
  35. # list all players
  36. player_list = ""
  37. for i in stats[12]:
  38. player_list += i+", "
  39. player_list = player_list[:-2]
  40. os.system("echo -e \"\\e[97mPlayers ["+str(stats[8])+"/"+str(stats[9])+"]: \\e[39m"+player_list+"\"")
  41. # reload after timer or stop completely
  42. if stop_cmd:
  43. break
  44. else:
  45. time.sleep(seconds)
  46. os.system("clear")