From 311e7684b280e72d1155a320ef48c75105ab1e5d Mon Sep 17 00:00:00 2001 From: syeopite Date: Fri, 26 Mar 2021 21:22:46 -0700 Subject: [PATCH] Add instance redirect on empty/broken search --- locales/en-US.json | 1 + src/invidious/helpers/utils.cr | 26 ++--- src/invidious/routes/misc.cr | 7 +- src/invidious/views/search.ecr | 177 +++++++++++++++++---------------- 4 files changed, 112 insertions(+), 99 deletions(-) diff --git a/locales/en-US.json b/locales/en-US.json index 12663915..1f4cd8d6 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -162,6 +162,7 @@ "Show less": "Show less", "Watch on YouTube": "Watch on YouTube", "Switch Invidious Instance": "Switch Invidious Instance", + "Broken?": "Broken?", "Hide annotations": "Hide annotations", "Show annotations": "Show annotations", "Genre: ": "Genre: ", diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 8c893310..cb94b44a 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -410,18 +410,18 @@ def convert_theme(theme) end end -def fetch_random_instance() - instance_list = HTTP::Client.get "https://api.invidious.io/instances.json" - instance_list = JSON.parse(instance_list.body) - - filtered_instance_list = [] of String - instance_list.as_a.each do |data| - if data[1]["type"] == "https" - if data[1]["monitor"] - health = data[1]["monitor"].as_h["dailyRatios"][0].as_h["ratio"] - filtered_instance_list << data[0].as_s if health.to_s.to_f > 90 - end - end +def fetch_random_instance + instance_list = HTTP::Client.get "https://api.invidious.io/instances.json" + instance_list = JSON.parse(instance_list.body) + + filtered_instance_list = [] of String + instance_list.as_a.each do |data| + if data[1]["type"] == "https" + if data[1]["monitor"] + health = data[1]["monitor"].as_h["dailyRatios"][0].as_h["ratio"] + filtered_instance_list << data[0].as_s if health.to_s.to_f > 90 + end end - return filtered_instance_list.sample(1)[0] + end + return filtered_instance_list.sample(1)[0] end diff --git a/src/invidious/routes/misc.cr b/src/invidious/routes/misc.cr index 857d0596..c14e0e54 100644 --- a/src/invidious/routes/misc.cr +++ b/src/invidious/routes/misc.cr @@ -37,8 +37,13 @@ class Invidious::Routes::Misc < Invidious::Routes::BaseRoute end def cross_instance_redirect(env) + instance_url = fetch_random_instance + if env.params.query["id"]? id = env.params.query["id"] - instance_url = fetch_random_instance env.redirect "https://#{instance_url}/watch?v=#{id}" + elsif env.params.query["q"]? + query, page = env.params.query["q"], env.params.query["page"] + env.redirect "https://#{instance_url}/search?q=#{query}&page=#{page}" + end end end diff --git a/src/invidious/views/search.ecr b/src/invidious/views/search.ecr index fefc9fbb..fdb9205c 100644 --- a/src/invidious/views/search.ecr +++ b/src/invidious/views/search.ecr @@ -2,92 +2,99 @@ <%= search_query.not_nil!.size > 30 ? HTML.escape(query.not_nil![0,30].rstrip(".") + "...") : HTML.escape(query.not_nil!) %> - Invidious <% end %> -
- -

<%= translate(locale, "filter") %>

-
-
-
- <%= translate(locale, "date") %> -
- <% ["hour", "today", "week", "month", "year"].each do |date| %> -
- <% if operator_hash.fetch("date", "all") == date %> - <%= translate(locale, date) %> - <% else %> - &page=<%= page %>"> - <%= translate(locale, date) %> - - <% end %> -
- <% end %> + +<% if count == 0 %> +

+ <%= translate(locale, "Broken?") %> <%= translate(locale, "Switch Invidious Instance") %> +

+<% else %> +
+ +

<%= translate(locale, "filter") %>

+
+
+
+ <%= translate(locale, "date") %> +
+ <% ["hour", "today", "week", "month", "year"].each do |date| %> +
+ <% if operator_hash.fetch("date", "all") == date %> + <%= translate(locale, date) %> + <% else %> + &page=<%= page %>"> + <%= translate(locale, date) %> + + <% end %> +
+ <% end %> +
+
+ <%= translate(locale, "content_type") %> +
+ <% ["video", "channel", "playlist", "movie", "show"].each do |content_type| %> +
+ <% if operator_hash.fetch("content_type", "all") == content_type %> + <%= translate(locale, content_type) %> + <% else %> + &page=<%= page %>"> + <%= translate(locale, content_type) %> + + <% end %> +
+ <% end %> +
+
+ <%= translate(locale, "duration") %> +
+ <% ["short", "long"].each do |duration| %> +
+ <% if operator_hash.fetch("duration", "all") == duration %> + <%= translate(locale, duration) %> + <% else %> + &page=<%= page %>"> + <%= translate(locale, duration) %> + + <% end %> +
+ <% end %> +
+
+ <%= translate(locale, "features") %> +
+ <% ["hd", "subtitles", "creative_commons", "3d", "live", "purchased", "4k", "360", "location", "hdr"].each do |feature| %> +
+ <% if operator_hash.fetch("features", "all").includes?(feature) %> + <%= translate(locale, feature) %> + <% elsif operator_hash.has_key?("features") %> + &page=<%= page %>"> + <%= translate(locale, feature) %> + + <% else %> + &page=<%= page %>"> + <%= translate(locale, feature) %> + + <% end %> +
+ <% end %> +
+
+ <%= translate(locale, "sort") %> +
+ <% ["relevance", "rating", "date", "views"].each do |sort| %> +
+ <% if operator_hash.fetch("sort", "relevance") == sort %> + <%= translate(locale, sort) %> + <% else %> + &page=<%= page %>"> + <%= translate(locale, sort) %> + + <% end %> +
+ <% end %> +
-
- <%= translate(locale, "content_type") %> -
- <% ["video", "channel", "playlist", "movie", "show"].each do |content_type| %> -
- <% if operator_hash.fetch("content_type", "all") == content_type %> - <%= translate(locale, content_type) %> - <% else %> - &page=<%= page %>"> - <%= translate(locale, content_type) %> - - <% end %> -
- <% end %> -
-
- <%= translate(locale, "duration") %> -
- <% ["short", "long"].each do |duration| %> -
- <% if operator_hash.fetch("duration", "all") == duration %> - <%= translate(locale, duration) %> - <% else %> - &page=<%= page %>"> - <%= translate(locale, duration) %> - - <% end %> -
- <% end %> -
-
- <%= translate(locale, "features") %> -
- <% ["hd", "subtitles", "creative_commons", "3d", "live", "purchased", "4k", "360", "location", "hdr"].each do |feature| %> -
- <% if operator_hash.fetch("features", "all").includes?(feature) %> - <%= translate(locale, feature) %> - <% elsif operator_hash.has_key?("features") %> - &page=<%= page %>"> - <%= translate(locale, feature) %> - - <% else %> - &page=<%= page %>"> - <%= translate(locale, feature) %> - - <% end %> -
- <% end %> -
-
- <%= translate(locale, "sort") %> -
- <% ["relevance", "rating", "date", "views"].each do |sort| %> -
- <% if operator_hash.fetch("sort", "relevance") == sort %> - <%= translate(locale, sort) %> - <% else %> - &page=<%= page %>"> - <%= translate(locale, sort) %> - - <% end %> -
- <% end %> -
-
-
+ +<% end %>