minestatsping/ping.py

56 lines
931 B
Python
Raw Normal View History

2023-01-14 21:27:52 +00:00
from mctools import PINGClient
2023-01-20 16:32:30 +00:00
ping = PINGClient('play.dmcraft.online')
2023-01-14 21:27:52 +00:00
#stats = ping.get_stats()
#Work with JSON
import json
def read():
global db
with open('db.json', 'r') as openfile:
db = json.load(openfile)
def write():
global db
js = json.dumps(db, indent=4)
with open("db.json", "w") as outfile:
outfile.write(js)
#My libraries
from time import sleep
#Read
read()
2023-01-20 16:20:12 +00:00
needsleep = 0
2023-01-21 10:45:53 +00:00
ttime = 0
2023-01-14 21:27:52 +00:00
while True:
2023-01-20 17:32:20 +00:00
if needsleep == 30:
needsleep = 0
sleep(30)
2023-01-21 10:45:53 +00:00
ttime = 29
2023-01-20 17:32:20 +00:00
else:
sleep(1)
needsleep += 1
2023-01-14 21:27:52 +00:00
2023-01-20 17:32:20 +00:00
try:
#if True:
stats = ping.get_stats()
if stats['players']['online'] != 0:
for i in stats['players']['sample']:
#Add in db if not in db
if i[0] not in db:
2023-01-21 10:45:53 +00:00
db[i[0]] = 1 + ttime
2023-01-20 17:32:20 +00:00
write()
else:
2023-01-21 10:45:53 +00:00
db[i[0]] = db[i[0]] + 1 + ttime
2023-01-20 17:32:20 +00:00
write()
2023-01-21 10:45:53 +00:00
ttime = 0
2023-01-14 21:27:52 +00:00
2023-01-20 17:32:20 +00:00
except Exception as e:
2023-01-22 10:23:28 +00:00
if e == '[Errno 32] Broken pipe':
sleep(60)
ttime = 59
print("CATCHED")
else:
print(e)
2023-01-20 16:20:12 +00:00