-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcstat.py
101 lines (92 loc) · 3.1 KB
/
mcstat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from mcstatus import JavaServer
from typing import Tuple
import asyncio
import json
import time
from apscheduler.schedulers.blocking import BlockingScheduler
import matplotlib.pyplot as plt
from matplotlib import font_manager
async def get_server_data(name: str, address: str) -> Tuple[dict,bool]:
try:
server = await JavaServer.async_lookup(address,timeout=10)
status = server.status()
except:
dict = {"name":name,
"address":address,
"icon":None,
"description":None,
"ping":None,
"version":None,
"players":None,
"online/max":None
}
return dict, False
# 服务器图标
icon = status.favicon
# 服务器描述
description = status.description
# 服务器延迟
ping = int(status.latency)
# 服务器版本
version = status.version.name
# 服务器人数上限
max = status.players.max
# 服务器在线人数
online = status.players.online
# 在线玩家列表
online_players = status.players.sample
dict = {"name":name,
"address":address,
"icon":icon,
"description":description,
"ping":ping,
"version":version,
"players":online_players,
"online":online,
"max":max
}
return dict
def process():
t=time.localtime()
server_list=None
server_stat_list=None
with open("config.json","r",encoding="utf-8") as _f:
server_list=json.loads(_f.read())
try:
with open("status.json","r",encoding="utf-8") as _f:
server_stat_list=json.loads(_f.read())
except:
server_stat_list={}
print("Server List:",server_list)
if server_list.keys()!=server_stat_list.keys():
server_stat_list={}
for _key in server_list.keys():
server_stat_list[_key]=[]
for _key in server_list.keys():
result=asyncio.run(get_server_data(_key,server_list[_key]))
if(len(server_stat_list[_key])>48):
server_stat_list[_key].pop(0)
server_stat_list[_key].append([result["online"],f"{t.tm_mon}-{t.tm_mday} {t.tm_hour}:{t.tm_min}"])
print(server_stat_list)
#font=font_manager.FontProperties(fname="dengxian.ttf")
max_tick=0
plt.figure(figsize=(10,3))
plt.style.use("ggplot")
plt.rcParams["font.sans-serif"]=["SimHei"]
for _key in server_stat_list.keys():
#print([i[1] for i in server_stat_list[_key]] )
plt.plot([i[1] for i in server_stat_list[_key]],[i[0] for i in server_stat_list[_key]],label=_key,alpha=0.5)
if len(server_stat_list[_key])>max_tick:
max_tick=len(server_stat_list[_key])
plt.xticks(ticks=list(range(0,max_tick,10)),rotation=45)
plt.title("Player Statistic")
plt.legend(loc="lower left")
plt.tight_layout()
plt.savefig("plot.png")
with open("status.json","w",encoding="utf-8") as _f:
_f.write(json.dumps(server_stat_list))
if __name__ == '__main__':
process()
scheduler=BlockingScheduler()
scheduler.add_job(process,"cron",minute="0,10,20,30,40,50")
scheduler.start()