search functions: Don't return result count

This is useless, as the items count can be directly acessed
using the '.size' method, so use that instead when needed.
This commit is contained in:
Samantaz Fox 2022-01-20 21:44:18 +01:00
parent 971b6ec96f
commit 84cc732281
No known key found for this signature in database
GPG key ID: F42821059186176E
7 changed files with 18 additions and 23 deletions

View file

@ -254,7 +254,7 @@ module Invidious::Routes::API::V1::Channels
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
count, search_results = channel_search(query, page, ucid) search_results = channel_search(query, page, ucid)
JSON.build do |json| JSON.build do |json|
json.array do json.array do
search_results.each do |item| search_results.each do |item|

View file

@ -32,7 +32,7 @@ module Invidious::Routes::API::V1::Search
return error_json(400, ex) return error_json(400, ex)
end end
count, search_results = search(query, search_params, region).as(Tuple) search_results = search(query, search_params, region)
JSON.build do |json| JSON.build do |json|
json.array do json.array do
search_results.each do |item| search_results.each do |item|

View file

@ -247,15 +247,13 @@ module Invidious::Routes::Playlists
query = env.params.query["q"]? query = env.params.query["q"]?
if query if query
begin begin
search_query, count, items, operators = process_search_query(query, page, user, region: nil) search_query, items, operators = process_search_query(query, page, user, region: nil)
videos = items.select(SearchVideo).map(&.as(SearchVideo)) videos = items.select(SearchVideo).map(&.as(SearchVideo))
rescue ex rescue ex
videos = [] of SearchVideo videos = [] of SearchVideo
count = 0
end end
else else
videos = [] of SearchVideo videos = [] of SearchVideo
count = 0
end end
env.set "add_playlist_items", plid env.set "add_playlist_items", plid

View file

@ -54,7 +54,7 @@ module Invidious::Routes::Search
user = env.get? "user" user = env.get? "user"
begin begin
search_query, count, videos, operators = process_search_query(query, page, user, region: region) search_query, videos, operators = process_search_query(query, page, user, region: region)
rescue ex : ChannelSearchException 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'.") 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 rescue ex

View file

