From 4846cef54b96ab57c78237758de42e67176ad76f Mon Sep 17 00:00:00 2001 From: Matt Carvalho Date: Sat, 20 Mar 2021 20:53:57 -0400 Subject: [PATCH] After adding songs to a playlist return a dict containing the new setVideoIds for each videoId --- tests/test.py | 3 ++- ytmusicapi/mixins/playlists.py | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/test.py b/tests/test.py index b8ab593..cecc375 100644 --- a/tests/test.py +++ b/tests/test.py @@ -256,7 +256,8 @@ class TestYTMusic(unittest.TestCase): playlistId, ['y0Hhvtmv0gk', 'y0Hhvtmv0gk'], source_playlist='OLAK5uy_nvjTE32aFYdFN7HCyMv3cGqD3wqBb4Jow', duplicates=True) - self.assertEqual(response, 'STATUS_SUCCEEDED', "Adding playlist item failed") + self.assertEqual(response["status"], 'STATUS_SUCCEEDED', "Adding playlist item failed") + self.assertGreater(len(response["playlistEditResults"]), 0, "Adding playlist item failed") playlist = youtube_auth.get_playlist(playlistId) self.assertEqual(len(playlist['tracks']), 46, "Getting playlist items failed") response = youtube_auth.remove_playlist_items(playlistId, playlist['tracks']) diff --git a/ytmusicapi/mixins/playlists.py b/ytmusicapi/mixins/playlists.py index 4d789a9..5b79236 100644 --- a/ytmusicapi/mixins/playlists.py +++ b/ytmusicapi/mixins/playlists.py @@ -222,7 +222,7 @@ class PlaylistsMixin: :param videoIds: List of Video ids :param source_playlist: Playlist id of a playlist to add to the current playlist (no duplicate check) :param duplicates: If True, duplicates will be added. If False, an error will be returned if there are duplicates (no items are added to the playlist) - :return: Status String or full response + :return: Status String and a dict that maps the videoIds to their new setVideoIds or full response """ self._check_auth() body = {'playlistId': playlistId, 'actions': []} @@ -232,6 +232,10 @@ class PlaylistsMixin: action['dedupeOption'] = 'DEDUPE_OPTION_SKIP' body['actions'].append(action) + # 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}) + if source_playlist: body['actions'].append({ 'action': 'ACTION_ADD_PLAYLIST', @@ -240,7 +244,7 @@ class PlaylistsMixin: endpoint = 'browse/edit_playlist' response = self._send_request(endpoint, body) - return response['status'] \ + return get_add_to_playlist_results(response) \ if 'status' in response and 'SUCCEEDED' in response['status'] \ else response @@ -270,3 +274,15 @@ class PlaylistsMixin: endpoint = 'browse/edit_playlist' response = self._send_request(endpoint, body) return response['status'] if 'status' in response else response + + +def get_add_to_playlist_results(response): + """ + Get the response data after adding songs to a playlist + + :param response: the response from add_playlist_items + :return: the response status and a dict that maps the videoIds to their new setVideoIds + """ + resp = [resultData.get("playlistEditVideoAddedResultData") for resultData in response.get("playlistEditResults", [])] + return {"status": response["status"], "playlistEditResults": resp} +