from datetime import datetime from math import floor from typing import Any, List, Optional from pydantic import BaseModel class Reaction(BaseModel): reaction: str actor: str class AudioFile(BaseModel): uri: str creation_timestamp: int class Video(BaseModel): uri: str creation_timestamp: int class Photo(BaseModel): uri: str creation_timestamp: int class Gif(BaseModel): uri: str class Share(BaseModel): link: str share_text: str class Sticker(BaseModel): uri: str ai_stickers: List[Any] class Message(BaseModel): sender_name: str timestamp_ms: int content: str | None = None sticker: Sticker | None = None share: Share | None = None photos: List[Photo] | None = None videos: List[Video] | None = None gifs: List[Gif] | None = None audio_files: List[AudioFile] | None = None call_duration: int | None = None reactions: List[Reaction] | None = None is_unsent: bool | None = None is_geoblocked_for_viewer: bool def __str__(self) -> str: dt = datetime.fromtimestamp(self.timestamp_ms / 1000) dt_str = dt.strftime("%d/%m/%Y, %H:%M:%S") return f"{self.sender_name}({dt_str}) : {self.content}" def get_counted_value(self): """ The content of the message should be (or contain) a number """ value = None # Remove any number that is not a digit # TODO parse potential math expressions in content cleaned_content = ''.join([c for c in self.content if c.isdigit()]) try: value = floor(float(cleaned_content)) except Exception as e: raise ValueError( f"Message {cleaned_content} does not contain a number ({e})") return value