Um svg-badge ausgabe erweitert, steuerbar mit GET-Parameter ?format=svg|text

This commit is contained in:
Reik Kaps 2020-06-19 17:52:00 +02:00
parent 0c408b53c7
commit 46f6df8e2f
1 changed files with 56 additions and 16 deletions

View File

@ -1,5 +1,8 @@
#!/bin/env python3 #!/bin/env python3
# #
# simple log parser and status server for the jitsi meet video conference tool
#
# Reik Kaps <r31k@k4p5.de> 2020
import re import re
import sys import sys
@ -9,10 +12,11 @@ from os.path import basename
from datetime import datetime from datetime import datetime
from argparse import ArgumentParser from argparse import ArgumentParser
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse from urllib.parse import urlparse, parse_qs
try: try:
from sh import tail from sh import tail
from pybadges import badge
except ImportError as e: except ImportError as e:
print('[Error] Please install sh via pip! {}'.format(e)) print('[Error] Please install sh via pip! {}'.format(e))
@ -75,7 +79,6 @@ class MeetingRoom():
def __str__(self): def __str__(self):
return '{}: {}'.format(self.jid, self.owner) return '{}: {}'.format(self.jid, self.owner)
class logThread(threading.Thread): class logThread(threading.Thread):
def __init__(self, threadID, name, filename): def __init__(self, threadID, name, filename):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -90,22 +93,59 @@ class logThread(threading.Thread):
class MyServer(BaseHTTPRequestHandler): class MyServer(BaseHTTPRequestHandler):
def badge_respones(self, response_string):
self.send_response(200)
self.send_header("Content-type", "image/svg+xml")
self.end_headers()
self.wfile.write(response_string)
def text_response(self, response_string):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(response_string)
def do_GET(self): def do_GET(self):
if basename(self.path) in roomList: query_components = parse_qs(urlparse(self.path).query)
self.send_response(200) if "format" in query_components:
self.send_header("Content-type", "text/plain") respones_format = query_components["format"][0]
self.end_headers()
response_string = bytes(basename(self.path)
+ '|'
+ str(roomList[basename(self.path)].ctime)
+ '|'
+ str(len(roomList[basename(self.path)].members))
+ '|', "utf-8")
self.wfile.write(response_string)
else: else:
self.send_response(404) respones_format = 'text'
self.send_header("Content-type", "text/plain")
self.end_headers() room_name = basename(self.path.split('?')[0])
print('{} - {}'.format(query_components, room_name))
if room_name in roomList:
if respones_format == 'json':
pass
elif respones_format == 'svg':
s = badge(left_text='{}: {}'.format('Room', room_name),
right_text=str(len(roomList[room_name].members)),
left_color='blue',
right_color='green')
response_string = bytes(s, "utf-8")
self.badge_respones(response_string)
else:
# default text response
response_string = bytes(room_name
+ '|'
+ str(roomList[room_name].ctime)
+ '|'
+ str(len(roomList[room_name].members))
+ '|', "utf-8")
self.text_response(response_string)
else:
if respones_format == 'svg':
s = badge(left_text='{}: {}'.format('Room', room_name),
right_text='empty',
left_color='red',
right_color='darkgray')
response_string = bytes(s, "utf-8")
self.badge_respones(response_string)
else:
self.send_response(404)
# self.send_header("Content-type", "text/plain")
self.end_headers()
def getRoomName(jid): def getRoomName(jid):