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

@@ -251,18 +251,22 @@ module Invidious::Routes::API::V1::Channels
def self.search(env)
locale = env.get("preferences").as(Preferences).locale
region = env.params.query["region"]?
env.response.content_type = "application/json"
ucid = env.params.url["ucid"]
query = Invidious::Search::Query.new(env.params.query, :channel, region)
query = env.params.query["q"]?
query ||= ""
# Required because we can't (yet) pass multiple parameter to the
# `Search::Query` initializer (in this case, an URL segment)
query.channel = env.params.url["ucid"]
page = env.params.query["page"]?.try &.to_i?
page ||= 1
begin
search_results = query.process
rescue ex
return error_json(400, ex)
end
search_results = Invidious::Search::Processors.channel(query, page, ucid)
JSON.build do |json|
json.array do
search_results.each do |item|

View File

@@ -5,34 +5,14 @@ module Invidious::Routes::API::V1::Search
env.response.content_type = "application/json"
query = env.params.query["q"]?
query ||= ""
page = env.params.query["page"]?.try &.to_i?
page ||= 1
sort_by = env.params.query["sort_by"]?.try &.downcase
sort_by ||= "relevance"
date = env.params.query["date"]?.try &.downcase
date ||= ""
duration = env.params.query["duration"]?.try &.downcase
duration ||= ""
features = env.params.query["features"]?.try &.split(",").map(&.downcase)
features ||= [] of String
content_type = env.params.query["type"]?.try &.downcase
content_type ||= "video"
query = Invidious::Search::Query.new(env.params.query, :regular, region)
begin
search_params = produce_search_params(page, sort_by, date, content_type, duration, features)
search_results = query.process
rescue ex
return error_json(400, ex)
end
search_results = search(query, search_params, region)
JSON.build do |json|
json.array do
search_results.each do |item|

View File

@@ -212,7 +212,10 @@ module Invidious::Routes::Playlists
end
def self.add_playlist_items_page(env)
locale = env.get("preferences").as(Preferences).locale
prefs = env.get("preferences").as(Preferences)
locale = prefs.locale
region = env.params.query["region"]? || prefs.region
user = env.get? "user"
sid = env.get? "sid"
@@ -236,15 +239,10 @@ module Invidious::Routes::Playlists
return env.redirect referer
end
query = env.params.query["q"]?
if query
begin
search_query, items, operators = process_search_query(query, page, user, region: nil)
videos = items.select(SearchVideo).map(&.as(SearchVideo))
rescue ex
videos = [] of SearchVideo
end
else
begin
query = Invidious::Search::Query.new(env.params.query, :playlist, region)
videos = query.process.select(SearchVideo).map(&.as(SearchVideo))
rescue ex
videos = [] of SearchVideo
end

View File

@@ -37,37 +37,29 @@ module Invidious::Routes::Search
end
def self.search(env)
locale = env.get("preferences").as(Preferences).locale
region = env.params.query["region"]?
prefs = env.get("preferences").as(Preferences)
locale = prefs.locale
query = env.params.query["search_query"]?
query ||= env.params.query["q"]?
region = env.params.query["region"]? || prefs.region
if !query || query.empty?
query = Invidious::Search::Query.new(env.params.query, :regular, region)
if query.empty?
# Display the full page search box implemented in #1977
env.set "search", ""
templated "search_homepage", navbar_search: false
else
page = env.params.query["page"]?.try &.to_i?
page ||= 1
user = env.get? "user"
begin
search_query, videos, operators = process_search_query(query, page, user, region: region)
videos = query.process
rescue ex : ChannelSearchException
return error_template(404, "Unable to find channel with id of '#{HTML.escape(ex.channel)}'. Are you sure that's an actual channel id? It should look like 'UC4QobU6STFB0P71PMvOGN5A'.")
rescue ex
return error_template(500, ex)
end
operator_hash = {} of String => String
operators.each do |operator|
key, value = operator.downcase.split(":")
operator_hash[key] = value
end
env.set "search", query
env.set "search", query.text
templated "search"
end
end