Unpack search items that are embedded in categories

This is a squash of a bunch of commits
cherry-picked commits

Fix category parse error on search

(cherry picked from commit cc02fed4e69f0eb5f19e017173632b3a3f20519f)

Fix category items not being extracted in search

(cherry picked from commit 2605b9c609ff217b5a6ae09d22450596dcad90fc)

Make search not include category items for now

(cherry picked from commit ca4afd59f46b595e3c339f31432cad98a5771ee1)

Change behavior of categories in search results

(cherry picked from commit cc1067561051b1c113b490e79c4a71cd346f7b3f)

Fix missing search results in extraction

(cherry picked from commit abda6840d5bfe58f845128bdd1a3f4916dd3bb84)

Fix miscount of search results

(cherry picked from commit 491e33450eb1300d0234bb33df0d0e78a027114f)
This commit is contained in:
syeopite 2021-05-08 03:43:26 -07:00
parent a50f64f6e9
commit ae30f32c36
No known key found for this signature in database
GPG Key ID: 6FA616E5A5294A82
2 changed files with 26 additions and 6 deletions

View File

@ -253,8 +253,8 @@ private class CategoryParser < ItemParser
# Content could be in three locations.
if content_container = item_contents["content"]["horizontalListRenderer"]?
elsif content_container = item_contents["content"]["expandedShelfContentsRenderer"]
elsif content_container = item_contents["content"]["verticalListRenderer"]
elsif content_container = item_contents["content"]["expandedShelfContentsRenderer"]?
elsif content_container = item_contents["content"]["verticalListRenderer"]?
else
content_container = item_contents["contents"]
end
@ -332,10 +332,15 @@ private class SearchResultsExtractor < ItemsContainerExtractor
end
private def extract(target)
raw_items = [] of JSON::Any
raw_items = [] of Array(JSON::Any)
content = target["primaryContents"]
renderer = content["sectionListRenderer"]["contents"].as_a[0]["itemSectionRenderer"]
raw_items = renderer["contents"].as_a
renderer = content["sectionListRenderer"]["contents"].as_a.each do |node|
if node = node["itemSectionRenderer"]?
raw_items << node["contents"].as_a
end
end
raw_items = raw_items.flatten
return raw_items
end

View File

@ -232,5 +232,20 @@ def process_search_query(query, page, user, region)
count, items = search(search_query, search_params, region).as(Tuple)
end
{search_query, count, items, operators}
# Light processing to flatten search results out of Categories.
# They should ideally be supported in the future.
items_without_cate_items = [] of SearchItem | ChannelVideo
items.each do |i|
if i.is_a? Category
i.contents.each do |nest_i|
if !nest_i.is_a? Video
items_without_cate_items << nest_i
end
end
else
items_without_cate_items << i
end
end
{search_query, items_without_cate_items.size, items_without_cate_items, url_params}
end