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
|
|
@ -96,8 +96,11 @@ class PlaylistsMixin:
|
|||
playlist['duration'] = header['secondSubtitle']['runs'][2]['text']
|
||||
|
||||
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:
|
||||
playlist['tracks'].extend(parse_playlist_items(results['contents']))
|
||||
songs_to_get = min(limit, song_count)
|
||||
|
|
@ -113,6 +116,25 @@ class PlaylistsMixin:
|
|||
|
||||
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,
|
||||
title: str,
|
||||
description: str,
|
||||
|
|
@ -212,7 +234,7 @@ class PlaylistsMixin:
|
|||
|
||||
def add_playlist_items(self,
|
||||
playlistId: str,
|
||||
videoIds: List[str],
|
||||
videoIds: List[str] = None,
|
||||
source_playlist: str = None,
|
||||
duplicates: bool = False) -> Union[str, Dict]:
|
||||
"""
|
||||
|
|
@ -226,15 +248,16 @@ class PlaylistsMixin:
|
|||
"""
|
||||
self._check_auth()
|
||||
body = {'playlistId': playlistId, 'actions': []}
|
||||
for videoId in videoIds:
|
||||
action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
||||
if duplicates:
|
||||
action['dedupeOption'] = 'DEDUPE_OPTION_SKIP'
|
||||
body['actions'].append(action)
|
||||
if not videoIds and not source_playlist:
|
||||
raise Exception(
|
||||
"You must provide either videoIds or a source_playlist to add to the playlist")
|
||||
|
||||
# add an empty ACTION_ADD_VIDEO because otherwise YTM doesn't return the dict that maps videoIds to their new setVideoIds
|
||||
if source_playlist and not videoIds:
|
||||
body['actions'].append({'action': 'ACTION_ADD_VIDEO', 'addedVideoId': None})
|
||||
if videoIds:
|
||||
for videoId in videoIds:
|
||||
action = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
||||
if duplicates:
|
||||
action['dedupeOption'] = 'DEDUPE_OPTION_SKIP'
|
||||
body['actions'].append(action)
|
||||
|
||||
if source_playlist:
|
||||
body['actions'].append({
|
||||
|
|
@ -242,6 +265,11 @@ class PlaylistsMixin:
|
|||
'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'
|
||||
response = self._send_request(endpoint, body)
|
||||
if 'status' in response and 'SUCCEEDED' in response['status']:
|
||||
|
|
|
|||
|
|
@ -36,3 +36,4 @@ FEEDBACK_TOKEN = ['feedbackEndpoint', 'feedbackToken']
|
|||
BADGE_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):
|
||||
ctoken = nav(results,
|
||||
['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation'])
|
||||
return get_continuation_string(ctoken)
|
||||
|
||||
|
||||
def get_continuation_string(ctoken):
|
||||
return "&ctoken=" + ctoken + "&continuation=" + ctoken
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue