From f0c9708d992c755edfc4b8538c5c818be197d09c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Thu, 13 May 2021 02:46:26 +0200 Subject: [PATCH] Fix search extractor ad section filtering The ads sections had a carouselAdRenderer property, now they have a promotedSparklesTextSearchRenderer property instead. As this may change again in the future, we should just get all items as we discriminate/filter them as videos afterwards with the videoRenderer property. --- extractors/search.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/extractors/search.py b/extractors/search.py index aeb891c..487ccf8 100644 --- a/extractors/search.py +++ b/extractors/search.py @@ -21,10 +21,15 @@ def extract_search(q): r.raise_for_status() content = r.content.decode("utf8") yt_initial_data = extract_yt_initial_data(content) + sections = yt_initial_data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"]["sectionListRenderer"]["contents"] - # find the section with the videos, not the one with the ads - section = next(s for s in sections if "itemSectionRenderer" in s and not (len(s["itemSectionRenderer"]["contents"]) >= 1 and "carouselAdRenderer" in s["itemSectionRenderer"]["contents"][0])) - items = section["itemSectionRenderer"]["contents"] + # youtube searches contain a lot of random stuff, just grab it all for now, then filter to `videoRenderer` later + itemSections = [s for s in sections if "itemSectionRenderer" in s] + + items = [] + for section in itemSections: + items += section["itemSectionRenderer"]["contents"] + results = [] for item in items: if "videoRenderer" in item: