24 lines
631 B
Python
24 lines
631 B
Python
from datetime import datetime, timedelta, timezone
|
|
from message_parse import Message
|
|
import re
|
|
|
|
|
|
def filter_message_list(message_list: list[Message], window: timedelta):
|
|
now = datetime.now(timezone.utc)
|
|
return [m for m in message_list if now - m.date <= window]
|
|
|
|
|
|
def parse_window(s: str) -> int:
|
|
pattern = r"(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$"
|
|
match = re.fullmatch(pattern, s.strip())
|
|
|
|
if not match:
|
|
raise ValueError(f"Invalid duration: {s}")
|
|
|
|
h, m, sec = match.groups()
|
|
|
|
return (
|
|
(int(h) * 3600 if h else 0)
|
|
+ (int(m) * 60 if m else 0)
|
|
+ (int(sec) if sec else 0)
|
|
)
|