mc-server-query.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # setting default values
  15. ip='127.0.0.1'
  16. port=25565
  17. seconds=1
  18. stop_cmd=False
  19. # get values from argv
  20. if len(argv)>1:
  21. if "-ip" in argv:
  22. ip=argv[argv.index("-ip")+1]
  23. if ":" in ip:
  24. initial_ip = ip
  25. ip=ip[:ip.find(":")]
  26. port=int(initial_ip[initial_ip.find(":")+1:])
  27. if "-p" in argv:
  28. port=int(argv[argv.index("-p")+1])
  29. if "-port" in argv:
  30. port=int(argv[argv.index("-port")+1])
  31. if "-stop" in argv:
  32. stop_cmd=True
  33. if "-sec" in argv:
  34. seconds=int(argv[argv.index("-sec")+1])
  35. if ("-help" in argv) or ( "--help" in argv):
  36. print("Here comes the help (soon).")
  37. exit()
  38. # define colors for bash shell, WARNING: "§r" gets removed and font styles are not integrated
  39. minecraft_to_terminal_colors={ "§0": "\\e[30m", "§1": "\\e[34m", "§2":"\\e[32m", "§2":"\\e[36m", "§4":"\\e[31m",
  40. "§5":"\\e[35m", "§6":"\\e[33m", "§7":"\\e[37m", "§8":"\\e[90m","§9":"\\e[94m", "§a":"\\e[92m", "§b":"\\e[96m",
  41. "§c":"\\e[91m", "§d":"\\e[95m", "§e":"\\e[93m", "§f":"\\e[97m", "§r":""}
  42. while True:
  43. # clear everything
  44. # system("clear")
  45. # try to get query
  46. try:
  47. with Client(ip,port) as client:
  48. stats=client.full_stats
  49. # catch Connection Error and reloop after sleep timer
  50. except ConnectionRefusedError:
  51. system("echo -e \"\\e[31mWarning: \\e[97m'ConnectionRefusedError' [Errno 111] detected\\e[39m\"")
  52. sleep(seconds)
  53. if stop_cmd:
  54. break
  55. else:
  56. continue
  57. # print motd
  58. motd="echo -e \""+replace_all(stats[2], minecraft_to_terminal_colors)+"\\e[39m\""
  59. system(motd)
  60. # print server type
  61. cache_text=str(stats[6])[str(stats[6]).find('\'')+1:]
  62. system("echo -e \"\n\\e[97mServer: \\e[92m"+stats[4]+" "+cache_text[:cache_text.find('\'')]+"\\e[39m\"")
  63. # list all players
  64. player_list=""
  65. for i in stats[12]:
  66. player_list+=i+", "
  67. player_list=player_list[:-2]
  68. system("echo -e \"\\e[97mPlayers ["+str(stats[8])+"/"+str(stats[9])+"]: \\e[92m"+player_list+"\\e[39m\"")
  69. # reload after timer or stop completely
  70. if stop_cmd:
  71. break
  72. else:
  73. sleep(seconds)