Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

message.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, 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. return f"{self.sender_name}({dt_str}) : {self.content}"
  43. def __hash__(self) -> int:
  44. return hash(self.item_id)
  45. @computed_field
  46. @property
  47. def item_id(self) -> str:
  48. return self._id
  49. @validator("date_time", pre=True, always=True)
  50. def parse_timestamp(cls, v):
  51. if isinstance(v, int):
  52. return datetime.fromtimestamp(v / 1000)
  53. return v