You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from datetime import datetime
  2. from typing import Any, List
  3. from uuid import uuid4
  4. from pydantic import BaseModel, Field, PrivateAttr, computed_field, field_validator
  5. class Reaction(BaseModel):
  6. reaction: str
  7. actor: str
  8. class AudioFile(BaseModel):
  9. uri: str
  10. creation_timestamp: int
  11. class Video(BaseModel):
  12. uri: str
  13. creation_timestamp: int
  14. class Photo(BaseModel):
  15. uri: str
  16. creation_timestamp: int
  17. class Gif(BaseModel):
  18. uri: str
  19. class Share(BaseModel):
  20. link: str
  21. share_text: str
  22. class Sticker(BaseModel):
  23. uri: str
  24. ai_stickers: List[Any]
  25. class Message(BaseModel):
  26. sender_name: str
  27. date_time: datetime = Field(alias="timestamp_ms")
  28. content: str | None = None
  29. sticker: Sticker | None = None
  30. share: Share | None = None
  31. photos: List[Photo] | None = None
  32. videos: List[Video] | None = None
  33. gifs: List[Gif] | None = None
  34. audio_files: List[AudioFile] | None = None
  35. call_duration: int | None = None
  36. reactions: List[Reaction] | None = None
  37. is_unsent: bool | None = None
  38. is_geoblocked_for_viewer: bool
  39. _id: str = PrivateAttr(default_factory=lambda: str(uuid4()))
  40. def __str__(self) -> str:
  41. dt_str = self.date_time.strftime("%d/%m/%Y, %H:%M:%S")
  42. msg_str = f"{self.sender_name}({dt_str})"
  43. if self.content:
  44. msg_str += " : " + self.content
  45. if self.photos:
  46. msg_str += f" [PHOTOS {len(self.photos)}]"
  47. if self.videos:
  48. msg_str += f" [VIDEOS {len(self.videos)}]"
  49. if self.gifs:
  50. msg_str += f" [GIFS {len(self.gifs)}]"
  51. return msg_str
  52. def __hash__(self) -> int:
  53. return hash(self.item_id)
  54. @computed_field
  55. @property
  56. def item_id(self) -> str:
  57. return self._id
  58. @field_validator("date_time")
  59. def parse_timestamp(cls, v):
  60. if isinstance(v, int):
  61. return datetime.fromtimestamp(v / 1000)
  62. return v