From 2830cd000d106982c4019010108bd9ee17431fae Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 22 Sep 2020 14:01:58 -0400 Subject: [PATCH] tidy up script --- gen_stats | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/gen_stats b/gen_stats index 3c3072f..eda2ceb 100755 --- a/gen_stats +++ b/gen_stats @@ -15,11 +15,6 @@ 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 @@ -36,29 +31,31 @@ def unsanitize(node, default=""): return node.text -define("usercount", "./general/usercount", int) -define("channelcount", "./general/channelcount", int) -schannels = root.findall("./channellist/channel") +out["usercount"] = int(root.find("./general/usercount").text) +out["channelcount"] = int(root.find("./general/channelcount").text) -channels = [] -for schannel in schannels: - channel = {} - channel["name"] = unsanitize(schannel.find("channelname")) - channel["topic"] = unsanitize(schannel.find("./channeltopic/topictext"), "no topic set") - channel["usercount"] = int(schannel.find("usercount").text) - channel["webchatlink"] = "https://web.tilde.chat/?join=" + quote(channel["name"]) +out["channels"] = [] +for c in root.findall("./channellist/channel"): + name = unsanitize(c.find("channelname")) if ( # skip channels in the blacklist or with mode +s - "s" in schannel.find("./channelmodes").text.split()[0] - or channel["name"] in BLACKLIST + "s" in c.find("./channelmodes").text.split()[0] + or name in BLACKLIST ): continue - channels.append(channel) + out["channels"].append( + dict( + name=name, + topic=unsanitize(c.find("./channeltopic/topictext"), "no topic set"), + usercount=int(c.find("usercount").text), + webchatlink="https://web.tilde.chat/?join=" + quote(name), + ) + ) -channels.sort(key=lambda x: x["name"].lower()) -out["channels"] = channels + +out["channels"].sort(key=lambda x: x["name"].lower()) with open(f"{WORK_DIR}/stats.json", "w") as f: json.dump(out, f)