fix mcstatus properly

This commit is contained in:
Daniel Olsen 2022-06-21 03:22:43 +02:00
parent e5d55ba0ad
commit 6faf2022e8
1 changed files with 46 additions and 32 deletions

View File

@ -198,44 +198,58 @@ in
from mcstatus import JavaServer from mcstatus import JavaServer
import signal import signal
from time import sleep from time import sleep
import notify2
pvv = JavaServer.lookup("minecraft.pvv.ntnu.no")
dods = JavaServer.lookup("mc.dodsorf.as")
def see_players(): def getPlayers(server):
status = server.status()
players = getattr(getattr(status, "players"), "sample", [])
return players or []
def build_players(list, server):
result = "" result = ""
if pvv_status is not None: if len(list) > 0:
result += "PVV: " result += server + ": "
for player in pvv_status.players.sample: for player in list:
result += player.name + " " result += player.name + " \n"
result += "\n" return result
if dods_status is not None:
result += "DODS: "
for player in dods_status.players.sample:
result += player.name + " "
result += "\n"
signal.signal(signal.SIGUSR1, see_players) def display_players(pvv, dods):
result = build_players(getPlayers(pvv), "PVV")
result += build_players(getPlayers(dods), "DODS")
return result
def peek(*_):
result = display_players(pvv, dods)
notify2.init('Minecraft Server Status')
n = notify2.Notification("Minecraft Server Status", result)
n.show()
main()
signal.signal(signal.SIGUSR1, peek)
def main():
while True: while True:
result = "" result = ""
try: pvvs = getPlayers(pvv)
pvv = JavaServer.lookup("minecraft.pvv.ntnu.no") dodss = getPlayers(dods)
pvv_status = pvv.status() if len(pvvs) > 0:
if pvv_status.players.online > 0: result += "P" + str(len(pvvs))
result += ("P" + str(pvv_status.players.online)) if len(dodss) > 0:
except: result += "D" + str(len(dodss))
pass print(result, flush=True)
try:
dods = JavaServer.lookup("mc.dodsorf.as")
dods_status = dods.status()
if dods_status.players.online > 0:
result += ("D" + str(dods_status.players.online))
except:
pass
print(result)
sleep(5) sleep(5)
main()
''; '';
click-left = "kill -USR1 %pid%"; click-left = "kill -USR1 %pid%";
# interval = # interval =