From 10ef054b2d45506ff758bb24131704eade7ffd57 Mon Sep 17 00:00:00 2001 From: Sergey Smirnov Date: Tue, 21 Jul 2020 15:49:22 +0300 Subject: [PATCH] Fix exception when album has no year (on artist page) --- ytmusicapi/parsers.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ytmusicapi/parsers.py b/ytmusicapi/parsers.py index 95cf8d8..20bf9ea 100644 --- a/ytmusicapi/parsers.py +++ b/ytmusicapi/parsers.py @@ -91,6 +91,7 @@ class Parser: def parse_artist_contents(self, results: List) -> Dict: categories = ['albums', 'singles', 'videos', 'playlists'] categories_local = [_('albums'), _('singles'), _('videos'), _('playlists')] + categories_parser = [parse_album, parse_single, parse_video, parse_playlist] artist = {} for i, category in enumerate(categories): data = [ @@ -109,14 +110,7 @@ class Parser: data[0], CAROUSEL_TITLE)['navigationEndpoint']['browseEndpoint']['params'] - if category == 'videos': - parse_func = parse_video - elif category == 'playlists': - parse_func = parse_playlist - else: - parse_func = parse_album - - artist[category]['results'] = parse_content_list(data[0]['contents'], parse_func) + artist[category]['results'] = parse_content_list(data[0]['contents'], categories_parser[i]) return artist @@ -130,11 +124,20 @@ def parse_content_list(results, parse_func): def parse_album(result): - location = SUBTITLE if len(result['subtitle']['runs']) == 1 else SUBTITLE2 return { 'title': nav(result, TITLE_TEXT), - 'year': nav(result, location), - 'browseId': nav(result, TITLE + NAVIGATION_BROWSE_ID) + 'year': nav(result, SUBTITLE2, True), + 'browseId': nav(result, TITLE + NAVIGATION_BROWSE_ID), + 'thumbnails': nav(result, THUMBNAIL_RENDERER) + } + + +def parse_single(result): + return { + 'title': nav(result, TITLE_TEXT), + 'year': nav(result, SUBTITLE, True), + 'browseId': nav(result, TITLE + NAVIGATION_BROWSE_ID), + 'thumbnails': nav(result, THUMBNAIL_RENDERER) } @@ -408,7 +411,7 @@ def nav(root, items, none_if_absent=False): for k in items: root = root[k] return root - except KeyError as err: + except Exception as err: if none_if_absent: return None else: