minestatsping/ping.py

50 lines
896 B
Python
Raw Normal View History

2023-01-14 21:27:52 +00:00
from mctools import PINGClient
2023-03-08 12:40:23 +00:00
ping = PINGClient('135.181.170.94',25630)
2023-01-14 21:27:52 +00:00
#stats = ping.get_stats()
2023-03-08 12:40:23 +00:00
#Get home dir
from subprocess import getoutput
global home
home = getoutput("echo $HOME")
2023-01-14 21:27:52 +00:00
#Work with JSON
import json
def read():
2023-03-08 12:40:23 +00:00
global db, home
with open(f'{home}/db.json', 'r') as openfile:
2023-01-14 21:27:52 +00:00
db = json.load(openfile)
def write():
global db
js = json.dumps(db, indent=4)
2023-03-08 12:40:23 +00:00
with open(f'{home}/db.json', 'w') as outfile:
2023-01-14 21:27:52 +00:00
outfile.write(js)
#My libraries
from time import sleep
#Read
read()
2023-01-21 10:45:53 +00:00
ttime = 0
2023-01-14 21:27:52 +00:00
while True:
2023-01-22 10:25:33 +00:00
sleep(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-03-08 12:40:23 +00:00
ping = PINGClient('135.181.170.94',25630)
print(e)
2023-01-20 16:20:12 +00:00