search: fix bugs with top result (closes #367)
This commit is contained in:
parent
3a03c635fd
commit
fcdb2ae7ba
4 changed files with 14 additions and 5 deletions
|
|
@ -50,7 +50,7 @@ class TestYTMusic(unittest.TestCase):
|
||||||
query = "edm playlist"
|
query = "edm playlist"
|
||||||
self.assertRaises(Exception, self.yt_auth.search, query, filter="song")
|
self.assertRaises(Exception, self.yt_auth.search, query, filter="song")
|
||||||
self.assertRaises(Exception, self.yt_auth.search, query, scope="upload")
|
self.assertRaises(Exception, self.yt_auth.search, query, scope="upload")
|
||||||
results = self.yt.search("l1qwkfkah2l1qwkfkah2")
|
results = self.yt.search("動水切脱学医発料転探耳金処潟載")
|
||||||
self.assertLessEqual(len(results), 2)
|
self.assertLessEqual(len(results), 2)
|
||||||
queries = ["taylor swift", "taylor swift blank space", "taylor swift fearless"]
|
queries = ["taylor swift", "taylor swift blank space", "taylor swift fearless"]
|
||||||
for q in queries:
|
for q in queries:
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,10 @@ class SearchMixin:
|
||||||
self.parser.get_search_result_types())
|
self.parser.get_search_result_types())
|
||||||
search_results.append(top_result)
|
search_results.append(top_result)
|
||||||
if results := nav(res, ['musicCardShelfRenderer', 'contents'], True):
|
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
|
type = None
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,8 @@ NAVIGATION_BROWSE_ID = NAVIGATION_BROWSE + ['browseId']
|
||||||
PAGE_TYPE = [
|
PAGE_TYPE = [
|
||||||
'browseEndpointContextSupportedConfigs', 'browseEndpointContextMusicConfig', 'pageType'
|
'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_PLAYLIST_ID = ['navigationEndpoint', 'watchEndpoint', 'playlistId']
|
||||||
NAVIGATION_WATCH_PLAYLIST_ID = ['navigationEndpoint', 'watchPlaylistEndpoint', 'playlistId']
|
NAVIGATION_WATCH_PLAYLIST_ID = ['navigationEndpoint', 'watchPlaylistEndpoint', 'playlistId']
|
||||||
NAVIGATION_VIDEO_TYPE = [
|
NAVIGATION_VIDEO_TYPE = [
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,8 @@ def get_search_result_type(result_type_local, result_types_local):
|
||||||
|
|
||||||
|
|
||||||
def parse_top_result(data, search_result_types):
|
def parse_top_result(data, search_result_types):
|
||||||
search_result = {}
|
|
||||||
result_type = get_search_result_type(nav(data, SUBTITLE), search_result_types)
|
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':
|
if result_type == 'artist':
|
||||||
subscribers = nav(data, SUBTITLE2, True)
|
subscribers = nav(data, SUBTITLE2, True)
|
||||||
if subscribers:
|
if subscribers:
|
||||||
|
|
@ -28,6 +27,12 @@ def parse_top_result(data, search_result_types):
|
||||||
artist_info = parse_song_runs(nav(data, ['title', 'runs']))
|
artist_info = parse_song_runs(nav(data, ['title', 'runs']))
|
||||||
search_result.update(artist_info)
|
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']:
|
if result_type in ['song', 'video', 'album']:
|
||||||
search_result['title'] = nav(data, TITLE_TEXT)
|
search_result['title'] = nav(data, TITLE_TEXT)
|
||||||
runs = nav(data, ['subtitle', 'runs'])[2:]
|
runs = nav(data, ['subtitle', 'runs'])[2:]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue