minecraft-monitor/generate-html.py

60 lines
1.7 KiB
Python

from json import loads
from datetime import datetime
from pytz import utc
from makeline import make_line, scale
from re import compile
from sys import argv
log_path = '/dev/null'
def get_lines(start):
lines = ''
find_stop = compile(r'x2="([0-9.]+)"')
with open(log_path+'events.log', 'r') as fin:
for l in fin:
evstop = float(find_stop.search(l).group(1))
if evstop > start:
break
lines += l
lines += fin.read()
with open(log_path+'current-state.json', 'r') as fin:
lines += make_line(loads(fin.read()))
return lines
def make_html(name, period, display_scale = 144): # display_scale = 1 pixel per 10 minutes
stop = scale*datetime.now().timestamp()
start = stop-period
day_start = scale*datetime.fromtimestamp(start/scale).replace(hour=0,minute=0,second=0,microsecond=0,tzinfo=utc).timestamp()
lines = get_lines(start)
with open('template.html', 'r') as fin:
with open(log_path+name+'.html', 'w') as fout:
fout.write(fin.read().format(
width = display_scale*period,
start = start,
period = period,
day_start = day_start,
day_stop = stop,
events = lines
))
def make_htmls():
make_html('hour', 1/24, 24*60*4) # 4 pixels per minute
make_html('day', 1, 24*60) # 1 pixel per minute
make_html('month', 31)
make_html('4months', 120)
make_html('18months', 365.256*1.5, 24) # 1 pixel per hour
if __name__ == "__main__":
if len(argv) > 1:
log_path = argv[1]+'/'
make_htmls()
else:
print('error: specify target path')