Feature: add_playlist_items now supports source playlists, duplicate check

This commit is contained in:
sigma67 2020-08-25 13:08:04 +02:00
commit 7c18d8df4b
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 25 additions and 5 deletions

View file

@ -182,10 +182,13 @@ class TestYTMusic(unittest.TestCase):
"test description",
source_playlist="OLAK5uy_lGQfnMNGvYCRdDq9ZLzJV2BJL2aHQsz9Y")
self.assertEqual(len(playlistId), 34, "Playlist creation failed")
response = youtube_auth.add_playlist_items(playlistId, ['y0Hhvtmv0gk'])
response = youtube_auth.add_playlist_items(
playlistId, ['y0Hhvtmv0gk', 'y0Hhvtmv0gk'],
source_playlist='OLAK5uy_nvjTE32aFYdFN7HCyMv3cGqD3wqBb4Jow',
duplicates=True)
self.assertEqual(response, 'STATUS_SUCCEEDED', "Adding playlist item failed")
playlist = youtube_auth.get_playlist(playlistId)
self.assertEqual(len(playlist['tracks']), 41, "Getting playlist items failed")
self.assertEqual(len(playlist['tracks']), 46, "Getting playlist items failed")
response = youtube_auth.remove_playlist_items(playlistId, playlist['tracks'])
self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist item removal failed")
response = youtube_auth.delete_playlist(playlistId)

View file

@ -207,22 +207,39 @@ class PlaylistsMixin:
response = self._send_request(endpoint, body)
return response['status'] if 'status' in response else response
def add_playlist_items(self, playlistId: str, videoIds: List[str]) -> Union[str, Dict]:
def add_playlist_items(self,
playlistId: str,
videoIds: List[str],
source_playlist: str = None,
duplicates: bool = False) -> Union[str, Dict]:
"""
Add songs to an existing playlist
:param playlistId: Playlist id
: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
"""
self._check_auth()
body = {'playlistId': playlistId, 'actions': []}
for videoId in videoIds:
body['actions'].append({'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId})
action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
if duplicates:
action['dedupeOption'] = 'DEDUPE_OPTION_SKIP'
body['actions'].append(action)
if source_playlist:
body['actions'].append({
'action': 'ACTION_ADD_PLAYLIST',
'addedFullListId': source_playlist
})
endpoint = 'browse/edit_playlist'
response = self._send_request(endpoint, body)
return response['status'] if 'status' in response else response
return response['status'] \
if 'status' in response and 'SUCCEEDED' in response['status'] \
else response
def remove_playlist_items(self, playlistId: str, videos: List[Dict]) -> Union[str, Dict]:
"""