mc-server-query.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. # WARNING: THIS PROGRAM ISN'T FUNCTIONAL AS OF RIGHT NOW
  3. # WARNING: Only on GNU/Linux you see the colors of this script,
  4. # because it is using the standard bash shell color formatting
  5. # import modules
  6. from mcipc.query import Client
  7. from os import name as os_name, system
  8. from time import sleep
  9. from sys import argv
  10. # define "replace_all" function
  11. def replace_all(text, dic):
  12. for i, j in dic.items():
  13. text=text.replace(i, j)
  14. return text
  15. # setting default values
  16. ip='127.0.0.1'
  17. port=25565
  18. seconds=1
  19. stop_cmd=False
  20. # get values from argv
  21. if len(argv)>1:
  22. if "-ip" in argv:
  23. ip=argv[argv.index("-ip")+1]
  24. if ":" in ip:
  25. initial_ip = ip
  26. ip=ip[:ip.find(":")]
  27. port=int(initial_ip[initial_ip.find(":")+1:])
  28. if "-p" in argv:
  29. port=int(argv[argv.index("-p")+1])
  30. if "-port" in argv:
  31. port=int(argv[argv.index("-port")+1])
  32. if "-stop" in argv:
  33. stop_cmd=True
  34. if "-sec" in argv:
  35. seconds=int(argv[argv.index("-sec")+1])
  36. if ("-help" in argv) or ( "--help" in argv):
  37. print("Here comes the help (soon).")
  38. exit()
  39. # define colors for bash shell, WARNING: "§r" gets removed and font styles are not integrated
  40. minecraft_to_terminal_colors={ "§0": "\\e[30m", "§1": "\\e[34m", "§2":"\\e[32m", "§2":"\\e[36m", "§4":"\\e[31m",
  41. "§5":"\\e[35m", "§6":"\\e[33m", "§7":"\\e[37m", "§8":"\\e[90m","§9":"\\e[94m", "§a":"\\e[92m", "§b":"\\e[96m",
  42. "§c":"\\e[91m", "§d":"\\e[95m", "§e":"\\e[93m", "§f":"\\e[97m", "§r":""}
  43. while True:
  44. # clear everything
  45. system("clear")
  46. # try to get query
  47. try:
  48. with Client(ip,port) as client:
  49. stats=client.full_stats
  50. # catch Connection Error and reloop after sleep timer
  51. except ConnectionRefusedError:
  52. system("echo -e \"\\e[31mWarning: \\e[97m'ConnectionRefusedError' [Errno 111] detected\\e[39m\"")
  53. sleep(seconds)
  54. if stop_cmd:
  55. break
  56. else:
  57. continue
  58. # print motd
  59. motd="echo -e \""+replace_all(stats[2], minecraft_to_terminal_colors)+"\\e[39m\""
  60. system(motd)
  61. # print server type
  62. cache_text=str(stats[6])[str(stats[6]).find('\'')+1:]
  63. system("echo -e \"\n\\e[97mServer: \\e[92m"+stats[4]+" "+cache_text[:cache_text.find('\'')]+"\\e[39m\"")
  64. # list all players
  65. player_list=""
  66. for i in stats[12]:
  67. player_list+=i+", "
  68. player_list=player_list[:-2]
  69. system("echo -e \"\\e[97mPlayers ["+str(stats[8])+"/"+str(stats[9])+"]: \\e[92m"+player_list+"\\e[39m\"")
  70. # reload after timer or stop completely
  71. if stop_cmd:
  72. break
  73. else:
  74. sleep(seconds)