check_numberdealers/check_numberdealers.py

123 lines
3.1 KiB
Python
Raw Normal View History

2023-10-27 14:18:51 +02:00
#!/usr/bin/env python3
import sys
import re
import json
2023-11-08 20:48:58 +01:00
try:
USERS = __import__("users").users()
except ImportError:
USERS = None
2023-10-27 14:18:51 +02:00
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
2023-11-08 20:48:58 +01:00
if "user_id" in line and USERS is not None:
line["username"] = USERS.get(line["user_id"])
if "username" in line:
line["mention"] = f" (@{line['username']})"
else:
line["mention"] = ""
2023-10-27 14:18:51 +02:00
if "message" in line and line["message"] != "":
message = line["message"]
2023-12-13 20:11:36 +01:00
message = re.sub(r"^[#>]* ?|[*_`]*", "", message)
2023-10-27 14:18:51 +02:00
for emoji, numb in NUMBER_EMOJI.items():
2023-11-08 20:48:58 +01:00
message = re.sub(f" *:{emoji}: *", numb, message)
2023-12-13 20:11:36 +01:00
message = re.sub(" ?:(?:green)?num([0-9]+): ?", lambda m: m.group(1), message)
2023-11-07 18:59:08 +01:00
message = message.replace("\ufe0f", "").replace("\u20e3", "")
2023-10-27 14:18:51 +02:00
message = message.strip()
else:
try:
message = line["metadata"]["files"][0]["name"].split(".")[0]
except (KeyError, IndexError):
messages.append(
2023-11-08 20:48:58 +01:00
f"- [Unrecognized post]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
continue
if line.get("edit_at") is not None:
messages.append(
2023-11-08 20:48:58 +01:00
f"- Edited message [{message}]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
m = re.fullmatch(r"-?[1-9][0-9]*|0", message)
if not m:
messages.append(
2023-11-08 20:48:58 +01:00
f"- Non-number message [{message}]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
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:
2023-11-08 20:48:58 +01:00
if number == second_last_number + 2 and last_number != second_last_number + 1:
messages.pop()
2023-10-27 14:18:51 +02:00
messages.append(
2023-11-08 20:48:58 +01:00
f"- [{last_number}]({URL_PREFIX}{last_line['id']}) should have been {number - 1}{last_line['mention']}"
2023-10-27 14:18:51 +02:00
)
2023-11-08 20:48:58 +01:00
elif number == last_number:
2023-10-27 14:18:51 +02:00
messages.append(
2023-11-08 20:48:58 +01:00
f"- Duplicate [{number}]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
2023-11-08 20:48:58 +01:00
elif number == second_last_number + 1 and last_number != second_last_number + 1:
2023-10-27 14:18:51 +02:00
messages.pop()
messages.append(
2023-11-08 20:48:58 +01:00
f"- Stray [{last_number}]({URL_PREFIX}{last_line['id']}){last_line['mention']}"
2023-10-27 14:18:51 +02:00
)
elif number == last_number + 2:
messages.append(
2023-11-08 20:48:58 +01:00
f"- Skipped [{last_number + 1}]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
else:
messages.append(
f"- Going from [{last_number}]({URL_PREFIX}{last_line['id']}) "
2023-11-08 20:48:58 +01:00
f"to [{number}]({URL_PREFIX}{line['id']}){line['mention']}"
2023-10-27 14:18:51 +02:00
)
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! 🎉")