search: fix bugs with top result (closes #367)

This commit is contained in:
sigma67 2023-04-02 11:35:40 +02:00
commit fcdb2ae7ba
No known key found for this signature in database
GPG key ID: 1C314227DF0103D0
4 changed files with 14 additions and 5 deletions

View file

@ -50,7 +50,7 @@ class TestYTMusic(unittest.TestCase):
query = "edm playlist"
self.assertRaises(Exception, self.yt_auth.search, query, filter="song")
self.assertRaises(Exception, self.yt_auth.search, query, scope="upload")
results = self.yt.search("l1qwkfkah2l1qwkfkah2")
results = self.yt.search("動水切脱学医発料転探耳金処潟載")
self.assertLessEqual(len(results), 2)
queries = ["taylor swift", "taylor swift blank space", "taylor swift fearless"]
for q in queries:

View file

@ -178,7 +178,10 @@ class SearchMixin:
self.parser.get_search_result_types())
search_results.append(top_result)
if results := nav(res, ['musicCardShelfRenderer', 'contents'], True):
category = nav(results.pop(0), ['messageRenderer'] + TEXT_RUN_TEXT, True)
category = None
# category "more from youtube" is missing sometimes
if 'messageRenderer' in results[0]:
category = nav(results.pop(0), ['messageRenderer'] + TEXT_RUN_TEXT)
type = None
else:
continue

View file

@ -24,7 +24,8 @@ NAVIGATION_BROWSE_ID = NAVIGATION_BROWSE + ['browseId']
PAGE_TYPE = [
'browseEndpointContextSupportedConfigs', 'browseEndpointContextMusicConfig', 'pageType'
]
NAVIGATION_VIDEO_ID = ['navigationEndpoint', 'watchEndpoint', 'videoId']
WATCH_VIDEO_ID = ['watchEndpoint', 'videoId']
NAVIGATION_VIDEO_ID = ['navigationEndpoint'] + WATCH_VIDEO_ID
NAVIGATION_PLAYLIST_ID = ['navigationEndpoint', 'watchEndpoint', 'playlistId']
NAVIGATION_WATCH_PLAYLIST_ID = ['navigationEndpoint', 'watchPlaylistEndpoint', 'playlistId']
NAVIGATION_VIDEO_TYPE = [

View file

@ -17,9 +17,8 @@ def get_search_result_type(result_type_local, result_types_local):
def parse_top_result(data, search_result_types):
search_result = {}
result_type = get_search_result_type(nav(data, SUBTITLE), search_result_types)
search_result['resultType'] = result_type
search_result = {'category': nav(data, CARD_SHELF_TITLE), 'resultType': result_type}
if result_type == 'artist':
subscribers = nav(data, SUBTITLE2, True)
if subscribers:
@ -28,6 +27,12 @@ def parse_top_result(data, search_result_types):
artist_info = parse_song_runs(nav(data, ['title', 'runs']))
search_result.update(artist_info)
if result_type in ['song', 'video']:
on_tap = data.get('onTap')
if on_tap:
search_result['videoId'] = nav(on_tap, WATCH_VIDEO_ID)
search_result['videoType'] = nav(on_tap, NAVIGATION_VIDEO_TYPE)
if result_type in ['song', 'video', 'album']:
search_result['title'] = nav(data, TITLE_TEXT)
runs = nav(data, ['subtitle', 'runs'])[2:]