tilde.chat/gen_stats

62 lines
1.7 KiB
Plaintext
Raw Normal View History

2019-03-16 18:53:20 +00:00
#!/usr/bin/env python3
import base64
import json
from lxml import etree
2018-06-20 18:54:31 +00:00
2019-03-16 18:59:02 +00:00
WORK_DIR = "/var/www/tilde.chat"
2018-06-20 18:54:31 +00:00
out = {}
parser = etree.XMLParser(strip_cdata=False)
root = etree.parse("http://localhost:8081/stats", parser)
assert root.getroot().tag == "inspircdstats"
2018-10-15 22:29:34 +00:00
2019-03-16 18:59:02 +00:00
with open(f"{WORK_DIR}/blacklist", "r") as f:
2019-03-16 18:53:20 +00:00
BLACKLIST = f.read().splitlines()
2018-10-15 22:29:34 +00:00
2019-03-16 19:26:15 +00:00
def unsanitize(node, default=""):
# workaround for weird behavior in insp's xml output
# https://github.com/inspircd/inspircd/blob/v3.7.0/src/modules/m_httpd_stats.cpp#L55
if node.text is None or node.text == "":
return default
elif str(etree.tostring(node)).startswith(f"b'<{node.tag}><![CDATA["):
missing_padding = len(node.text) % 4
if missing_padding:
v = node.text + "=" * (4 - missing_padding)
else:
v = node.text
return base64.b64decode(v).decode("utf-8")
return node.text
2018-10-15 22:29:34 +00:00
2020-09-22 18:01:58 +00:00
out["usercount"] = int(root.find("./general/usercount").text)
out["channelcount"] = int(root.find("./general/channelcount").text)
2019-03-16 18:53:20 +00:00
2020-09-22 18:01:58 +00:00
out["channels"] = []
for c in root.findall("./channellist/channel"):
name = unsanitize(c.find("channelname"))
2019-03-16 19:26:15 +00:00
2020-10-23 21:38:27 +00:00
modes = c.find("./channelmodes").text
2018-10-15 22:29:34 +00:00
if (
2019-03-16 19:26:15 +00:00
# skip channels in the blacklist or with mode +s
2020-10-23 21:38:27 +00:00
name in BLACKLIST
or modes is None
or "s" in modes.split()[0]
2018-10-15 22:29:34 +00:00
):
continue
2019-03-16 19:26:15 +00:00
2020-09-22 18:01:58 +00:00
out["channels"].append(
dict(
name=name,
topic=unsanitize(c.find("./channeltopic/topictext"), "no topic set"),
usercount=int(c.find("usercount").text),
2020-11-18 00:58:11 +00:00
webchatlink="https://kiwi.tilde.chat/" + name,
2020-09-22 18:01:58 +00:00
)
)
out["channels"].sort(key=lambda x: x["name"].lower())
2019-03-16 18:53:20 +00:00
2019-03-16 18:59:02 +00:00
with open(f"{WORK_DIR}/stats.json", "w") as f:
2018-10-15 22:29:34 +00:00
json.dump(out, f)