@ -5,7 +5,7 @@ class ChannelSearchException < InfoException
end end
end end
def channel_search(query, page, channel) def channel_search(query, page, channel) : Array(SearchItem)
response = YT_POOL.client &.get("/channel/#{channel}") response = YT_POOL.client &.get("/channel/#{channel}")
if response.status_code == 404 if response.status_code == 404
@ -24,7 +24,7 @@ def channel_search(query, page, channel)
continuation_items = response_json["onResponseReceivedActions"]? continuation_items = response_json["onResponseReceivedActions"]?
.try &.[0]["appendContinuationItemsAction"]["continuationItems"] .try &.[0]["appendContinuationItemsAction"]["continuationItems"]
return 0, [] of SearchItem if !continuation_items return [] of SearchItem if !continuation_items
items = [] of SearchItem items = [] of SearchItem
continuation_items.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item| continuation_items.as_a.select(&.as_h.has_key?("itemSectionRenderer")).each { |item|
@ -32,17 +32,16 @@ def channel_search(query, page, channel)
.try { |t| items << t } .try { |t| items << t }
} }
return items.size, items return items
end end
def search(query, search_params = produce_search_params(content_type: "all"), region = nil) def search(query, search_params = produce_search_params(content_type: "all"), region = nil) : Array(SearchItem)
return 0, [] of SearchItem if query.empty? return [] of SearchItem if query.empty?
client_config = YoutubeAPI::ClientConfig.new(region: region) client_config = YoutubeAPI::ClientConfig.new(region: region)
initial_data = YoutubeAPI.search(query, search_params, client_config: client_config) initial_data = YoutubeAPI.search(query, search_params, client_config: client_config)
items = extract_items(initial_data)
return items.size, items return extract_items(initial_data)
end end
def produce_search_params(page = 1, sort : String = "relevance", date : String = "", content_type : String = "", def produce_search_params(page = 1, sort : String = "relevance", date : String = "", content_type : String = "",
@ -217,7 +216,7 @@ def process_search_query(query, page, user, region)
search_query = (query.split(" ") - operators).join(" ") search_query = (query.split(" ") - operators).join(" ")
if channel if channel
count, items = channel_search(search_query, page, channel) items = channel_search(search_query, page, channel)
elsif subscriptions elsif subscriptions
if view_name if view_name
items = PG_DB.query_all("SELECT id,title,published,updated,ucid,author,length_seconds FROM ( items = PG_DB.query_all("SELECT id,title,published,updated,ucid,author,length_seconds FROM (
@ -227,16 +226,14 @@ def process_search_query(query, page, user, region)
as document as document
FROM #{view_name} FROM #{view_name}
) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;", search_query, (page - 1) * 20, as: ChannelVideo) ) v_search WHERE v_search.document @@ plainto_tsquery($1) LIMIT 20 OFFSET $2;", search_query, (page - 1) * 20, as: ChannelVideo)
count = items.size
else else
items = [] of ChannelVideo items = [] of ChannelVideo
count = 0
end end
else else
search_params = produce_search_params(page: page, sort: sort, date: date, content_type: content_type, search_params = produce_search_params(page: page, sort: sort, date: date, content_type: content_type,
duration: duration, features: features) duration: duration, features: features)
count, items = search(search_query, search_params, region).as(Tuple) items = search(search_query, search_params, region)
end end
# Light processing to flatten search results out of Categories. # Light processing to flatten search results out of Categories.
@ -254,5 +251,5 @@ def process_search_query(query, page, user, region)
end end
end end
{search_query, items_without_category.size, items_without_category, operators} {search_query, items_without_category, operators}
end end

View file

@ -48,7 +48,7 @@
</div> </div>
<div class="pure-u-1 pure-u-lg-3-5"></div> <div class="pure-u-1 pure-u-lg-3-5"></div>
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right"> <div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
<% if count >= 20 %> <% if videos.size >= 20 %>
<a href="/add_playlist_items?list=<%= plid %>&q=<%= URI.encode_www_form(query.not_nil!) %>&page=<%= page + 1 %>"> <a href="/add_playlist_items?list=<%= plid %>&q=<%= URI.encode_www_form(query.not_nil!) %>&page=<%= page + 1 %>">
<%= translate(locale, "Next page") %> <%= translate(locale, "Next page") %>
</a> </a>

View file

@ -5,7 +5,7 @@
<% search_query_encoded = env.get?("search").try { |x| URI.encode_www_form(x.as(String), space_to_plus: true) } %> <% search_query_encoded = env.get?("search").try { |x| URI.encode_www_form(x.as(String), space_to_plus: true) } %>
<!-- Search redirection and filtering UI --> <!-- Search redirection and filtering UI -->
<% if count == 0 %> <% if videos.size == 0 %>
<h3 style="text-align: center"> <h3 style="text-align: center">
<a href="/redirect?referer=<%= env.get?("current_page") %>"><%= translate(locale, "Broken? Try another Invidious Instance!") %></a> <a href="/redirect?referer=<%= env.get?("current_page") %>"><%= translate(locale, "Broken? Try another Invidious Instance!") %></a>
</h3> </h3>
@ -98,7 +98,7 @@
</details> </details>
<% end %> <% end %>
<% if count == 0 %> <% if videos.size == 0 %>
<hr style="margin: 0;"/> <hr style="margin: 0;"/>
<% else %> <% else %>
<hr/> <hr/>
@ -114,7 +114,7 @@
</div> </div>
<div class="pure-u-1 pure-u-lg-3-5"></div> <div class="pure-u-1 pure-u-lg-3-5"></div>
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right"> <div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
<% if count >= 20 %> <% if videos.size >= 20 %>
<a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>"> <a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>">
<%= translate(locale, "Next page") %> <%= translate(locale, "Next page") %>
</a> </a>
@ -138,7 +138,7 @@
</div> </div>
<div class="pure-u-1 pure-u-lg-3-5"></div> <div class="pure-u-1 pure-u-lg-3-5"></div>
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right"> <div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
<% if count >= 20 %> <% if videos.size >= 20 %>
<a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>"> <a href="/search?q=<%= search_query_encoded %>&page=<%= page + 1 %>">
<%= translate(locale, "Next page") %> <%= translate(locale, "Next page") %>
</a> </a>