From a45e872d169a06ce22ed3817a6efeea14603e3db Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 1 Feb 2021 11:10:37 +0100 Subject: [PATCH] Fix search edge case for song results with no duration --- ytmusicapi/parsers/browsing.py | 7 +++++-- ytmusicapi/parsers/utils.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 423dbfc..6e871ff 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -1,3 +1,4 @@ +import re from typing import List, Dict from .utils import * from ytmusicapi.helpers import i18n @@ -37,7 +38,8 @@ class Parser: runs = get_flex_column_item(data, 1)['text']['runs'] last_artist_index = get_last_artist_index(runs) - if not search_result['videoId'] and default_offset: # unavailable song in default search edge case + # unavailable song in default search + if not search_result['videoId'] and default_offset: default_offset = 0 search_result['artists'] = parse_song_artists_runs( @@ -66,7 +68,8 @@ class Parser: elif resultType in ['song']: search_result['album'] = parse_song_album_runs(runs, last_artist_index) - search_result['duration'] = runs[-1]['text'] + search_result['duration'] = None if not re.match( + r"^\d+:\d+$", runs[-1]['text']) else runs[-1]['text'] search_result['isExplicit'] = nav(data, BADGE_LABEL, True) == 'Explicit' if 'menu' in data: toggle_menu = find_object_by_key(nav(data, MENU_ITEMS), diff --git a/ytmusicapi/parsers/utils.py b/ytmusicapi/parsers/utils.py index 7ca86bc..e4d0d20 100644 --- a/ytmusicapi/parsers/utils.py +++ b/ytmusicapi/parsers/utils.py @@ -25,8 +25,8 @@ def get_last_artist_index(runs): return next( len(runs) - i - 1 for i, run in enumerate(reversed(runs)) if 'navigationEndpoint' in run and - (nav(run, NAVIGATION_BROWSE_ID).startswith('UC') or - nav(run, NAVIGATION_BROWSE_ID).startswith('FEmusic_library_privately_owned_artist'))) + (nav(run, NAVIGATION_BROWSE_ID).startswith('UC') or nav( + run, NAVIGATION_BROWSE_ID).startswith('FEmusic_library_privately_owned_artist'))) except StopIteration: # try to find album if no artist IDs available if 'navigationEndpoint' in runs[-3] or runs[-3]['text'].endswith('views'): # has album return len(runs) - 5 @@ -45,7 +45,8 @@ def parse_song_album(data, index): def parse_song_album_runs(runs, last_artist_index): - if len(runs) - last_artist_index == 5: # has album + if len(runs) > last_artist_index + 1 and type( + runs[last_artist_index + 2]) is dict: # has album return { 'name': runs[last_artist_index + 2]['text'], 'id': nav(runs[last_artist_index + 2], NAVIGATION_BROWSE_ID, True)