minecraft-monitor/generate-html.py

62 lines
2.1 KiB
Python

from json import loads
from datetime import datetime
from re import compile
# scale down coordinates to dx=1 per day to prevent SVG coordinate overflows
scale = 1/60/60/24
def get_date(dtime):
return datetime.fromtimestamp(dtime).strftime('%Y-%m-%d %H:%M')
def make_line(event):
evstart = event['period'][0]
evstop = event['period'][1]
return '<line{type} x1="{start}" x2="{stop}" y1="{height}" y2="{height}"><title>{text} ({start_date}{stop_date})</title></line>'.format(
type = '' if event['status'] == 'online' else ' class="error"',
start = scale*evstart,
stop = scale*evstop,
start_date = get_date(evstart),
stop_date = get_date(evstop),
height = len(event['players']) if 'players' in event else 0,
text = ('no one' if len(event['players']) == 0 else ', '.join(event['players'])) if event['status'] == 'online' else event['status']
)
def get_lines(start,stop):
lines = []
find_start = compile(r'x1="([0-9.]+)"')
find_stop = compile(r'x2="([0-9.]+)"')
with open('events.log', 'r') as fin:
for l in fin:
evstart = float(find_start.search(l).group(1))
evstop = float(find_stop.search(l).group(1))
if evstop > start and evstart < stop:
lines.append(l)
with open('current-state.json', 'r') as fin:
lines.append(make_line(loads(fin.read())))
return lines
def make_html(name,period):
stop = scale*datetime.now().timestamp()
start = stop-period
lines = get_lines(start, stop)
display_scale = 24*60/10 # 1 pixel per 10 minutes
with open('template.html', 'r') as fin:
with open(name+'.html', 'w') as fout:
fout.write(fin.read().format(
width = display_scale*period,
start = start,
period = period,
day_start = scale*datetime.fromtimestamp(start/scale).replace(hour=0,minute=0,second=0,microsecond=0).timestamp(),
day_stop = stop,
events = ''.join(lines)
))
def make_htmls():
make_html('day', 1)
make_html('month', 31)
make_html('4months', 120)
make_htmls()