From ae30f32c36c738b85dc114a4bb4edaa95257a3c2 Mon Sep 17 00:00:00 2001 From: syeopite Date: Sat, 8 May 2021 03:43:26 -0700 Subject: [PATCH] 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) --- src/invidious/helpers/extractors.cr | 15 ++++++++++----- src/invidious/search.cr | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/invidious/helpers/extractors.cr b/src/invidious/helpers/extractors.cr index 1fa06c91..ea9411d7 100644 --- a/src/invidious/helpers/extractors.cr +++ b/src/invidious/helpers/extractors.cr @@ -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 diff --git a/src/invidious/search.cr b/src/invidious/search.cr index eb9c37c5..3873b2dd 100644 --- a/src/invidious/search.cr +++ b/src/invidious/search.cr @@ -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