minecraft-monitor/generate-svg.py

83 lines
2.0 KiB
Python

from json import loads
from xml.etree.ElementTree import Element, tostring
from datetime import datetime
def readEvents():
events = []
with open('events.json', 'r') as f:
for line in f:
events.append(loads(line))
with open('current-state.json', 'r') as f:
events.append(loads(f.read()))
return events
def getSVG(events):
scale = 1/60/60/24
period = 3*30 *60*60*24
end = datetime.now().timestamp()
start = end-period
# start = events[0]['period'][0]
# end = events[-1]['period'][1]
root = Element('svg', {
'version': '1.1',
'xmlns': 'http://www.w3.org/2000/svg',
'viewBox': '0 0 {} 200'.format(scale*period*200),
'width': str(scale*period*200),
'height': '200',
})
group = Element('svg', {
'viewBox': '{} 0 {} 10'.format(scale*start,scale*period),
'width': '100%',
'height': '100%',
'preserveAspectRatio': 'none'
})
for i in range(0,10+1):
num = Element('text', {
'x': str(scale*period*200-20),
'y': str((1-i/10)*200)
})
num.text = str(i)
root.append(num)
root.append(group)
for event in events:
evstart = event['period'][0]
evend = event['period'][1]
if evend < start or evstart > end:
continue
props = { 'x1': str(scale*evstart), 'x2': str(scale*evend), 'y1': '0', 'y2': '0' }
titletext = 'unknown'
if 'players' in event:
y = str(len(event['players']))
props['y1'] = y
props['y2'] = y
if len(event['players']) == 0:
titletext = '(no one)'
else:
titletext = ', '.join(event['players'])
if event['type'] == 'error':
props['class'] = 'error'
titletext = 'error'
ev = Element('line', props)
title = Element('title')
title.text = '{} ({}{})'.format(titletext, datetime.fromtimestamp(evstart).strftime('%Y-%m-%d %H:%M'), datetime.fromtimestamp(evend).strftime('%Y-%m-%d %H:%M'))
ev.append(title)
group.append(ev)
return root
def dumpHTML(arg):
with open('index.html', 'r') as f:
html = f.read()
print( html.format(arg) )
dumpHTML(tostring(getSVG(readEvents()), encoding='unicode'))