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
This commit is contained in:
sigma67 2022-10-01 15:36:22 +02:00
commit 527fed67ff
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
3 changed files with 15 additions and 32 deletions

View file

@ -43,7 +43,6 @@ Explore
Watch
--------
.. automethod:: YTMusic.get_watch_playlist
.. automethod:: YTMusic.get_watch_playlist_shuffle
Library
-------

View file

@ -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)
################

View file

@ -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')