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 cc02fed4e6)

Fix category items not being extracted in search

(cherry picked from commit 2605b9c609)

Make search not include category items for now

(cherry picked from commit ca4afd59f4)

Change behavior of categories in search results

(cherry picked from commit cc10675610)

Fix missing search results in extraction

(cherry picked from commit abda6840d5)

Fix miscount of search results

(cherry picked from commit 491e33450e)
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