Skip to content

Instantly share code, notes, and snippets.

@Axeltherabbit
Last active March 31, 2022 15:56
Show Gist options
  • Save Axeltherabbit/d91624739392a59d48c5e8282e7811ab to your computer and use it in GitHub Desktop.
Save Axeltherabbit/d91624739392a59d48c5e8282e7811ab to your computer and use it in GitHub Desktop.
Messy code but it creates a list with user's messages stats from a telegram group's exported json file
import json
from collections import namedtuple
class person:
def __init__(self, _id, username):
self.id = _id
self.username = username
self.msgs_len = []
def add_msg(self, msg):
m = []
def recmsg2text(raw):
if isinstance(raw, str):
m.append(raw)
if isinstance(raw, list):
for item in raw:
recmsg2text(item)
if isinstance(raw, dict):
for key in raw:
if key == "text":
m.append(raw[key])
recmsg2text(msg)
self.msgs_len.append(len(" ".join(m)))
def __hash__(self):
return self.id
def id2int(_id):
return int(_id.replace("user", "").replace("channel", ""))
def main():
people = {}
with open("result.json", "r") as f:
js = json.load(f)
for msg in js["messages"]:
if msg["type"] == "message":
_id = id2int(msg["from_id"])
if not people.get(_id, False):
p = person(_id, msg["from"])
people[_id] = p
people[_id].add_msg(msg["text"])
prsn = namedtuple("person", ["username", "messages", "average_len", "median_len"])
res = [
prsn(
person.username,
len(person.msgs_len),
int(sum(person.msgs_len) / len(person.msgs_len)),
sorted(person.msgs_len)[len(person.msgs_len) // 2],
)
for _id, person in people.items()
]
st = sorted(res, key=lambda o: o.messages)
print(*st, sep="\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment