Skip to content

Commit

Permalink
edit_playlist: add addToTop function parameter (closes #444)
Browse files Browse the repository at this point in the history
toggles a playlist from default behaviour (add to bottom) to "add to top"
  • Loading branch information
sigma67 committed Sep 27, 2023
1 parent 24024aa commit 0acef5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
11 changes: 7 additions & 4 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ def test_search(self):
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)

Expand Down Expand Up @@ -413,7 +413,9 @@ def test_subscribe_artists(self):

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)
Expand Down Expand Up @@ -468,6 +470,7 @@ def test_end2end(self):
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],
Expand Down
15 changes: 11 additions & 4 deletions ytmusicapi/mixins/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,17 @@ def get_playlist(self,
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:
song_count = len(results['contents'])

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'])
Expand Down Expand Up @@ -230,7 +232,8 @@ def edit_playlist(self,
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.
Expand All @@ -241,7 +244,8 @@ def edit_playlist(self,
: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)}
Expand Down Expand Up @@ -271,6 +275,9 @@ def edit_playlist(self,
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)
Expand Down

0 comments on commit 0acef5d

Please sign in to comment.