Fix search edge case for song results with no duration

This commit is contained in:
sigma67 2021-02-01 11:10:37 +01:00
commit a45e872d16
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 9 additions and 5 deletions

View file

@ -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),

View file

@ -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)