Make use of Search::Query/Filters and associated HTML generator

This commit is contained in:
Samantaz Fox
2022-03-26 20:15:02 +01:00
parent a813955ad3
commit d93a7b315d
10 changed files with 87 additions and 331 deletions

View File

@@ -79,7 +79,7 @@ module Invidious::Search
)
end
def is_default? : Bool
def default? : Bool
return @date.none? && @type.all? && @duration.none? && \
@features.none? && @sort.relevance?
end

View File

@@ -2,22 +2,32 @@ module Invidious::Search
module Processors
extend self
# Regular search (`/search` endpoint)
def regular(query : Query) : Array(SearchItem)
search_params = query.filters.to_yt_params(page: query.page)
client_config = YoutubeAPI::ClientConfig.new(region: query.region)
initial_data = YoutubeAPI.search(query.text, search_params, client_config: client_config)
return extract_items(initial_data)
end
# Search a youtube channel
# TODO: clean code, and rely more on YoutubeAPI
def channel(query, page, channel) : Array(SearchItem)
response = YT_POOL.client &.get("/channel/#{channel}")
def channel(query : Query) : Array(SearchItem)
response = YT_POOL.client &.get("/channel/#{query.channel}")
if response.status_code == 404
response = YT_POOL.client &.get("/user/#{channel}")
response = YT_POOL.client &.get("/c/#{channel}") if response.status_code == 404
response = YT_POOL.client &.get("/user/#{query.channel}")
response = YT_POOL.client &.get("/c/#{query.channel}") if response.status_code == 404
initial_data = extract_initial_data(response.body)
ucid = initial_data.dig?("header", "c4TabbedHeaderRenderer", "channelId").try(&.as_s?)
raise ChannelSearchException.new(channel) if !ucid
raise ChannelSearchException.new(query.channel) if !ucid
else
ucid = channel
ucid = query.channel
end
continuation = produce_channel_search_continuation(ucid, query, page)
continuation = produce_channel_search_continuation(ucid, query.text, query.page)
response_json = YoutubeAPI.browse(continuation)
continuation_items = response_json["onResponseReceivedActions"]?
@@ -34,7 +44,7 @@ module Invidious::Search
end
# Search inside of user subscriptions
def subscriptions(query, page, user : Invidious::User) : Array(ChannelVideo)
def subscriptions(query : Query, user : Invidious::User) : Array(ChannelVideo)
view_name = "subscriptions_#{sha256(user.email)}"
return PG_DB.query_all("
@@ -46,7 +56,7 @@ module Invidious::Search
as document
FROM #{view_name}
) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;",
query, (page - 1) * 20,
query.text, (query.page - 1) * 20,
as: ChannelVideo
)
end

View File

@@ -110,11 +110,10 @@ module Invidious::Search
case @type
when .regular?, .playlist?
all_items = search(@query, @filters, @page, @region)
items = unnest_items(all_items)
items = unnest_items(Processors.regular(self))
#
when .channel?
items = Processors.channel(@query, @page, @channel)
items = Processors.channel(self)
#
when .subscriptions?
if user