From 0107b774f29b0f4cc0a7fabe546db347390337ec Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 28 Apr 2023 17:23:40 +0200 Subject: [PATCH 1/3] Trending: Don't extract items from categories --- src/invidious/trending.cr | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/invidious/trending.cr b/src/invidious/trending.cr index 134eb437..74bab1bd 100644 --- a/src/invidious/trending.cr +++ b/src/invidious/trending.cr @@ -17,7 +17,9 @@ def fetch_trending(trending_type, region, locale) client_config = YoutubeAPI::ClientConfig.new(region: region) initial_data = YoutubeAPI.browse("FEtrending", params: params, client_config: client_config) - trending = extract_videos(initial_data) - return {trending, plid} + items, _ = extract_items(initial_data) + + # Return items, but ignore categories (e.g featured content) + return items.reject!(Category), plid end From 7afa03d821365673e955468eff58009b5fb5c4c8 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 28 Apr 2023 17:27:06 +0200 Subject: [PATCH 2/3] Search: Don't extract items from categories too --- src/invidious/search/processors.cr | 4 ++-- src/invidious/search/query.cr | 23 +---------------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/src/invidious/search/processors.cr b/src/invidious/search/processors.cr index 7e909590..25edb936 100644 --- a/src/invidious/search/processors.cr +++ b/src/invidious/search/processors.cr @@ -10,7 +10,7 @@ module Invidious::Search initial_data = YoutubeAPI.search(query.text, search_params, client_config: client_config) items, _ = extract_items(initial_data) - return items + return items.reject!(Category) end # Search a youtube channel @@ -32,7 +32,7 @@ module Invidious::Search response_json = YoutubeAPI.browse(continuation) items, _ = extract_items(response_json, "", ucid) - return items + return items.reject!(Category) end # Search inside of user subscriptions diff --git a/src/invidious/search/query.cr b/src/invidious/search/query.cr index 24e79609..e38845d9 100644 --- a/src/invidious/search/query.cr +++ b/src/invidious/search/query.cr @@ -113,7 +113,7 @@ module Invidious::Search case @type when .regular?, .playlist? - items = unnest_items(Processors.regular(self)) + items = Processors.regular(self) # when .channel? items = Processors.channel(self) @@ -136,26 +136,5 @@ module Invidious::Search return params end - - # TODO: clean code - private def unnest_items(all_items) : Array(SearchItem) - items = [] of SearchItem - - # Light processing to flatten search results out of Categories. - # They should ideally be supported in the future. - all_items.each do |i| - if i.is_a? Category - i.contents.each do |nest_i| - if !nest_i.is_a? Video - items << nest_i - end - end - else - items << i - end - end - - return items - end end end From 3cfbc19ccc031ec4640f5e06568d2a52ebf90627 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 28 Apr 2023 17:30:01 +0200 Subject: [PATCH 3/3] Extractors: Add utility function to extract items from categories --- src/invidious/yt_backend/extractors_utils.cr | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/invidious/yt_backend/extractors_utils.cr b/src/invidious/yt_backend/extractors_utils.cr index 0cb3c079..b247dca8 100644 --- a/src/invidious/yt_backend/extractors_utils.cr +++ b/src/invidious/yt_backend/extractors_utils.cr @@ -68,19 +68,16 @@ rescue ex return false end -def extract_videos(initial_data : Hash(String, JSON::Any), author_fallback : String? = nil, author_id_fallback : String? = nil) : Array(SearchVideo) - extracted, _ = extract_items(initial_data, author_fallback, author_id_fallback) +# This function extracts the SearchItems from a Category. +# Categories are commonly returned in search results and trending pages. +def extract_category(category : Category) : Array(SearchVideo) + items = [] of SearchItem - target = [] of (SearchItem | Continuation) - extracted.each do |i| - if i.is_a?(Category) - i.contents.each { |cate_i| target << cate_i if !cate_i.is_a? Video } - else - target << i - end + category.contents.each do |item| + target << cate_i if item.is_a?(SearchItem) end - return target.select(SearchVideo) + return items end def extract_selected_tab(tabs)