Initial commit

This commit is contained in:
Midgard 2023-10-27 14:18:51 +02:00
commit cf37ccb7e3
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 124 additions and 0 deletions

17
README.md Normal file
View file

@ -0,0 +1,17 @@
# check_numberdealers
Verify that no errors have been made in the counting in ~numberdealers and ~numberdealers-ng
When errors are detected, a nice Markdown list detailing them is produced.
## Dependencies
- Python
- [mmcli](https://git.zeus.gent/midgard/mmcli)
## Usage
```
$ mmcli cat zeus/numberdealers | ./check_numberdealers.py # Fetching this whole channel is gonna take half a minute
$ mmcli cat zeus/numberdealers-ng | ./check_numberdealers.py
```
See mmcli documentation for how to provide your credentials.

107
check_numberdealers.py Executable file
View file

@ -0,0 +1,107 @@
#!/usr/bin/env python3
import sys
import re
import json
NUMBER_EMOJI = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
URL_PREFIX = "https://mattermost.zeus.gent/zeus/pl/"
second_last_number = None
second_last_line = {"id": None}
last_number = None
last_line = {"id": None}
messages = []
start_number = None
for line in sys.stdin:
line = json.loads(line)
# Ignore non-message posts (e.g. join/leave)
if line.get("type") is not None:
continue
if "message" in line and line["message"] != "":
message = line["message"]
message = re.sub(r"^[#>]* *|[*_`]*", "", message)
for emoji, numb in NUMBER_EMOJI.items():
message = re.sub(f":{emoji}: *", numb, message)
message = message.strip()
else:
try:
message = line["metadata"]["files"][0]["name"].split(".")[0]
except (KeyError, IndexError):
messages.append(
f"- [Unrecognized post]({URL_PREFIX}{line['id']})"
)
continue
if line.get("edit_at") is not None:
messages.append(
f"- Edited message [{message}]({URL_PREFIX}{line['id']})"
)
m = re.fullmatch(r"-?[1-9][0-9]*|0", message)
if not m:
messages.append(
f"- Non-number message [{message}]({URL_PREFIX}{line['id']})"
)
else:
number = int(m.group(0))
if last_number is None:
start_number = number
last_number = number - 1
second_last_number = number - 2
if number != last_number + 1:
if number == last_number:
messages.append(
f"- Duplicate [{number}]({URL_PREFIX}{line['id']})"
)
elif number == second_last_number + 1 and last_number != second_last_number + 1:
messages.pop()
messages.append(
f"- Stray [{last_number}]({URL_PREFIX}{last_line['id']})"
)
elif number == second_last_number + 2 and last_number != second_last_number + 1:
messages.pop()
messages.append(
f"- [{last_number}]({URL_PREFIX}{last_line['id']}) should have been {number - 1}"
)
elif number == last_number + 2:
messages.append(
f"- Skipped [{last_number + 1}]({URL_PREFIX}{line['id']})"
)
else:
messages.append(
f"- Going from [{last_number}]({URL_PREFIX}{last_line['id']}) "
f"to [{number}]({URL_PREFIX}{line['id']})"
)
second_last_number = last_number
second_last_line = last_line
last_number = number
last_line = line
if start_number is None and messages == []:
print("No input data")
else:
if start_number is None:
print("No valid number messages!")
else:
print(f"Checked from {start_number} up to {number}")
if messages:
print("Errors:")
print("\n".join(messages))
else:
print("No errors! 🎉")