From 02e32896169bcb98bf25ea57f57c3dc34a50ee13 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Wed, 7 Dec 2022 17:39:26 +0100 Subject: [PATCH] library: fix for new response structure and empty libraries (#320) --- ytmusicapi/mixins/library.py | 2 ++ ytmusicapi/navigation.py | 4 +++- ytmusicapi/parsers/library.py | 32 +++++++++++++++++++++++--------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/ytmusicapi/mixins/library.py b/ytmusicapi/mixins/library.py index f941924..25aaacf 100644 --- a/ytmusicapi/mixins/library.py +++ b/ytmusicapi/mixins/library.py @@ -77,6 +77,8 @@ class LibraryMixin: results = response['results'] songs = response['parsed'] + if songs is None: + return [] if 'continuations' in results: request_continuations_func = lambda additionalParams: self._send_request( diff --git a/ytmusicapi/navigation.py b/ytmusicapi/navigation.py index 7952669..25ad1d3 100644 --- a/ytmusicapi/navigation.py +++ b/ytmusicapi/navigation.py @@ -2,7 +2,9 @@ CONTENT = ['contents', 0] RUN_TEXT = ['runs', 0, 'text'] TAB_CONTENT = ['tabs', 0, 'tabRenderer', 'content'] -SINGLE_COLUMN_TAB = ['contents', 'singleColumnBrowseResultsRenderer'] + TAB_CONTENT +TAB_1_CONTENT = ['tabs', 1, 'tabRenderer', 'content'] +SINGLE_COLUMN = ['contents', 'singleColumnBrowseResultsRenderer'] +SINGLE_COLUMN_TAB = SINGLE_COLUMN + TAB_CONTENT SECTION_LIST = ['sectionListRenderer', 'contents'] SECTION_LIST_ITEM = ['sectionListRenderer'] + CONTENT ITEM_SECTION = ['itemSectionRenderer'] + CONTENT diff --git a/ytmusicapi/parsers/library.py b/ytmusicapi/parsers/library.py index f1de247..8aef4db 100644 --- a/ytmusicapi/parsers/library.py +++ b/ytmusicapi/parsers/library.py @@ -98,16 +98,30 @@ def parse_library_artists(response, request_func, limit): def parse_library_songs(response): results = get_library_contents(response, MUSIC_SHELF) - return {'results': results, 'parsed': (parse_playlist_items(results['contents'][1:]))} + return { + 'results': results, + 'parsed': parse_playlist_items(results['contents'][1:]) if results else results + } def get_library_contents(response, renderer): - # first 3 lines are original path prior to #301 - contents = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST, True) - if contents is None: # empty library - return None - results = find_object_by_key(contents, 'itemSectionRenderer') - if results is None: - return nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + renderer, True) + """ + Find library contents. This function is a bit messy now + as it is supporting two different response types. Can be + cleaned up once all users are migrated to the new responses. + :param response: ytmusicapi response + :param renderer: GRID or MUSIC_SHELF + :return: library contents or None + """ + section = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST, True) + contents = None + if section is None: # empty library + contents = nav(response, SINGLE_COLUMN + TAB_1_CONTENT + SECTION_LIST_ITEM + renderer, + True) else: - return nav(results, ITEM_SECTION + renderer) + results = find_object_by_key(section, 'itemSectionRenderer') + if results is None: + contents = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + renderer, True) + else: + contents = nav(results, ITEM_SECTION + renderer, True) + return contents