pancakecounter/table.py

63 lines
1.7 KiB
Python
Raw Normal View History

2020-12-06 01:53:40 +01:00
#!/usr/bin/env python3
import sys
import re
from datetime import datetime
from collections import defaultdict
2020-12-09 15:09:39 +01:00
from typing import Dict
2020-12-06 01:53:40 +01:00
if sys.stdin.isatty():
2020-12-09 15:09:39 +01:00
print("Hint: stdin is a terminal, "
"you may want to do `./make_table.py < verifications.log` instead.", file=sys.stderr)
2020-12-06 01:53:40 +01:00
2020-12-09 15:07:31 +01:00
# users[awardee][post_id][verifier]: score
2020-12-09 15:09:39 +01:00
users: Dict[str, Dict[str, Dict[str, int]]] = \
2020-12-09 15:07:31 +01:00
defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: 0)))
2020-12-06 01:53:40 +01:00
for line_nr, line in enumerate(sys.stdin, start=1):
line = line.rstrip()
2020-12-09 15:07:31 +01:00
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) verified with score ([^ ]+)", line)
2020-12-06 01:53:40 +01:00
if m:
awardee = m.group(1)
post_id = m.group(2)
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
2020-12-09 15:07:31 +01:00
score = int(m.group(5))
2020-12-06 01:53:40 +01:00
2020-12-09 15:07:31 +01:00
users[awardee][post_id][verifier] = score
2020-12-06 01:53:40 +01:00
continue
2020-12-09 15:09:39 +01:00
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) updated their verification's score "
"from ([^ ]+) to ([^ ]+)", line)
2020-12-06 01:53:40 +01:00
if m:
awardee = m.group(1)
post_id = m.group(2)
2020-12-09 15:07:31 +01:00
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
prev_score = int(m.group(5))
score = int(m.group(6))
2020-12-06 01:53:40 +01:00
2020-12-09 15:07:31 +01:00
users[awardee][post_id][verifier] = score
continue
2020-12-09 15:07:31 +01:00
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) retracted their verification", line)
if m:
awardee = m.group(1)
post_id = m.group(2)
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
2020-12-06 01:53:40 +01:00
2020-12-09 15:07:31 +01:00
users[awardee][post_id][verifier] = 0
2020-12-06 01:53:40 +01:00
continue
print(f"Couldn't parse line {line_nr}, looks like the file is corrupt: {line}", file=sys.stderr)
2020-12-06 02:08:50 +01:00
2020-12-06 01:53:40 +01:00
for username, user_posts in users.items():
2020-12-09 15:07:31 +01:00
score = sum(
max(post_verifications.values(), default=0)
for post_verifications in user_posts.values()
)
if score > 0:
print(f"{username}: {score}")