From 2e8c09a4307e1ea1d81306bb3b20b700be825e4c Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 30 Jan 2022 14:50:41 +0100 Subject: [PATCH] get_charts: fix unavailable song error in trending section --- ytmusicapi/parsers/browsing.py | 6 +----- ytmusicapi/parsers/explore.py | 9 +++++---- ytmusicapi/parsers/utils.py | 9 +++++++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 9027a50..c176f77 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -180,11 +180,7 @@ def parse_single(result): def parse_video(result): runs = result['subtitle']['runs'] - artists_len = len(runs) - try: - artists_len = runs.index({'text': ' • '}) - except ValueError: - pass + artists_len = get_dot_separator_index(runs) video = { 'title': nav(result, TITLE_TEXT), 'videoId': nav(result, NAVIGATION_VIDEO_ID), diff --git a/ytmusicapi/parsers/explore.py b/ytmusicapi/parsers/explore.py index 3b83617..ecb13d1 100644 --- a/ytmusicapi/parsers/explore.py +++ b/ytmusicapi/parsers/explore.py @@ -43,12 +43,13 @@ def parse_chart_artist(data): def parse_chart_trending(data): flex_0 = get_flex_column_item(data, 0) artists = parse_song_artists(data, 1) - views = artists.pop() # last item is views for some reason - views = views['name'].split(' ')[0] + index = get_dot_separator_index(artists) + # last item is views for some reason + views = None if index == len(artists) else artists.pop()['name'].split(' ')[0] return { 'title': nav(flex_0, TEXT_RUN_TEXT), - 'videoId': nav(flex_0, TEXT_RUN + NAVIGATION_VIDEO_ID), - 'playlistId': nav(flex_0, TEXT_RUN + NAVIGATION_PLAYLIST_ID), + 'videoId': nav(flex_0, TEXT_RUN + NAVIGATION_VIDEO_ID, True), + 'playlistId': nav(flex_0, TEXT_RUN + NAVIGATION_PLAYLIST_ID, True), 'artists': artists, 'thumbnails': nav(data, THUMBNAILS), 'views': views diff --git a/ytmusicapi/parsers/utils.py b/ytmusicapi/parsers/utils.py index f4dd1fd..5f46f68 100644 --- a/ytmusicapi/parsers/utils.py +++ b/ytmusicapi/parsers/utils.py @@ -172,3 +172,12 @@ def find_objects_by_key(object_list, key, nested=None): if key in item: objects.append(item) return objects + + +def get_dot_separator_index(runs): + index = len(runs) + try: + index = runs.index({'text': ' • '}) + except ValueError: + len(runs) + return index