1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-10-31 21:37:30 +00:00

Fix search

This commit is contained in:
Cadence Ember 2020-10-24 00:36:20 +13:00
parent ad1f8df7cb
commit 861f441f9f
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
2 changed files with 5 additions and 2 deletions

View File

@ -21,7 +21,10 @@ def extract_search(q):
r.raise_for_status()
content = r.content.decode("utf8")
yt_initial_data = extract_yt_initial_data(content)
items = yt_initial_data["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"]
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"]
results = []
for item in items:
if "videoRenderer" in item:

View File

@ -1,7 +1,7 @@
import re
import json
r_yt_initial_data = re.compile(r"""^\s*window\["ytInitialData"\] = (\{.*\});\n?$""", re.M)
r_yt_initial_data = re.compile(r"""^(?:\s*window\["ytInitialData"\]|var ytInitialData) = (\{.*\});\s*\n?$""", re.M)
def extract_yt_initial_data(content):
m_yt_initial_data = re.search(r_yt_initial_data, content)