tidy up script

This commit is contained in:
Ben Harris 2020-09-22 14:01:58 -04:00
parent 034e963ade
commit 2830cd000d
1 changed files with 17 additions and 20 deletions

View File

@ -15,11 +15,6 @@ with open(f"{WORK_DIR}/blacklist", "r") as f:
BLACKLIST = f.read().splitlines() 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=""): def unsanitize(node, default=""):
# workaround for weird behavior in insp's xml output # 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 # 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 return node.text
define("usercount", "./general/usercount", int) out["usercount"] = int(root.find("./general/usercount").text)
define("channelcount", "./general/channelcount", int) out["channelcount"] = int(root.find("./general/channelcount").text)
schannels = root.findall("./channellist/channel")
channels = [] out["channels"] = []
for schannel in schannels: for c in root.findall("./channellist/channel"):
channel = {} name = unsanitize(c.find("channelname"))
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"])
if ( if (
# skip channels in the blacklist or with mode +s # skip channels in the blacklist or with mode +s
"s" in schannel.find("./channelmodes").text.split()[0] "s" in c.find("./channelmodes").text.split()[0]
or channel["name"] in BLACKLIST or name in BLACKLIST
): ):
continue 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: with open(f"{WORK_DIR}/stats.json", "w") as f:
json.dump(out, f) json.dump(out, f)