diff options
author | Matthew <colethedj@protonmail.com> | 2020-11-15 09:03:40 +1300 |
---|---|---|
committer | Matthew <colethedj@protonmail.com> | 2020-11-15 09:03:40 +1300 |
commit | 0366ae875692bbe38867761952db70a62e32fd53 (patch) | |
tree | 8bdd79945f99e41d7eaabe6bc79ff5b9e0c15853 /youtube_dlc | |
parent | d052b9a112fb7ae749a829dceba6e3289663a303 (diff) | |
download | hypervideo-pre-0366ae875692bbe38867761952db70a62e32fd53.tar.lz hypervideo-pre-0366ae875692bbe38867761952db70a62e32fd53.tar.xz hypervideo-pre-0366ae875692bbe38867761952db70a62e32fd53.zip |
Fix search to not depend on index position for videoRenderer and token items.
Diffstat (limited to 'youtube_dlc')
-rw-r--r-- | youtube_dlc/extractor/youtube.py | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/youtube_dlc/extractor/youtube.py b/youtube_dlc/extractor/youtube.py index 97cc793f9..76c98ba36 100644 --- a/youtube_dlc/extractor/youtube.py +++ b/youtube_dlc/extractor/youtube.py @@ -3336,10 +3336,33 @@ class YoutubeSearchIE(SearchInfoExtractor, YoutubePlaylistBaseInfoExtractor): list) if not slr_contents: break - isr_contents = try_get( - slr_contents, - lambda x: x[0]['itemSectionRenderer']['contents'], - list) + + 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 len(isr_contents) == 0: + isr_contents = try_get( + slr_contents, + (lambda x: x[index]['itemSectionRenderer']['contents']), + 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: break for content in isr_contents: @@ -3373,13 +3396,9 @@ class YoutubeSearchIE(SearchInfoExtractor, YoutubePlaylistBaseInfoExtractor): } if total == n: return - token = try_get( - slr_contents, - lambda x: x[1]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token'], - compat_str) - if not token: + if not continuation_token: break - data['continuation'] = token + data['continuation'] = continuation_token def _get_n_results(self, query, n): """Get a specified number of results for a query""" |