From 527fed67ffc05669de00b6ffa67c7df030976a21 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sat, 1 Oct 2022 15:36:22 +0200 Subject: [PATCH] get_watch_playlist: add shuffle and radio parameters (closes #282) removed get_watch_playlist_shuffle in favor of the new shuffle parameter to get_watch_playlist setting the radio parameter now properly randomizes the watch playlist, as discussed in #282 --- docs/source/reference.rst | 1 - tests/test.py | 14 +++++--------- ytmusicapi/mixins/watch.py | 32 ++++++++++---------------------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/docs/source/reference.rst b/docs/source/reference.rst index db3d769..981b72e 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -43,7 +43,6 @@ Explore Watch -------- .. automethod:: YTMusic.get_watch_playlist -.. automethod:: YTMusic.get_watch_playlist_shuffle Library ------- diff --git a/tests/test.py b/tests/test.py index da45b72..6fe16e3 100644 --- a/tests/test.py +++ b/tests/test.py @@ -207,21 +207,17 @@ class TestYTMusic(unittest.TestCase): ############### def test_get_watch_playlist(self): - playlist = self.yt_auth.get_watch_playlist( - playlistId="OLAK5uy_ln_o1YXFqK4nfiNuTfhJK2XcRNCxml0fY", limit=90) + playlist = self.yt_auth.get_watch_playlist(playlistId="RDAMPLOLAK5uy_l_fKDQGOUsk8kbWsm9s86n4-nZNd2JR8Q", + radio=True, limit=90) self.assertGreaterEqual(len(playlist['tracks']), 90) playlist = self.yt_auth.get_watch_playlist("9mWr4c_ig54", limit=50) self.assertGreater(len(playlist['tracks']), 45) playlist = self.yt_auth.get_watch_playlist("UoAf_y9Ok4k") # private track self.assertGreaterEqual(len(playlist['tracks']), 25) - - def test_get_watch_playlist_shuffle(self): - playlist = self.yt.get_watch_playlist_shuffle( - playlistId="OLAK5uy_lKgoGvlrWhX0EIPavQUXxyPed8Cj38AWc") + playlist = self.yt.get_watch_playlist( + playlistId="OLAK5uy_lKgoGvlrWhX0EIPavQUXxyPed8Cj38AWc", shuffle=True) self.assertEqual(len(playlist['tracks']), 12) - - def test_get_watch_playlist_shuffle_playlist(self): - playlist = self.yt_brand.get_watch_playlist_shuffle(playlistId=config['playlists']['own']) + playlist = self.yt_brand.get_watch_playlist(playlistId=config['playlists']['own'], shuffle=True) self.assertEqual(len(playlist['tracks']), 4) ################ diff --git a/ytmusicapi/mixins/watch.py b/ytmusicapi/mixins/watch.py index 6a5aa85..412c89d 100644 --- a/ytmusicapi/mixins/watch.py +++ b/ytmusicapi/mixins/watch.py @@ -10,7 +10,8 @@ class WatchMixin: videoId: str = None, playlistId: str = None, limit=25, - params: str = None) -> Dict[str, Union[List[Dict]]]: + radio: bool = False, + shuffle: bool = False) -> Dict[str, Union[List[Dict]]]: """ Get a watch list of tracks. This watch playlist appears when you press play on a track in YouTube Music. @@ -22,7 +23,9 @@ class WatchMixin: :param videoId: videoId of the played video :param playlistId: playlistId of the played playlist or album :param limit: minimum number of watch playlist items to return - :param params: only used internally by :py:func:`get_watch_playlist_shuffle` + :param radio: get a radio playlist (changes each time) + :param shuffle: shuffle the input playlist. only works when the playlistId parameter + is set at the same time. does not work if radio=True :return: List of watch playlist items. The counterpart key is optional and only appears if a song has a corresponding video counterpart (UI song/video switcher). @@ -108,7 +111,7 @@ class WatchMixin: body['videoId'] = videoId if not playlistId: playlistId = "RDAMVM" + videoId - if not params: + if not (radio or shuffle): body['watchEndpointMusicSupportedConfigs'] = { 'watchEndpointMusicConfig': { 'hasPersistentPlaylistPanel': True, @@ -118,8 +121,10 @@ class WatchMixin: body['playlistId'] = validate_playlist_id(playlistId) is_playlist = body['playlistId'].startswith('PL') or \ body['playlistId'].startswith('OLA') - if params: - body['params'] = params + if shuffle and playlistId is not None: + body['params'] = "wAEB8gECKAE%3D" + if radio: + body['params'] = "wAEB" endpoint = 'next' response = self._send_request(endpoint, body) watchNextRenderer = nav(response, [ @@ -152,20 +157,3 @@ class WatchMixin: playlistId=playlist, lyrics=lyrics_browse_id, related=related_browse_id) - - def get_watch_playlist_shuffle(self, - videoId: str = None, - playlistId: str = None, - limit=50) -> Dict[str, Union[List[Dict]]]: - """ - Shuffle any playlist - - :param videoId: Optional video id of the first video in the shuffled playlist - :param playlistId: Playlist id - :param limit: The number of watch playlist items to return - :return: A list of watch playlist items (see :py:func:`get_watch_playlist`) - """ - return self.get_watch_playlist(videoId=videoId, - playlistId=playlistId, - limit=limit, - params='wAEB8gECKAE%3D')