From 09baceab4bf6bdb6a274edbe8127ab8a7d03353d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 19 Sep 2022 20:39:21 +0200 Subject: [PATCH] Include album_id and the list of tracks in music.tidal.get_album --- platypush/plugins/music/tidal/__init__.py | 2 +- platypush/schemas/tidal.py | 27 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/music/tidal/__init__.py b/platypush/plugins/music/tidal/__init__.py index 14230578e2..484b1c594e 100644 --- a/platypush/plugins/music/tidal/__init__.py +++ b/platypush/plugins/music/tidal/__init__.py @@ -207,7 +207,7 @@ class MusicTidalPlugin(RunnablePlugin): :return: .. schema:: tidal.TidalAlbumSchema """ ret = self.session.album(album_id) - return TidalAlbumSchema().dump(ret) + return TidalAlbumSchema(with_tracks=True).dump(ret) @action def get_track(self, track_id: Union[str, int]): diff --git a/platypush/schemas/tidal.py b/platypush/schemas/tidal.py index d7df81b93e..4dde5fcae2 100644 --- a/platypush/schemas/tidal.py +++ b/platypush/schemas/tidal.py @@ -1,4 +1,4 @@ -from marshmallow import Schema, fields, pre_dump +from marshmallow import Schema, fields, pre_dump, post_dump from platypush.schemas import DateTime @@ -35,10 +35,13 @@ class TidalArtistSchema(TidalSchema): class TidalAlbumSchema(TidalSchema): + def __init__(self, *args, with_tracks=False, **kwargs): + super().__init__(*args, **kwargs) + self._with_tracks = with_tracks + id = fields.String( required=True, dump_only=True, - attribute='uuid', metadata={ 'example': '45288612', 'description': 'Album ID', @@ -59,12 +62,32 @@ class TidalAlbumSchema(TidalSchema): duration = fields.Int(metadata={'description': 'Album duration, in seconds'}) year = fields.Integer(metadata={'example': 2003}) num_tracks = fields.Int(metadata={'example': 10}) + tracks = fields.List(fields.Dict(), attribute='_tracks') @pre_dump def _prefill_url(self, data, *_, **__): data.url = f'https://tidal.com/album/{data.id}' return data + @pre_dump + def _cache_tracks(self, data, *_, **__): + if self._with_tracks: + album_id = str(data.id) + self.context[album_id] = { + 'tracks': data.tracks(), + } + + return data + + @post_dump + def _dump_tracks(self, data, *_, **__): + if self._with_tracks: + album_id = str(data['id']) + ctx = self.context.pop(album_id, {}) + data['tracks'] = TidalTrackSchema().dump(ctx.pop('tracks', []), many=True) + + return data + class TidalTrackSchema(TidalSchema): id = fields.String(