minecraft-monitor/convert-logs.py

56 lines
2.0 KiB
Python

from json import loads, dumps
from makeline import make_line
from sys import argv
log_path = '/dev/null'
def parse_logs():
with open('all-time.txt', 'r') as fin:
(last_t, last_ev, last_evt, last_ps) = (0, 'error', 0, [])
with open(log_path+'events.json', 'w') as fout:
cur_t = 0
cur_ps = []
for line in fin:
current = loads(line)
cur_t = current['time']
cur_ps = current['players']
if last_t == 0:
(last_t, last_ev, last_evt, last_ps) = (cur_t, 'online', cur_t, cur_ps)
else:
if cur_t - last_t > 90:
if last_ev == 'online':
fout.write(dumps({'period': [last_evt, last_t], 'status': last_ev, 'players': last_ps})+'\n')
(last_ev, last_evt, last_ps) = ('unreachable', last_t, cur_ps)
else:
if last_ev != 'online':
fout.write(dumps({'period': [last_evt, last_t], 'status': last_ev})+'\n')
(last_ev, last_evt, last_ps) = ('online', last_t, last_ps)
if last_ps != cur_ps:
fout.write(dumps({'period': [last_evt, cur_t], 'status': last_ev, 'players': last_ps})+'\n')
(last_ev, last_evt, last_ps) = ('online', cur_t, cur_ps)
last_t = cur_t
with open(log_path+'current-state.json', 'w') as fout:
fout.write(dumps({'period': [last_evt, last_t], 'status': 'online', 'players': last_ps}))
def events_to_lines():
with open(log_path+'events.json', 'r') as fin:
with open(log_path+'events.log', 'w') as fout:
for l in fin:
fout.write(make_line(loads(l))+'\n')
if __name__ == "__main__":
if len(argv) > 1:
log_path = argv[1]+'/'
parse_logs()
events_to_lines()
else:
print('error: specify target path')