After adding songs to a playlist return a dict containing the new setVideoIds for each videoId

This commit is contained in:
Matt Carvalho 2021-03-20 20:53:57 -04:00
commit 4846cef54b
2 changed files with 20 additions and 3 deletions

View file

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

View file

@ -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}