tilde.chat/gen_stats

55 lines
1.6 KiB
Plaintext
Raw Normal View History

2019-03-16 18:53:20 +00:00
#!/usr/bin/env python3
2018-06-20 18:54:31 +00:00
import requests, json
import xml.etree.ElementTree as xml
2019-03-16 18:59:02 +00:00
WORK_DIR = "/var/www/tilde.chat"
2018-06-20 18:54:31 +00:00
r = requests.get("http://localhost:8081/stats")
r.raise_for_status()
out = {}
d = xml.fromstring(r.text)
assert d.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
def define(name, xps, vfilter=lambda x: x):
global out
out[name] = vfilter(d.findall(xps)[0].text)
define("usercount", "./general/usercount", int)
define("channelcount", "./general/channelcount", int)
2018-06-20 18:54:31 +00:00
schannels = d.findall("./channellist/channel")
2019-03-16 18:53:20 +00:00
2018-06-20 18:54:31 +00:00
channels = []
for schannel in schannels:
2018-10-15 22:29:34 +00:00
channel = dict(
name=schannel.findall("channelname")[0].text,
usercount=int(schannel.findall("usercount")[0].text),
)
channel["topic"] = (
schannel.findall("./channeltopic/topictext")[0].text
if schannel.findall("./channeltopic/topictext")[0].text is not None
else "No topic set"
)
# bugfix: if mode s is set and there's a topic, hide the topic
if (
"s" in schannel.findall("./channelmodes")[0].text.split()[0]
and channel["topic"] != "No topic set"
):
channel["topic"] = "Topic hidden"
2019-03-16 18:53:20 +00:00
if channel["name"] in BLACKLIST:
# skip channels in the blacklist
2018-10-15 22:29:34 +00:00
continue
channel["webchatlink"] = "https://web.tilde.chat/?join=" + channel["name"].lstrip(
"#"
)
channels.append(channel)
2019-03-16 18:53:20 +00:00
2018-06-20 21:31:44 +00:00
channels.sort(key=lambda x: x["name"].lower())
2018-10-15 22:29:34 +00:00
out["channels"] = channels
2019-03-16 18:53:20 +00:00
2018-10-15 22:29:34 +00:00
# print([x.text for x in d.findall("./channellist/channel/channeltopic/topictext")])
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)