Merge pull request #208 from colethedj/ytsearchfix

[youtube] fix: ytsearch not returning results sometimes due to promoted content
This commit is contained in:
Tom-Oliver Heidel 2020-11-30 02:34:28 +01:00 committed by GitHub
commit e8dfaa0fd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3469,10 +3469,33 @@ class YoutubeSearchIE(SearchInfoExtractor, YoutubeBaseInfoExtractor):
list) list)
if not slr_contents: if not slr_contents:
break break
isr_contents = []
continuation_token = None
# Youtube sometimes adds promoted content to searches,
# changing the index location of videos and token.
# So we search through all entries till we find them.
for index, isr in enumerate(slr_contents):
if not isr_contents:
isr_contents = try_get( isr_contents = try_get(
slr_contents, slr_contents,
lambda x: x[0]['itemSectionRenderer']['contents'], (lambda x: x[index]['itemSectionRenderer']['contents']),
list) list)
for content in isr_contents:
if content.get('videoRenderer') is not None:
break
else:
isr_contents = []
if continuation_token is None:
continuation_token = try_get(
slr_contents,
lambda x: x[index]['continuationItemRenderer']['continuationEndpoint']['continuationCommand'][
'token'],
compat_str)
if continuation_token is not None and isr_contents:
break
if not isr_contents: if not isr_contents:
break break
for content in isr_contents: for content in isr_contents:
@ -3506,13 +3529,9 @@ class YoutubeSearchIE(SearchInfoExtractor, YoutubeBaseInfoExtractor):
} }
if total == n: if total == n:
return return
token = try_get( if not continuation_token:
slr_contents,
lambda x: x[1]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'],
compat_str)
if not token:
break break
data['continuation'] = token data['continuation'] = continuation_token
def _get_n_results(self, query, n): def _get_n_results(self, query, n):
"""Get a specified number of results for a query""" """Get a specified number of results for a query"""