artist_event.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Artist, ClientType, JSONType, Track
  6. @model
  7. class ArtistEvent(YandexMusicModel):
  8. """Класс, представляющий артиста в событии фида.
  9. Attributes:
  10. artist (:obj:`yandex_music.Artist`, optional): Артист.
  11. tracks (:obj:`list` из :obj:`yandex_music.Track`): Треки.
  12. similar_to_artists_from_history (:obj:`list` из :obj:`yandex_music.Artist`): Похожие артисты из истории.
  13. subscribed (:obj:`bool`): Подписан ли на событие.
  14. client (:obj:`yandex_music.Client`, optional): Клиент Yandex Music.
  15. """
  16. artist: Optional['Artist']
  17. tracks: List['Track']
  18. similar_to_artists_from_history: List['Artist']
  19. subscribed: Optional['bool'] = None
  20. client: Optional['ClientType'] = None
  21. def __post_init__(self) -> None:
  22. self._id_attrs = (self.artist, self.tracks, self.similar_to_artists_from_history)
  23. @classmethod
  24. def de_json(cls, data: 'JSONType', client: 'ClientType') -> Optional['ArtistEvent']:
  25. """Десериализация объекта.
  26. Args:
  27. data (:obj:`dict`): Поля и значения десериализуемого объекта.
  28. client (:obj:`yandex_music.Client`, optional): Клиент Yandex Music.
  29. Returns:
  30. :obj:`yandex_music.ArtistEvent`: Артист из события фида.
  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 Artist, Track
  36. cls_data['artist'] = Artist.de_json(data.get('artist'), client)
  37. cls_data['tracks'] = Track.de_list(data.get('tracks'), client)
  38. cls_data['similar_to_artists_from_history'] = Artist.de_list(
  39. data.get('similar_to_artists_from_history'), client
  40. )
  41. return cls(client=client, **cls_data) # type: ignore