選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

message.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from datetime import datetime
  2. from math import floor
  3. from typing import Any, List, Optional
  4. from pydantic import BaseModel
  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. timestamp_ms: int
  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. def __str__(self) -> str:
  40. dt = datetime.fromtimestamp(self.timestamp_ms / 1000)
  41. dt_str = dt.strftime("%d/%m/%Y, %H:%M:%S")
  42. return f"{self.sender_name}({dt_str}) : {self.content}"
  43. def get_counted_value(self):
  44. """
  45. The content of the message should be (or contain) a number
  46. """
  47. value = None
  48. # Remove any number that is not a digit
  49. # TODO parse potential math expressions in content
  50. cleaned_content = ''.join([c for c in self.content if c.isdigit()])
  51. try:
  52. value = floor(float(cleaned_content))
  53. except Exception as e:
  54. raise ValueError(
  55. f"Message {cleaned_content} does not contain a number ({e})")
  56. return value