day.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from typing import TYPE_CHECKING, List, Optional
  2. from yandex_music import YandexMusicModel
  3. from yandex_music.utils import model
  4. if TYPE_CHECKING:
  5. from yandex_music import ClientType, Event, JSONType, Track, TrackWithAds
  6. @model
  7. class Day(YandexMusicModel):
  8. """Класс, представляющий день в фиде.
  9. Attributes:
  10. day (:obj:`str`): Дата в формате YYYY-MM-DD.
  11. events (:obj:`list` из :obj:`yandex_music.Event`): События TODO.
  12. tracks_to_play_with_ads (:obj:`list` из :obj:`yandex_music.TrackWithAds`): Треки для проигрывания с рекламой.
  13. tracks_to_play (:obj:`list` из :obj:`yandex_music.Track`): Треки для проигрывания.
  14. client (:obj:`yandex_music.Client`, optional): Клиент Yandex Music.
  15. """
  16. day: str
  17. events: List['Event']
  18. tracks_to_play_with_ads: List['TrackWithAds']
  19. tracks_to_play: List['Track']
  20. client: Optional['ClientType'] = None
  21. def __post_init__(self) -> None:
  22. self._id_attrs = (self.day, self.events, self.tracks_to_play_with_ads, self.tracks_to_play)
  23. @classmethod
  24. def de_json(cls, data: 'JSONType', client: 'ClientType') -> Optional['Day']:
  25. """Десериализация объекта.
  26. Args:
  27. data (:obj:`dict`): Поля и значения десериализуемого объекта.
  28. client (:obj:`yandex_music.Client`, optional): Клиент Yandex Music.
  29. Returns:
  30. :obj:`yandex_music.Day`: День в фиде.
  31. """
  32. if not cls.is_dict_model_data(data):
  33. return None
  34. cls_data = cls.cleanup_data(data, client)
  35. from yandex_music import Event, Track, TrackWithAds
  36. cls_data['events'] = Event.de_list(data.get('events'), client)
  37. cls_data['tracks_to_play_with_ads'] = TrackWithAds.de_list(data.get('tracks_to_play_with_ads'), client)
  38. cls_data['tracks_to_play'] = Track.de_list(data.get('tracks_to_play'), client)
  39. return cls(client=client, **cls_data) # type: ignore