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