Feature: add get_playlist_suggestions
get_playlist: added suggestions_token add_playlist_items: made videoIds parameter optional
This commit is contained in:
parent
b76eea3738
commit
81afbe118c
4 changed files with 62 additions and 25 deletions
|
|
@ -255,24 +255,28 @@ class TestYTMusic(unittest.TestCase):
|
||||||
self.assertGreater(len(playlist['tracks']), 200)
|
self.assertGreater(len(playlist['tracks']), 200)
|
||||||
|
|
||||||
def test_get_owned_playlist(self):
|
def test_get_owned_playlist(self):
|
||||||
playlist = self.yt_auth.get_playlist(config['playlists']['own'], 300)
|
playlist = self.yt_brand.get_playlist(config['playlists']['own'])
|
||||||
self.assertGreater(len(playlist['tracks']), 200)
|
self.assertLess(len(playlist['tracks']), 100)
|
||||||
|
suggestions = self.yt_brand.get_playlist_suggestions(playlist['suggestions_token'])
|
||||||
|
self.assertGreater(len(suggestions['tracks']), 5)
|
||||||
|
refresh = self.yt_brand.get_playlist_suggestions(suggestions['refresh_token'])
|
||||||
|
self.assertGreater(len(refresh['tracks']), 5)
|
||||||
|
|
||||||
def test_edit_playlist(self):
|
def test_edit_playlist(self):
|
||||||
playlist = self.yt_auth.get_playlist(config['playlists']['own'])
|
playlist = self.yt_brand.get_playlist(config['playlists']['own'])
|
||||||
response = self.yt_auth.edit_playlist(playlist['id'],
|
response = self.yt_brand.edit_playlist(playlist['id'],
|
||||||
title='',
|
title='',
|
||||||
description='',
|
description='',
|
||||||
privacyStatus='PRIVATE',
|
privacyStatus='PRIVATE',
|
||||||
moveItem=(playlist['tracks'][1]['setVideoId'],
|
moveItem=(playlist['tracks'][1]['setVideoId'],
|
||||||
playlist['tracks'][0]['setVideoId']))
|
playlist['tracks'][0]['setVideoId']))
|
||||||
self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed")
|
self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed")
|
||||||
self.yt_auth.edit_playlist(playlist['id'],
|
self.yt_brand.edit_playlist(playlist['id'],
|
||||||
title=playlist['title'],
|
title=playlist['title'],
|
||||||
description=playlist['description'],
|
description=playlist['description'],
|
||||||
privacyStatus=playlist['privacy'],
|
privacyStatus=playlist['privacy'],
|
||||||
moveItem=(playlist['tracks'][0]['setVideoId'],
|
moveItem=(playlist['tracks'][0]['setVideoId'],
|
||||||
playlist['tracks'][1]['setVideoId']))
|
playlist['tracks'][1]['setVideoId']))
|
||||||
self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed")
|
self.assertEqual(response, 'STATUS_SUCCEEDED', "Playlist edit failed")
|
||||||
|
|
||||||
# end to end test adding playlist, adding item, deleting item, deleting playlist
|
# end to end test adding playlist, adding item, deleting item, deleting playlist
|
||||||
|
|
|
||||||
|
|
@ -96,8 +96,11 @@ class PlaylistsMixin:
|
||||||
playlist['duration'] = header['secondSubtitle']['runs'][2]['text']
|
playlist['duration'] = header['secondSubtitle']['runs'][2]['text']
|
||||||
|
|
||||||
playlist['trackCount'] = song_count
|
playlist['trackCount'] = song_count
|
||||||
playlist['tracks'] = []
|
playlist['suggestions_token'] = nav(
|
||||||
|
response, SINGLE_COLUMN_TAB + ['sectionListRenderer', 'contents', 1] + MUSIC_SHELF
|
||||||
|
+ RELOAD_CONTINUATION, True)
|
||||||
|
|
||||||
|
playlist['tracks'] = []
|
||||||
if song_count > 0:
|
if song_count > 0:
|
||||||
playlist['tracks'].extend(parse_playlist_items(results['contents']))
|
playlist['tracks'].extend(parse_playlist_items(results['contents']))
|
||||||
songs_to_get = min(limit, song_count)
|
songs_to_get = min(limit, song_count)
|
||||||
|
|
@ -113,6 +116,25 @@ class PlaylistsMixin:
|
||||||
|
|
||||||
return playlist
|
return playlist
|
||||||
|
|
||||||
|
def get_playlist_suggestions(self, suggestions_token: str) -> Dict:
|
||||||
|
"""
|
||||||
|
Gets suggested tracks to add to a playlist. Suggestions are offered for playlists with less than 100 tracks
|
||||||
|
|
||||||
|
:param suggestions_token: Token returned by :py:func:`get_playlist` or this function
|
||||||
|
:return: Dictionary containing suggested `tracks` and a `refresh_token` to get another set of suggestions.
|
||||||
|
For data format of tracks, check :py:func:`get_playlist`
|
||||||
|
"""
|
||||||
|
if not suggestions_token:
|
||||||
|
raise Exception('Suggestions token is None. '
|
||||||
|
'Please ensure the playlist is small enough to receive suggestions.')
|
||||||
|
endpoint = 'browse'
|
||||||
|
additionalParams = get_continuation_string(suggestions_token)
|
||||||
|
response = self._send_request(endpoint, {}, additionalParams)
|
||||||
|
results = nav(response, ['continuationContents', 'musicShelfContinuation'])
|
||||||
|
refresh_token = nav(results, RELOAD_CONTINUATION)
|
||||||
|
suggestions = parse_playlist_items(results['contents'])
|
||||||
|
return {'tracks': suggestions, 'refresh_token': refresh_token}
|
||||||
|
|
||||||
def create_playlist(self,
|
def create_playlist(self,
|
||||||
title: str,
|
title: str,
|
||||||
description: str,
|
description: str,
|
||||||
|
|
@ -212,7 +234,7 @@ class PlaylistsMixin:
|
||||||
|
|
||||||
def add_playlist_items(self,
|
def add_playlist_items(self,
|
||||||
playlistId: str,
|
playlistId: str,
|
||||||
videoIds: List[str],
|
videoIds: List[str] = None,
|
||||||
source_playlist: str = None,
|
source_playlist: str = None,
|
||||||
duplicates: bool = False) -> Union[str, Dict]:
|
duplicates: bool = False) -> Union[str, Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
@ -226,15 +248,16 @@ class PlaylistsMixin:
|
||||||
"""
|
"""
|
||||||
self._check_auth()
|
self._check_auth()
|
||||||
body = {'playlistId': playlistId, 'actions': []}
|
body = {'playlistId': playlistId, 'actions': []}
|
||||||
for videoId in videoIds:
|
if not videoIds and not source_playlist:
|
||||||
action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
raise Exception(
|
||||||
if duplicates:
|
"You must provide either videoIds or a source_playlist to add to the playlist")
|
||||||
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 videoIds:
|
||||||
if source_playlist and not videoIds:
|
for videoId in videoIds:
|
||||||
body['actions'].append({'action': 'ACTION_ADD_VIDEO', 'addedVideoId': None})
|
action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
||||||
|
if duplicates:
|
||||||
|
action['dedupeOption'] = 'DEDUPE_OPTION_SKIP'
|
||||||
|
body['actions'].append(action)
|
||||||
|
|
||||||
if source_playlist:
|
if source_playlist:
|
||||||
body['actions'].append({
|
body['actions'].append({
|
||||||
|
|
@ -242,6 +265,11 @@ class PlaylistsMixin:
|
||||||
'addedFullListId': source_playlist
|
'addedFullListId': source_playlist
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# 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})
|
||||||
|
|
||||||
endpoint = 'browse/edit_playlist'
|
endpoint = 'browse/edit_playlist'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
if 'status' in response and 'SUCCEEDED' in response['status']:
|
if 'status' in response and 'SUCCEEDED' in response['status']:
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,4 @@ FEEDBACK_TOKEN = ['feedbackEndpoint', 'feedbackToken']
|
||||||
BADGE_LABEL = [
|
BADGE_LABEL = [
|
||||||
'badges', 0, 'musicInlineBadgeRenderer', 'accessibilityData', 'accessibilityData', 'label'
|
'badges', 0, 'musicInlineBadgeRenderer', 'accessibilityData', 'accessibilityData', 'label'
|
||||||
]
|
]
|
||||||
|
RELOAD_CONTINUATION = ['continuations', 0, 'reloadContinuationData', 'continuation']
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,10 @@ def get_parsed_continuation_items(response, parse_func, continuation_type):
|
||||||
def get_continuation_params(results, ctoken_path):
|
def get_continuation_params(results, ctoken_path):
|
||||||
ctoken = nav(results,
|
ctoken = nav(results,
|
||||||
['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation'])
|
['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation'])
|
||||||
|
return get_continuation_string(ctoken)
|
||||||
|
|
||||||
|
|
||||||
|
def get_continuation_string(ctoken):
|
||||||
return "&ctoken=" + ctoken + "&continuation=" + ctoken
|
return "&ctoken=" + ctoken + "&continuation=" + ctoken
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue