diff --git a/ytmusicapi/mixins/playlists.py b/ytmusicapi/mixins/playlists.py index 3ea0dc3..997918c 100644 --- a/ytmusicapi/mixins/playlists.py +++ b/ytmusicapi/mixins/playlists.py @@ -109,77 +109,77 @@ class PlaylistsMixin: response = self._send_request(endpoint, body) results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + ['musicPlaylistShelfRenderer']) - playlist = {} - # own_playlist = 'musicEditablePlaylistDetailHeaderRenderer' in response['header'] - # if not own_playlist: - # header = response['header']['musicDetailHeaderRenderer'] - # playlist['privacy'] = 'PUBLIC' - # else: - # header = response['header']['musicEditablePlaylistDetailHeaderRenderer'] - # playlist['privacy'] = header['editHeader']['musicPlaylistEditHeaderRenderer'][ - # 'privacy'] - # header = header['header']['musicDetailHeaderRenderer'] - # - # playlist['title'] = nav(header, TITLE_TEXT) - # playlist['thumbnails'] = nav(header, THUMBNAIL_CROPPED) - # playlist["description"] = nav(header, DESCRIPTION, True) - # run_count = len(nav(header, SUBTITLE_RUNS)) - # if run_count > 1: - # playlist['author'] = { - # 'name': nav(header, SUBTITLE2), - # 'id': nav(header, SUBTITLE_RUNS + [2] + NAVIGATION_BROWSE_ID, True) - # } - # if run_count == 5: - # playlist['year'] = nav(header, SUBTITLE3) - # - # playlist['views'] = None - # playlist['duration'] = None - # if 'runs' in header['secondSubtitle']: - # second_subtitle_runs = header['secondSubtitle']['runs'] - # 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'] - # 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 - # + playlist = {'id': results['playlistId']} + own_playlist = 'musicEditablePlaylistDetailHeaderRenderer' in response['header'] + if not own_playlist: + header = response['header']['musicDetailHeaderRenderer'] + playlist['privacy'] = 'PUBLIC' + else: + header = response['header']['musicEditablePlaylistDetailHeaderRenderer'] + playlist['privacy'] = header['editHeader']['musicPlaylistEditHeaderRenderer'][ + 'privacy'] + header = header['header']['musicDetailHeaderRenderer'] + + playlist['title'] = nav(header, TITLE_TEXT) + playlist['thumbnails'] = nav(header, THUMBNAIL_CROPPED) + playlist["description"] = nav(header, DESCRIPTION, True) + run_count = len(nav(header, SUBTITLE_RUNS)) + if run_count > 1: + playlist['author'] = { + 'name': nav(header, SUBTITLE2), + 'id': nav(header, SUBTITLE_RUNS + [2] + NAVIGATION_BROWSE_ID, True) + } + if run_count == 5: + playlist['year'] = nav(header, SUBTITLE3) + + playlist['views'] = None + playlist['duration'] = None + if 'runs' in header['secondSubtitle']: + second_subtitle_runs = header['secondSubtitle']['runs'] + 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'] + 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 ) - # - # # suggestions and related are missing e.g. on liked songs - # section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer']) - # playlist['related'] = [] - # if 'continuations' in section_list: - # additionalParams = get_continuation_params(section_list) - # if own_playlist and (suggestions_limit > 0 or related): - # parse_func = lambda results: parse_playlist_items(results) - # suggested = request_func(additionalParams) - # continuation = nav(suggested, SECTION_LIST_CONTINUATION) - # additionalParams = get_continuation_params(continuation) - # suggestions_shelf = nav(continuation, CONTENT + MUSIC_SHELF) - # playlist['suggestions'] = get_continuation_contents(suggestions_shelf, parse_func) - # - # parse_func = lambda results: parse_playlist_items(results) - # playlist['suggestions'].extend( - # get_continuations(suggestions_shelf, - # 'musicShelfContinuation', - # suggestions_limit - len(playlist['suggestions']), - # request_func, - # parse_func, - # reloadable=True)) - # - # if related: - # response = request_func(additionalParams) - # continuation = nav(response, SECTION_LIST_CONTINUATION, True) - # if continuation: - # parse_func = lambda results: parse_content_list(results, parse_playlist) - # playlist['related'] = get_continuation_contents( - # nav(continuation, CONTENT + CAROUSEL), parse_func) + + # suggestions and related are missing e.g. on liked songs + section_list = nav(response, SINGLE_COLUMN_TAB + ['sectionListRenderer']) + playlist['related'] = [] + if 'continuations' in section_list: + additionalParams = get_continuation_params(section_list) + if own_playlist and (suggestions_limit > 0 or related): + parse_func = lambda results: parse_playlist_items(results) + suggested = request_func(additionalParams) + continuation = nav(suggested, SECTION_LIST_CONTINUATION) + additionalParams = get_continuation_params(continuation) + suggestions_shelf = nav(continuation, CONTENT + MUSIC_SHELF) + playlist['suggestions'] = get_continuation_contents(suggestions_shelf, parse_func) + + parse_func = lambda results: parse_playlist_items(results) + playlist['suggestions'].extend( + get_continuations(suggestions_shelf, + 'musicShelfContinuation', + suggestions_limit - len(playlist['suggestions']), + request_func, + parse_func, + reloadable=True)) + + if related: + response = request_func(additionalParams) + continuation = nav(response, SECTION_LIST_CONTINUATION, True) + if continuation: + parse_func = lambda results: parse_content_list(results, parse_playlist) + playlist['related'] = get_continuation_contents( + nav(continuation, CONTENT + CAROUSEL), parse_func) playlist['tracks'] = [] if 'contents' in results: