tilde.chat/gen_stats

65 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
import base64
import json
from lxml import etree
from urllib.parse import quote
WORK_DIR = "/var/www/tilde.chat"
out = {}
parser = etree.XMLParser(strip_cdata=False)
root = etree.parse("http://localhost:8081/stats", parser)
assert root.getroot().tag == "inspircdstats"
with open(f"{WORK_DIR}/blacklist", "r") as f:
BLACKLIST = f.read().splitlines()
def define(name, xps, vfilter=lambda x: x):
global out
out[name] = vfilter(root.findall(xps)[0].text)
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
define("usercount", "./general/usercount", int)
define("channelcount", "./general/channelcount", int)
schannels = root.findall("./channellist/channel")
channels = []
for schannel in schannels:
channel = {}
channel["name"] = unsanitize(schannel.find("channelname"))
channel["topic"] = unsanitize(schannel.find("./channeltopic/topictext"))
channel["usercount"] = int(schannel.find("usercount").text)
channel["webchatlink"] = "https://web.tilde.chat/?join=" + quote(channel["name"])
if (
# skip channels in the blacklist or with mode +s
"s" in schannel.find("./channelmodes").text.split()[0]
or channel["name"] in BLACKLIST
):
continue
channels.append(channel)
channels.sort(key=lambda x: x["name"].lower())
out["channels"] = channels
with open(f"{WORK_DIR}/stats.json", "w") as f:
json.dump(out, f)