Commit 9274f2be authored by Romain Courteaud's avatar Romain Courteaud

Bot: add HTML output

parent a37def25
......@@ -54,6 +54,7 @@ import datetime
from email.utils import parsedate_to_datetime
import tempfile
import os
import html
__version__ = "0.8.0"
......@@ -67,6 +68,82 @@ def rfc822(date):
return email.utils.format_datetime(date)
def _(string_to_escape):
return html.escape("%s" % string_to_escape, quote=False)
def __(string_to_escape):
return html.escape("%s" % string_to_escape, quote=True)
def dumpStatusDictToHTML(status_dict):
body_result = "<main>"
status_list = []
append_status = status_list.append
for table_key in status_dict:
table = status_dict[table_key]
if table:
append_status(
"<table><caption>%s</caption><thead><tr>" % _(table_key)
)
# Headers
table_key_list = [x for x in table[0].keys()]
table_key_list.sort()
for table_key in table_key_list:
append_status('<th scope="col">%s</th>' % _(table_key))
append_status("</tr></thead><tbody>")
# Status
for line in table:
append_status("<tr>")
for table_key in table_key_list:
if table_key == "url":
append_status(
'<td><a href="%s">%s</a></td>'
% (__(line[table_key]), _(line[table_key]))
)
elif table_key in ("domain", "hostname"):
append_status(
'<td><a href="https://%s">%s</a></td>'
% (__(line[table_key]), _(line[table_key]))
)
else:
append_status("<td>%s</td>" % _(line[table_key]))
append_status("</tr>")
append_status("</tbody></table>")
body_result += "\n".join(status_list)
body_result += "</main>"
result = (
"<!DOCTYPE html><html><head>"
'<meta name="viewport"\n'
'content="width=device-width,height=device-height,'
'initial-scale=1" />'
"<title>Surykatka</title>"
"<style>"
"html {display: flex; justify-content: center;}\n"
"body {width: 100%%; padding: 0 1em;"
" word-break: break-all; display: flex;"
" min-height: 100vh; flex-direction: column;}\n"
"main {flex: 1;}\n"
"html, body {height: 100%%; margin: 0;}\n"
"table {border: 2px solid black; border-collapse: collapse;}\n"
"caption {font-weight: bold;}\n"
"th, td {border: 1px solid black; padding: 0 .5em}\n"
"</style>\n"
"</head><body>%(body)s</body></html>"
% {
"body": body_result,
}
)
return result
def filterRecentStatus(status_dict, warning_period_duration):
now = datetime.datetime.utcnow()
......@@ -934,6 +1011,8 @@ class WebBot:
)
if self.config["FORMAT"] == "json":
status_output = json.dumps(status_dict)
if self.config["FORMAT"] == "html":
status_output = dumpStatusDictToHTML(status_dict)
else:
status_list = []
append_status = status_list.append
......
......@@ -57,7 +57,7 @@ from .bot import create_bot
"--output",
"-o",
help="The status output format.",
type=click.Choice(["plain", "json"]),
type=click.Choice(["plain", "json", "html"]),
default="plain",
show_default=True,
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment