From 0acef5ddd907f02d3c98044d37ef4c51ceef0520 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Wed, 27 Sep 2023 21:36:32 +0200 Subject: [PATCH] edit_playlist: add addToTop function parameter (closes #444) toggles a playlist from default behaviour (add to bottom) to "add to top" --- tests/test.py | 11 +++++++---- ytmusicapi/mixins/playlists.py | 15 +++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/test.py b/tests/test.py index f544370..59f09a9 100644 --- a/tests/test.py +++ b/tests/test.py @@ -83,14 +83,14 @@ class TestYTMusic(unittest.TestCase): query = "edm playlist" self.assertRaises(Exception, self.yt_auth.search, query, filter="song") self.assertRaises(Exception, self.yt_auth.search, query, scope="upload") - queries = ["Maylssi", "qllwlwl", "heun"] + queries = ["Monekes", "qllwlwl", "heun"] for q in queries: with self.subTest(): results = self.yt_brand.search(q) self.assertListEqual(["resultType" in r for r in results], [True] * len(results)) - self.assertGreater(len(results), 10) + self.assertGreaterEqual(len(results), 10) results = self.yt.search(q) - self.assertGreater(len(results), 10) + self.assertGreaterEqual(len(results), 10) results = self.yt_auth.search("Martin Stig Andersen - Deteriation", ignore_spelling=True) self.assertGreater(len(results), 0) @@ -413,7 +413,9 @@ class TestYTMusic(unittest.TestCase): def test_get_playlist_foreign(self): self.assertRaises(Exception, self.yt.get_playlist, "PLABC") - playlist = self.yt.get_playlist("PLPK7133-0ahmzknIfvNUMNJglX-O1rTd2", limit=300, suggestions_limit=7) + playlist = self.yt.get_playlist("PLk5BdzXBUiUe8Q5I13ZSCD8HbxMqJUUQA", + limit=300, + suggestions_limit=7) self.assertGreater(len(playlist['duration']), 5) self.assertGreater(len(playlist["tracks"]), 200) self.assertNotIn("suggestions", playlist) @@ -468,6 +470,7 @@ class TestYTMusic(unittest.TestCase): source_playlist="OLAK5uy_lGQfnMNGvYCRdDq9ZLzJV2BJL2aHQsz9Y", ) self.assertEqual(len(playlistId), 34, "Playlist creation failed") + self.yt_brand.edit_playlist(playlistId, addToTop=True) response = self.yt_brand.add_playlist_items( playlistId, [sample_video, sample_video], diff --git a/ytmusicapi/mixins/playlists.py b/ytmusicapi/mixins/playlists.py index 1e6f882..8446290 100644 --- a/ytmusicapi/mixins/playlists.py +++ b/ytmusicapi/mixins/playlists.py @@ -139,7 +139,8 @@ class PlaylistsMixin: has_views = (len(second_subtitle_runs) > 3) * 2 playlist['views'] = None if not has_views else to_int(second_subtitle_runs[0]['text']) has_duration = (len(second_subtitle_runs) > 1) * 2 - playlist['duration'] = None if not has_duration else second_subtitle_runs[has_views + has_duration]['text'] + playlist['duration'] = None if not has_duration else second_subtitle_runs[ + has_views + has_duration]['text'] song_count = second_subtitle_runs[has_views + 0]['text'].split(" ") song_count = to_int(song_count[0]) if len(song_count) > 1 else 0 else: @@ -147,7 +148,8 @@ class PlaylistsMixin: playlist['trackCount'] = song_count - request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams) + request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams + ) # suggestions and related are missing e.g. on liked songs section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer']) @@ -230,7 +232,8 @@ class PlaylistsMixin: description: str = None, privacyStatus: str = None, moveItem: Tuple[str, str] = None, - addPlaylistId: str = None) -> Union[str, Dict]: + addPlaylistId: str = None, + addToTop: bool = False) -> Union[str, Dict]: """ Edit title, description or privacyStatus of a playlist. You may also move an item within a playlist or append another playlist to this playlist. @@ -241,7 +244,8 @@ class PlaylistsMixin: :param privacyStatus: Optional. New privacy status for the playlist :param moveItem: Optional. Move one item before another. Items are specified by setVideoId, see :py:func:`get_playlist` :param addPlaylistId: Optional. Id of another playlist to add to this playlist - :return: Status String or full response + :param addToTop: Optional. Change the state of this playlist to add items to the top of the playlist by default. + :return: Status String or full responwwwse """ self._check_auth() body = {'playlistId': validate_playlist_id(playlistId)} @@ -271,6 +275,9 @@ class PlaylistsMixin: if addPlaylistId: actions.append({'action': 'ACTION_ADD_PLAYLIST', 'addedFullListId': addPlaylistId}) + if addToTop: + actions.append({'action': 'ACTION_SET_ADD_TO_TOP', 'addToTop': 'true'}) + body['actions'] = actions endpoint = 'browse/edit_playlist' response = self._send_request(endpoint, body)