Feature: create_playlist supports video_ids and source_playlist parameters

This commit is contained in:
sigma67 2020-05-15 21:35:59 +02:00
commit bcffaca1b9
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 17 additions and 4 deletions

View file

@ -3,7 +3,7 @@ import configparser
from ytmusicapi.ytmusic import YTMusic
config = configparser.RawConfigParser()
config.read('./test.cfg')
config.read('./test.cfg', 'utf-8')
youtube = YTMusic()
youtube_auth = YTMusic(config['auth']['headers_file'])
@ -126,12 +126,15 @@ class TestYTMusic(unittest.TestCase):
# end to end test adding playlist, adding item, deleting item, deleting playlist
def test_end2end(self):
playlistId = youtube_auth.create_playlist("test", "test description")
playlistId = youtube_auth.create_playlist(
"test",
"test description",
source_playlist="OLAK5uy_lGQfnMNGvYCRdDq9ZLzJV2BJL2aHQsz9Y")
self.assertEqual(len(playlistId), 34, "Playlist creation failed")
response = youtube_auth.add_playlist_items(playlistId, ['y0Hhvtmv0gk'])
self.assertEqual(response, 'STATUS_SUCCEEDED', "Adding playlist item failed")
playlist = youtube_auth.get_playlist(playlistId)
self.assertEqual(len(playlist['tracks']), 1, "Getting playlist items failed")
self.assertEqual(len(playlist['tracks']), 41, "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

@ -752,13 +752,17 @@ class YTMusic:
def create_playlist(self,
title: str,
description: str,
privacy_status: str = "PRIVATE") -> str:
privacy_status: str = "PRIVATE",
video_ids: List = None,
source_playlist: str = None) -> str:
"""
Creates a new empty playlist and returns its id.
:param title: Playlist title
:param description: Playlist description
:param privacy_status: Playlists can be 'PUBLIC', 'PRIVATE', or 'UNLISTED'. Default: 'PRIVATE'
:param video_ids: IDs of songs to create the playlist with
:param source_playlist: Another playlist whose songs should be added to the new playlist
:return: ID of the YouTube playlist
"""
self.__check_auth()
@ -767,6 +771,12 @@ class YTMusic:
'description': html_to_txt(description), # YT does not allow HTML tags
'privacyStatus': privacy_status
}
if video_ids is not None:
body['videoIds'] = video_ids
if source_playlist is not None:
body['sourcePlaylistId'] = source_playlist
endpoint = 'playlist/create'
response = self.__send_request(endpoint, body)
return response['playlistId']