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 Watch
-------- --------
.. automethod:: YTMusic.get_watch_playlist .. automethod:: YTMusic.get_watch_playlist
.. automethod:: YTMusic.get_watch_playlist_shuffle
Library Library
------- -------

View file

@ -207,21 +207,17 @@ class TestYTMusic(unittest.TestCase):
############### ###############
def test_get_watch_playlist(self): def test_get_watch_playlist(self):
playlist = self.yt_auth.get_watch_playlist( playlist = self.yt_auth.get_watch_playlist(playlistId="RDAMPLOLAK5uy_l_fKDQGOUsk8kbWsm9s86n4-nZNd2JR8Q",
playlistId="OLAK5uy_ln_o1YXFqK4nfiNuTfhJK2XcRNCxml0fY", limit=90) radio=True, limit=90)
self.assertGreaterEqual(len(playlist['tracks']), 90) self.assertGreaterEqual(len(playlist['tracks']), 90)
playlist = self.yt_auth.get_watch_playlist("9mWr4c_ig54", limit=50) playlist = self.yt_auth.get_watch_playlist("9mWr4c_ig54", limit=50)
self.assertGreater(len(playlist['tracks']), 45) self.assertGreater(len(playlist['tracks']), 45)
playlist = self.yt_auth.get_watch_playlist("UoAf_y9Ok4k") # private track playlist = self.yt_auth.get_watch_playlist("UoAf_y9Ok4k") # private track
self.assertGreaterEqual(len(playlist['tracks']), 25) self.assertGreaterEqual(len(playlist['tracks']), 25)
playlist = self.yt.get_watch_playlist(
def test_get_watch_playlist_shuffle(self): playlistId="OLAK5uy_lKgoGvlrWhX0EIPavQUXxyPed8Cj38AWc", shuffle=True)
playlist = self.yt.get_watch_playlist_shuffle(
playlistId="OLAK5uy_lKgoGvlrWhX0EIPavQUXxyPed8Cj38AWc")
self.assertEqual(len(playlist['tracks']), 12) self.assertEqual(len(playlist['tracks']), 12)
playlist = self.yt_brand.get_watch_playlist(playlistId=config['playlists']['own'], shuffle=True)
def test_get_watch_playlist_shuffle_playlist(self):
playlist = self.yt_brand.get_watch_playlist_shuffle(playlistId=config['playlists']['own'])
self.assertEqual(len(playlist['tracks']), 4) self.assertEqual(len(playlist['tracks']), 4)
################ ################

View file

@ -10,7 +10,8 @@ class WatchMixin:
videoId: str = None, videoId: str = None,
playlistId: str = None, playlistId: str = None,
limit=25, 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 Get a watch list of tracks. This watch playlist appears when you press
play on a track in YouTube Music. play on a track in YouTube Music.
@ -22,7 +23,9 @@ class WatchMixin:
:param videoId: videoId of the played video :param videoId: videoId of the played video
:param playlistId: playlistId of the played playlist or album :param playlistId: playlistId of the played playlist or album
:param limit: minimum number of watch playlist items to return :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 :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 appears if a song has a corresponding video counterpart (UI song/video
switcher). switcher).
@ -108,7 +111,7 @@ class WatchMixin:
body['videoId'] = videoId body['videoId'] = videoId
if not playlistId: if not playlistId:
playlistId = "RDAMVM" + videoId playlistId = "RDAMVM" + videoId
if not params: if not (radio or shuffle):
body['watchEndpointMusicSupportedConfigs'] = { body['watchEndpointMusicSupportedConfigs'] = {
'watchEndpointMusicConfig': { 'watchEndpointMusicConfig': {
'hasPersistentPlaylistPanel': True, 'hasPersistentPlaylistPanel': True,
@ -118,8 +121,10 @@ class WatchMixin:
body['playlistId'] = validate_playlist_id(playlistId) body['playlistId'] = validate_playlist_id(playlistId)
is_playlist = body['playlistId'].startswith('PL') or \ is_playlist = body['playlistId'].startswith('PL') or \
body['playlistId'].startswith('OLA') body['playlistId'].startswith('OLA')
if params: if shuffle and playlistId is not None:
body['params'] = params body['params'] = "wAEB8gECKAE%3D"
if radio:
body['params'] = "wAEB"
endpoint = 'next' endpoint = 'next'
response = self._send_request(endpoint, body) response = self._send_request(endpoint, body)
watchNextRenderer = nav(response, [ watchNextRenderer = nav(response, [
@ -152,20 +157,3 @@ class WatchMixin:
playlistId=playlist, playlistId=playlist,
lyrics=lyrics_browse_id, lyrics=lyrics_browse_id,
related=related_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')