diff --git a/tests/test.py b/tests/test.py index e8ac277..5eb8685 100644 --- a/tests/test.py +++ b/tests/test.py @@ -255,24 +255,28 @@ class TestYTMusic(unittest.TestCase): self.assertGreater(len(playlist['tracks']), 200) def test_get_owned_playlist(self): - playlist = self.yt_auth.get_playlist(config['playlists']['own'], 300) - self.assertGreater(len(playlist['tracks']), 200) + playlist = self.yt_brand.get_playlist(config['playlists']['own']) + self.assertLess(len(playlist['tracks']), 100) + suggestions = self.yt_brand.get_playlist_suggestions(playlist['suggestions_token']) + self.assertGreater(len(suggestions['tracks']), 5) + refresh = self.yt_brand.get_playlist_suggestions(suggestions['refresh_token']) + self.assertGreater(len(refresh['tracks']), 5) def test_edit_playlist(self): - playlist = self.yt_auth.get_playlist(config['playlists']['own']) - response = self.yt_auth.edit_playlist(playlist['id'], - title='', - description='', - privacyStatus='PRIVATE', - moveItem=(playlist['tracks'][1]['setVideoId'], - playlist['tracks'][0]['setVideoId'])) + playlist = self.yt_brand.get_playlist(config['playlists']['own']) + response = self.yt_brand.edit_playlist(playlist['id'], + title='', + description='', + privacyStatus='PRIVATE', + moveItem=(playlist['tracks'][1]['setVideoId'], + playlist['tracks'][0]['setVideoId'])) self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed") - self.yt_auth.edit_playlist(playlist['id'], - title=playlist['title'], - description=playlist['description'], - privacyStatus=playlist['privacy'], - moveItem=(playlist['tracks'][0]['setVideoId'], - playlist['tracks'][1]['setVideoId'])) + self.yt_brand.edit_playlist(playlist['id'], + title=playlist['title'], + description=playlist['description'], + privacyStatus=playlist['privacy'], + moveItem=(playlist['tracks'][0]['setVideoId'], + playlist['tracks'][1]['setVideoId'])) self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed") # end to end test adding playlist, adding item, deleting item, deleting playlist diff --git a/ytmusicapi/mixins/playlists.py b/ytmusicapi/mixins/playlists.py index e560059..f2644db 100644 --- a/ytmusicapi/mixins/playlists.py +++ b/ytmusicapi/mixins/playlists.py @@ -96,8 +96,11 @@ class PlaylistsMixin: playlist['duration'] = header['secondSubtitle']['runs'][2]['text'] playlist['trackCount'] = song_count - playlist['tracks'] = [] + playlist['suggestions_token'] = nav( + response, SINGLE_COLUMN_TAB + ['sectionListRenderer', 'contents', 1] + MUSIC_SHELF + + RELOAD_CONTINUATION, True) + playlist['tracks'] = [] if song_count > 0: playlist['tracks'].extend(parse_playlist_items(results['contents'])) songs_to_get = min(limit, song_count) @@ -113,6 +116,25 @@ class PlaylistsMixin: return playlist + def get_playlist_suggestions(self, suggestions_token: str) -> Dict: + """ + Gets suggested tracks to add to a playlist. Suggestions are offered for playlists with less than 100 tracks + + :param suggestions_token: Token returned by :py:func:`get_playlist` or this function + :return: Dictionary containing suggested `tracks` and a `refresh_token` to get another set of suggestions. + For data format of tracks, check :py:func:`get_playlist` + """ + if not suggestions_token: + raise Exception('Suggestions token is None. ' + 'Please ensure the playlist is small enough to receive suggestions.') + endpoint = 'browse' + additionalParams = get_continuation_string(suggestions_token) + response = self._send_request(endpoint, {}, additionalParams) + results = nav(response, ['continuationContents', 'musicShelfContinuation']) + refresh_token = nav(results, RELOAD_CONTINUATION) + suggestions = parse_playlist_items(results['contents']) + return {'tracks': suggestions, 'refresh_token': refresh_token} + def create_playlist(self, title: str, description: str, @@ -212,7 +234,7 @@ class PlaylistsMixin: def add_playlist_items(self, playlistId: str, - videoIds: List[str], + videoIds: List[str] = None, source_playlist: str = None, duplicates: bool = False) -> Union[str, Dict]: """ @@ -226,15 +248,16 @@ class PlaylistsMixin: """ self._check_auth() body = {'playlistId': playlistId, 'actions': []} - for videoId in videoIds: - action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId} - if duplicates: - action['dedupeOption'] = 'DEDUPE_OPTION_SKIP' - body['actions'].append(action) + if not videoIds and not source_playlist: + raise Exception( + "You must provide either videoIds or a source_playlist to add to the playlist") - # add an empty ACTION_ADD_VIDEO because otherwise YTM doesn't return the dict that maps videoIds to their new setVideoIds - if source_playlist and not videoIds: - body['actions'].append({'action': 'ACTION_ADD_VIDEO', 'addedVideoId': None}) + if videoIds: + for videoId in videoIds: + action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId} + if duplicates: + action['dedupeOption'] = 'DEDUPE_OPTION_SKIP' + body['actions'].append(action) if source_playlist: body['actions'].append({ @@ -242,6 +265,11 @@ class PlaylistsMixin: 'addedFullListId': source_playlist }) + # add an empty ACTION_ADD_VIDEO because otherwise + # YTM doesn't return the dict that maps videoIds to their new setVideoIds + if not videoIds: + body['actions'].append({'action': 'ACTION_ADD_VIDEO', 'addedVideoId': None}) + endpoint = 'browse/edit_playlist' response = self._send_request(endpoint, body) if 'status' in response and 'SUCCEEDED' in response['status']: diff --git a/ytmusicapi/parsers/__init__.py b/ytmusicapi/parsers/__init__.py index 856f9e8..35aeca7 100644 --- a/ytmusicapi/parsers/__init__.py +++ b/ytmusicapi/parsers/__init__.py @@ -36,3 +36,4 @@ FEEDBACK_TOKEN = ['feedbackEndpoint', 'feedbackToken'] BADGE_LABEL = [ 'badges', 0, 'musicInlineBadgeRenderer', 'accessibilityData', 'accessibilityData', 'label' ] +RELOAD_CONTINUATION = ['continuations', 0, 'reloadContinuationData', 'continuation'] diff --git a/ytmusicapi/parsers/utils.py b/ytmusicapi/parsers/utils.py index d0cdc92..2ce68d3 100644 --- a/ytmusicapi/parsers/utils.py +++ b/ytmusicapi/parsers/utils.py @@ -100,6 +100,10 @@ def get_parsed_continuation_items(response, parse_func, continuation_type): def get_continuation_params(results, ctoken_path): ctoken = nav(results, ['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation']) + return get_continuation_string(ctoken) + + +def get_continuation_string(ctoken): return "&ctoken=" + ctoken + "&continuation=" + ctoken